【问题标题】:How to relate two tables in Laravel 5.0如何在 Laravel 5.0 中关联两个表
【发布时间】:2017-02-15 15:20:08
【问题描述】:

在一个小例子中,我有 3 个表(其中 2 个很重要)。 我的表是 PRODUCT、TRANSFER、WAREHOUSE

我想将产品从 1 个仓库转移到另一个仓库,显然这种转移必须在 TRANSFER TABLE 中,我的示例模型可能是下一个。

HERE THE ENTITY - RELATION - MODEL

现在我正在使用 Laravel 5.0 当我创建模型时,我正在这样做,使用 TRANSFER 模型:

<?php namespace Sicem;

使用 Illuminate\Database\Eloquent\Model;

类 TRANSFER 扩展模型{

/**
 * 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!

}

现在,我的问题在我的 WAREHOUSE 模型中,我不知道该放什么:

<?php namespace Sicem;

使用 Illuminate\Database\Eloquent\Model;

类 WAREHOUSE 扩展模型{

/**
 * 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???????????

}

SIEM:是我的项目名称

请帮帮我。

【问题讨论】:

    标签: laravel models has-many belongs-to database-relations


    【解决方案1】:
    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);
    

    【讨论】:

    • 如果你使用旧版本的 PHP 不支持 Product::class 构造,只需将其替换为 'Product'
    • 忘了添加你的名字空间,类扩展和类似的东西。
    • 你也可以像这样添加Warehouse类的关系:public function transfers() { return $this-&gt;hasMany(Transfer::class); }
    • 只有一个功能?我的意思是,不需要两个吗?因为我在转会表中有两个
    • 是的,使用 laravel 就这么简单。
    猜你喜欢
    • 2021-06-25
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 2014-02-14
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多