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

后端 未结 13 2288
清酒与你
清酒与你 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 17:38

    Now you can use the pluckmethod on a Collection instance:

    This will return only the uuid attribute of the Post model

    App\Models\User::find(2)->posts->pluck('uuid')
    => Illuminate\Support\Collection {#983
         all: [
           "1",
           "2",
           "3",
         ],
       }
    

提交回复
热议问题