And to print out like the names in view do something like this
@foreach ($weapons as $item)
{{ $item->name }}
@endforeach
Hey thanks for that. Should the user_weapon be the name of the model or the actual table?
Because i keep getting this error:
Trying to get property of non-object
You should have two models: User and Weapon. user_weapon is the name of the pivot table. You don't need a third model for that.
Additionally, the code snippet eriktisme posted has a couple of typos - he misspelled "belongsToMany" so that may cause errors if you copy/pasted
Hi thanks for the reply
I just noticed the typo^^ I get no errors now, but still don't see the weapon names :-\
This is my code now:
controller:
weapon model:
user model:
view:
Thanks again for the reply
Hi, on line 9 of your controller, try changing $weapon = Auth::user()->weapons();
to $weapon = Auth::user()->weapons()->get();
This should convert it from a relationship to a collection, allowing you to iterate over it
Finally! It works!
Thanks everyone!
Seriously thanks! I was about to punch my pc xD
Also you could change in your controller
Thanks, that indeed looks better.
One question tho i had to change
if ( ! $user ) {
return View::make('home') ->with('global', 'Profile was not found');
}
to
if(!$user) {
return Redirect::route('home') ->with('global', 'Profile was not found');
}
Otherwise it won't show the global message. Is this normal?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community