jeudi 24 octobre 2019

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 Check Image:

enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire