I'm trying to get selected columns instead of full columns.
$profiles= auth()->user()->profile()->where('id', $id)->select('profile.id','profile.name');
and my profile()
method
public function profile()
{
return $this->hasMany(Profile::class)->get();
}
if i remove the ->select('profile.id','profile.name')
, it returns full columns of profile
table but i just want only a few columns of it. And if I add in ->select()
it gives me error of:
BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::select does not exist. in file
Please advise to change the method call. Thank you.
->get()
afterhasMany()
in your relationship function; you completely remove the ability to chain queries, likeselect()
,where()
, etc, since you're immediately executing the query. Double check the documentation, laravel.com/docs/8.x/eloquent-relationships, none of the example functions use->get()
or->first()
afterhasOne()
,hasMany()
,belongsTo()
, etc, and now you know why :) – Tim Lewis