在一个小示例中,我有3个表(其中2个很重要)。我的表是产品、调拨、仓库
我想要将产品从一个仓库转移到另一个仓库,显然这个转移必须在转移表中,我的示例模型可能是下一个。
HERE THE ENTITY - RELATION - MODEL
现在我使用的是Laravel 5.0,当我创建模型时,我使用的是TRANSFER模型:
<?php namespace Sicem;使用Illuminate\Database\Eloquent\Model;
类转移扩展了模型{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'TRANSFER';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['id','ware_ori_id','ware_end_id','product_id'];
public function product(){
return $this->belongsTo('Sicem\Product');
}//THIS IS OK!
public function sourceware(){
return $this->belongsTo('Sicem\Warehouse\ware_ori_id');
}//I THINK THIS IS OK!
public function endware(){
return $this->belongsTo('Sicem\Warehouse\ware_end_id');
}//I THINK THIS IS OK!}
现在,我的问题是在我的仓库模型中,我不知道我能放什么:
<?php namespace Sicem;使用Illuminate\Database\Eloquent\Model;
类仓库扩展了模型{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'WAREHOUSE';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['id','name'];
public function transfer(){
return $this->hasMany('Sicem\TRANSFER');
}//I supose this.
//But is or not necesary to have 2(two) functions for the relation in my TRANSFER model???????????}
SICEM:是我的项目名称
请帮帮我。
发布于 2016-10-07 03:01:39
class Product {
protected $table = 'PRODUCT';
protected $fillable = ['name'];
public function transfers()
{
return $this->hasMany(Transfer::class);
}
public function transfer($warehouse_from_id, $warehouse_to_id)
{
return Transfer::create([
'product_id' => $this->id,
]);
}
}
class Transfer {
protected $table = 'TRANSFER';
protected $filalble = ['ware_ori_id', 'ware_end_id', 'product_id'];
public function warehouse_from()
{
retrun $this->belongsTo(Warehouse::class);
}
public function warehouse_to()
{
return $this->belongsTo(Warehouse::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
}
class Warehouse {
protected $table = 'WAREHOUSE';
protected $fillable = ['name'];
}所以你需要这样做:
$warehouseFrom = Warehouse::find(1);
$warehouseTo = Warehouse::find(2);
$product = Product::find(23);
$product->transfer($warehouseFrom->id, $warehouseTo->id);https://stackoverflow.com/questions/39903451
复制相似问题