【发布时间】:2017-10-05 12:00:09
【问题描述】:
我正在尝试将数据保存到两个不同的表中,具体取决于选择(x 可编辑)。这是我的代码,并通过指出我做错的地方来帮助我。
我正在寻找的结果:我在 TBL:Product 中将付款状态更改为已付款,TBL:Product_payment 中的已付款值也从 0 更改为 1
TBL:产品
- product_id
- client_id
...
- status {paid/pending}
TBL:Product_payment
- product_id
- payment_id
....
- paid {1/0}
控制器:
public function update()
{
$inputs = Input::all();
if ($row = Product::with('payments')->find($inputs['pk']))
{
$row->$inputs['name'] = $inputs['value'];
if($row['status'] == 'paid') {
$row['paid'] = 1;
}
$row->save();
return $row;
}
Product.php(型号)
class Product extends Eloquent
{
protected $primaryKey = 'product_id';
protected $table = 'products';
protected $fillable = array('client_id', 'date', 'amount', 'status', 'notes');
public function payments()
{
return $this->hasMany('ProductPayment');
}
}
ProductPayment.php(模型)
class ProductPayment extends Eloquent
{
public $table = 'product_payments';
protected $primaryKey = 'product_payment_id';
protected $fillable = array('product_id', 'client_id', 'payment_method', 'amount_paid', 'paid');
public function product()
{
return $this->belongsTo('Products');
}
public function clients()
{
return $this->belongsTo('Clients');
}
}
【问题讨论】:
-
如果我的问题不清楚,请告诉我。
标签: php laravel laravel-5 model eloquent