1

In a little example, I have 3 tables (2 of them are important). My tables are PRODUCT, TRANSFER, WAREHOUSE

I want to transfer the PRODUCT from 1 WAREHOUSE to another and obviously this transfer has to be in the TRANSFER TABLE, My example model could be the next.

HERE THE ENTITY - RELATION - MODEL

Now I'm Using Laravel 5.0 And when I create the models im doing this, with TRANSFER model:

<?php namespace Sicem;

use Illuminate\Database\Eloquent\Model;

class TRANSFER extends 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!

}

Now, My question is here in my WAREHOUSE model, I don't what can I put:

<?php namespace Sicem;

use Illuminate\Database\Eloquent\Model;

class WAREHOUSE extends 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: is my project name

Please Help me.

JORGE GARNICA
  • 1,016
  • 2
  • 9
  • 13

1 Answers1

0
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'];

}

So you need to do something like this:

$warehouseFrom = Warehouse::find(1);
$warehouseTo = Warehouse::find(2);
$product = Product::find(23);
$product->transfer($warehouseFrom->id, $warehouseTo->id);
astratyandmitry
  • 471
  • 3
  • 7