Laravel:如何从View/Blade获取数据并传递给Controller
Laravel: How to get data from View/Blade and pass to Controller
表:项目有布尔值“onloan”
赞助人(id,姓名),项目(id,姓名,onloan),交易(patron_id,item_id,借出,到期,归还)
关系:
Patron.php
public function transaction ()
{
return $this->hasMany(Transaction::class);
}
Item.php
public function transaction ()
{
return $this->hasMany(Transaction::class);
}
Transaction.php
public function item()
{
return $this->belongsTo(Item::class);
// return $this->belongsTo('App\Item','item_id');
}
public function patron()
{
return $this->belongsTo(Patron::class);
}
查看:create.blade.php
<label for="item_id">Item</label>
<select name="item_id" id="item_id" class="form-control select2">
@foreach($items as $item)
<option value="{{ $item->id }}">
{{ $item->barcode }} - {{ $item->name }}
</option>
@endforeach
</select>
TransactionController.php
这部分是我有问题的地方,两 (2) 个 table 需要更新。
即
Transactionstable:(这部分已经可以了,没关系)
姓名……|项目…………|借出.....|由于..
约翰·多伊 |哈利·波特 | 20 年 9 月 22 日 | 20 年 9 月 23 日
项目table:(这部分我不知道如何在Controller中添加)
姓名…………|外借
哈利·波特 | 1
• 如何更新此控制器中的外来 table(项目)以便
$item->id 的 "onloan" 值为 1.
public function store(TransactionRequest $request)
{
Transaction::create([
'patron_id' => $request->patron_id,
'item_id' => $request->item_id,
'loaned' => $request->loaned,
'due' => $request->due,
]);
//This is what I tried, but it's not working.
Item::find($request->item_id);
$item->update([
'onloan' => 1,
]);
请帮忙。谢谢。
Y你可以这样写
Item::find($request->item_id)->update(['onloan' => 1,]);
创造你也可以做到
Transaction::create([$request->all()]);
表:项目有布尔值“onloan”
赞助人(id,姓名),项目(id,姓名,onloan),交易(patron_id,item_id,借出,到期,归还)
关系:
Patron.php
public function transaction ()
{
return $this->hasMany(Transaction::class);
}
Item.php
public function transaction ()
{
return $this->hasMany(Transaction::class);
}
Transaction.php
public function item()
{
return $this->belongsTo(Item::class);
// return $this->belongsTo('App\Item','item_id');
}
public function patron()
{
return $this->belongsTo(Patron::class);
}
查看:create.blade.php
<label for="item_id">Item</label>
<select name="item_id" id="item_id" class="form-control select2">
@foreach($items as $item)
<option value="{{ $item->id }}">
{{ $item->barcode }} - {{ $item->name }}
</option>
@endforeach
</select>
TransactionController.php
这部分是我有问题的地方,两 (2) 个 table 需要更新。
即
Transactionstable:(这部分已经可以了,没关系)
姓名……|项目…………|借出.....|由于..
约翰·多伊 |哈利·波特 | 20 年 9 月 22 日 | 20 年 9 月 23 日
项目table:(这部分我不知道如何在Controller中添加)
姓名…………|外借
哈利·波特 | 1
• 如何更新此控制器中的外来 table(项目)以便
$item->id 的 "onloan" 值为 1.
public function store(TransactionRequest $request)
{
Transaction::create([
'patron_id' => $request->patron_id,
'item_id' => $request->item_id,
'loaned' => $request->loaned,
'due' => $request->due,
]);
//This is what I tried, but it's not working.
Item::find($request->item_id);
$item->update([
'onloan' => 1,
]);
请帮忙。谢谢。
Y你可以这样写
Item::find($request->item_id)->update(['onloan' => 1,]);
创造你也可以做到
Transaction::create([$request->all()]);