0

How I can add the 'Status' and 'Message'? The status will be true if the request is successful and add the message:success and message:false when the request is failed for example invalid data and add appropriate message text.

Here it is my controller code:

<?php

class authorController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // Get autors

        $authors = Author::with('Authorprofile')->get();


        //Return collection of authors as a resource
        return authorResource::collection($authors);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //only one author with id
        $author = Author::find($id);
        return new authorResource (($author),($author->Authorprofile));
        //return one author


    }
}

Here is the Resource Code:

    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'username' => $this->username,
            'firebase_id' => $this->firebase_id,
            'name' => $this->name,
            'email'=> $this->email,
            'email_verified' =>$this->email_verified,
            'authorprofile'=>$this->authorprofile,
            'is_newsletter_subscribed' =>$this->is_newsletter_subscribed,
            'password'=>$this->password,
            'provider'=>$this->provider,
            'last_ip'=>$this->last_ip,
            'last_login' =>$this->last_login,
            'login_counts'=>$this->login_counts
        ];
    }
}

The Output for now is:

{
    "data": {
        "id": 2,
        "username": "mithun",
        "firebase_id": "2",
        "name": "mithun",
        "email": "[email protected]",
        "email_verified": 0,
        "authorprofile": {
            "id": 2,
            "author_id": 2,
            "image": "mithun.jpg",
            "location": "Bangalore",
            "about": "Co-Founder",
            "created_at": "2019-10-23 03:06:00",
            "updated_at": null
        },
        "is_newsletter_subscribed": 0,
        "password": "mithun",
        "provider": "idk",
        "last_ip": "100.20.3255",
        "last_login": "2019-10-23 08:18:14",
        "login_counts": "1"
    }
}

I want output as :

{
     "Status":"True"
     "message:"Sucess"
     "data":{
 ,,,,,,,,,,,
,,,,,,,,,,,,
         }
}
0

3 Answers 3

1
public function index()
{
    $authors = Author::with('Authorprofile')->get();

    $status = $authors->count() === 0 ? false : true;

    return authorResource::collection($authors)->additional(['status' => $status, 'message' => $status]);
}

public function show($id)
{
    try {
        $author = Author::findOrFail($id);
        return (new authorResource ($author, $author->Authorprofile))
            ->additional(['status' => true, 'message' => true]);
    } catch (\Throwable $e) {
        return response()->json([
            'data' => [],
            'status' => false,
            'message' => false,
        ], 404);
    }
} 
6
  • And If error is there how to get the status and message as false
    – Rakesh
    Oct 24, 2019 at 11:00
  • can i use try ?
    – Rakesh
    Oct 24, 2019 at 11:04
  • I changed my answer. Oct 24, 2019 at 11:15
  • Yes i saw but the problem is when it false like if id is not there it also giving the status as tur
    – Rakesh
    Oct 24, 2019 at 11:19
  • brother actually i am new to php so one this what catch (\Throwable $e) does????
    – Rakesh
    Oct 24, 2019 at 11:45
0

Change this

return authorResource::collection($authors);

to

$response = authorResource::collection($authors);
return ['status' => 200, 'response' => $response];
7
  • If the specified id is not there in the db then how to get the status as false and message out the error.
    – Rakesh
    Oct 24, 2019 at 11:01
  • Can i use Try ?
    – Rakesh
    Oct 24, 2019 at 11:04
  • You can make 'status' => $id != null ? true : false
    – Eyad Jaabo
    Oct 24, 2019 at 11:05
  • or i can add if ?
    – Rakesh
    Oct 24, 2019 at 11:06
  • If statment like the above, you check the $id if not null makes the status true else false
    – Eyad Jaabo
    Oct 24, 2019 at 11:07
0

I hope that code will work for you

if($authors->count()>0){ return $this->sendResponse($authors, 'Success!'); } else { return $this->sendError('No records founds!',[],401); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.