Back

call to undefined method and parse error


I have 3 tables: users, category and profile tables. The profile table has user_id and category_id as foreign keys.

I want to fetch the category name and user name in the profile controller, but I am getting Parse error: syntax error, unexpected ')', expecting '[' or method undefined when i use get() or all() respectively. kindly help me. I have wasted too much time on this project. Thank you. Profile Model `function user() { return $this->belongsTo('App\User'); }

function category() { return $this->belongsTo('App\Category'); }`

User model

function profiles() { return $this->hasMany('App/Profile'); }

Category model

function profile() { return $this->hasMany('App\Profile'); }

Profile Controller-First approach

public function index($category_id) { $profile = Profile::where('category_id',$category_id)->get();

 return view('display_services ', ['profile' => $profile]);

}


Second approach

public function index($category_id) { $profile = Profile::where('category_id',$category_id)->all();

 return view('display_services ', ['profile' => $profile]);

}

 NB:I get Parse error: syntax error, unexpected ')', expecting '[' and Property [contact] does not exist on this collection instance when i use the first approach.

And I get method undefined when this approach(Call to undefined method Illuminate\Database\Query\Builder::all()) when i use the second approach. please help me

View
   
<p>{{$profile->contact}}</p> @foreach($profile->user as users) <p> {{$users->name}}</p> @endforeach

@foreach($profile->category as cat) <p> {{$cat->name}}</p> @endforeach

Tobias van Beek replied 6 months ago

Can you place the code in a code block? As far as I can see there isn't an error in the code you provide but maybe I missed it.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us