You should use Laravel Eloquent Relationship. When you create model file for DB Table, You just put relation with other model.
If Relation has set, than Laravel it self fetch data from Related table.
In your case, Three models are created.
1) User Model.
2) Item Model.
3) ItemDestributed Model.
UserModel.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class User extends Model
{
/**
* Other code goes here
**/
public function itemDestribution()
{
return $this->hasMany('AppItemDistribution','foreign_key');
}
}
ItemModel.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Items extends Model
{
/**
* Other code goes here
**/
public function itemDestribution()
{
return $this->hasMany('AppItemDistribution','foreign_key');
}
}
ItemDestributionModel.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class ItemDestribution extends Model
{
/**
* Other code goes here
**/
public function user()
{
return $this->belongsTo('AppUser','foreign_key_of_user');
}
public function item()
{
return $this->belongsTo('AppItem','foreign_key_of_item');
}
}
You can find more about Eloquent Relation from Here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…