mercredi 16 octobre 2019

Get request from controller in model - Laravel

In my api I used "with" method for get parent's model relation and all thing working fine. I want to add a attribute in my relation and return it in my api but I should use request in my model.

Some thing like this :

Book.php

protected $appends = ['userState'];
public function getUserStateAttribute () {
   return User::find($request->id); //request not exists here currently
}

I have $request in my controller (api controller)

Controller.php

public function get(Request $request) {
   Post::with('books')->all();
}

I believe using static content in appends array of model is so easy but how about using request's based content ?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2MIOWWt
via IFTTT

Aucun commentaire:

Enregistrer un commentaire