murilo

Enable and Desable Appends in a Laravel Search

Posted 6 months ago by murilo

Hello , I just discovered working with appends in my Model . I have been working with Vue js , and I have been working to mutch with Json request searches . In some cases I have to use Appends in my model to get some results in my search , like this -

CONTROLLER ->

 $result  = Post::select(['id', 'title' , 'description'])->get()

  return response()->json($result) .

MODEL ->


class POST extends Model
{
    protected $appends = ['is_liked'];

public function getIsLikedAttribute() 
    {
        return $this->likes->where('user_id', auth()->id())->count() > 0;
    }

    }

THE RESULT WILL BE LIKE THIS ->

[{ "id":1 , "title":title , "description":post_description ,"likes":true , .... } ,
  { "id":2 , "title":other title , "description":other post_description ,"likes":false , .... }]

How could I desable appends in my search ? or just use this when I want ?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.