Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 1 year ago.
0

http://laravel.io/bin/wGJ5Q

And to print out like the names in view do something like this

@foreach ($weapons as $item)
    {{ $item->name }}
@endforeach
Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

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:

http://laravel.io/bin/QqNWa

weapon model:

http://laravel.io/bin/n5Q89

user model:

http://laravel.io/bin/wGJy2

view:

http://laravel.io/bin/6EL9j

Thanks again for the reply

Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

Finally! It works!

Thanks everyone!

Seriously thanks! I was about to punch my pc xD

Last updated 1 year ago.
0

Also you could change in your controller

http://laravel.io/bin/PymYP

Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Dieter91 dieter91 Joined 9 Jun 2014

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.