mardi 27 septembre 2016

hasOne and belongsTo relation tables in laravel

my tables :

users :

id fname email

brands:

id title user_id_made

each brand has a user_id_made that is a foreign key of users table.

in Brand model :

public function user()
{
    return $this->hasOne('App\Models\User','id');
}

in User model:

public function brand()
{
    return $this->belongsTo('App\Models\Brand','user_id_made');
}

I have a list of brands like this :

title of brand       -   email of user that made it   

in my controller I write this code :

$data['brands']  = Brand::with('user')->simplePaginate(2);

in foreach of view I got trying to get non-object property error :

            @foreach($brands as $brand)



                <tr>
                 <td></td>
                 <td></td>
                 <td></td> // error line



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire