jeudi 13 octobre 2016

Laravel 5 eloquent: select specific column of relation

I have 2 models: Author and Post

In my Post model, I have an author function (One To Many (Inverse)):

public function author()
    {
        return $this->belongsTo('App\Author', 'author_id');
    }

Now I want to export to Excel the following information:

id | title | author name

This is what I tried:

$posts = Post::with(array(
        'author' => function ($query) {
            $query->select('name');
        }))->select('id', 'title')->get();

What I get is an empty author's name column.

What do I wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire