Get Specific Columns Using “With()” Function in Laravel Eloquent

后端 未结 13 2158
清酒与你
清酒与你 2020-11-22 16:57

I have two tables, User and Post. One User can have many posts and one post belongs to only one user

13条回答
  •  有刺的猬
    2020-11-22 18:00

    Note that if you only need one column from the table then using 'lists' is quite nice. In my case i am retrieving a user's favourite articles but i only want the article id's:

    $favourites = $user->favourites->lists('id');
    

    Returns an array of ids, eg:

    Array
    (
        [0] => 3
        [1] => 7
        [2] => 8
    )
    

提交回复
热议问题