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

后端 未结 13 2172
清酒与你
清酒与你 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:48

    If you want to get specific columns using with() in laravel eloquent then you can use code as below which is originally answered by @Adam in his answer here in response of this same question :

    Post::with('user:id,username')->get();
    

    So i have used it in my code but it was giving me error of 1052: Column 'id' in field list is ambiguous, so if you guys are also facing same problem

    Then for solving it you have to specify table name before the id column in with() method as below code:

    Post::with('user:user.id,username')->get();
    

提交回复
热议问题