Rakesh
Rakesh

Reputation: 43

How to add the 'Status', 'message' with this in laravel

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":{
 ,,,,,,,,,,,
,,,,,,,,,,,,
         }
}

Upvotes: 0

Views: 1617

Answers (3)

Serhii Posternak
Serhii Posternak

Reputation: 514

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);
    }
} 

Upvotes: 1

Ani
Ani

Reputation: 69

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); }

Upvotes: 0

Eyad Jaabo
Eyad Jaabo

Reputation: 389

Change this

return authorResource::collection($authors);

to

$response = authorResource::collection($authors);
return ['status' => 200, 'response' => $response];

Upvotes: 0

Related Questions