Laravel:如何从View / Blade获取数据并传递给Controller [英] Laravel: How to get data from View/Blade and pass to Controller

查看:528
本文介绍了Laravel:如何从View / Blade获取数据并传递给Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户(id,名称),项目(id,名称,onloan),交易(patron_id,item_id,借出,到期,返回) )

Patron(id, name), Item(id, name, onloan), Transactions (patron_id, item_id, loaned, due, returned)

Patron.php

Patron.php

    public function transaction ()
    {
        return $this->hasMany(Transaction::class);
    }

Item.php

    public function transaction ()
    {
        return $this->hasMany(Transaction::class);
    }

Transaction.php

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 )表需要更新。

ie

Transactions表:(此部分已经在工作,没关系)

名称...。 .. |项目.......... |贷款..... |到期..

John Doe |哈利·波特| 09/22/20 | 20年9月23日

TransactionController.php

This part is where I have problem, Two (2) tables need to be updated.
i.e.
Transactions table: (this part is already working, it's ok)
Name......| Item..........| Loaned.....| Due..
John Doe | Harry Potter | 09/22/20 | 09/23/20

项目表:(这一部分我不知道如何在控制器中添加)

名称... ..... | Onloan

哈利·波特| 1

Items table: (this part I don't know how to add it in the Controller)
Name...........| Onloan
Harry Potter | 1

•如何在此控制器中更新外表(项),以使
中的 onloan $ item-> id的值是1。

• how update a foreign table (Items) in this controller so that
the "onloan" value of that $item->id is 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,
        ]);

请帮助。谢谢。

推荐答案

Υ您可以这样写

Item::find($request->item_id)->update(['onloan' => 1,]);

创建您也可以做到的

 Transaction::create([$request->all()]);

这篇关于Laravel:如何从View / Blade获取数据并传递给Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆