mercredi 30 novembre 2016

Transform specific fields in Laravel?

How do I Transform specific fields in Laravel?

For example:

$user = \Auth::User();
return [
    'user_id'  => $user->id,
    'articles' => $user->articles,
    'pages'    => $user->pages,
];

$user->articles will show the entries the articles table and $user->pages from the pages table.

In the User model, it would be defined something like that:

public function articles()
{
    return $this->hasMany(Article::class);
}

I do not want to return all the fields from $user->articles, just id, name, and description. What is good way to transform this collection?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire