简体   繁体   English

Laravel 8 - 如何将一个表中的值更新到另一个表中

[英]Laravel 8 - How to update the value in one table to another table

I'm sorry this question might be a little lengthy, I wanna make sure that my problem is addressed thoroughly.很抱歉这个问题可能有点冗长,我想确保我的问题得到彻底解决。

I have a truck table which looks like this on the interface:我有一个在界面上看起来像这样的卡车表:

在此处输入图像描述

As you can see, I have truck number 12, 13, 15, and the number can go on.如您所见,我有 12、13、15 号卡车,号码是 go。 As for the 'Quantity' column, it represents how many packages is assigned to that particular truck.至于“数量”列,它表示分配给该特定卡车的包裹数量。 There's a one to many relationship between the Truck and Package tables. Truck 和 Package 表之间存在一对多关系。 One truck can have many packages.一辆卡车可以装很多包裹。 One package belongsto a single truck.一个package属于单车。

There's a separate Package page exist for user to add new packages and assign the packages to specific truck.有一个单独的 Package 页面供用户添加新包裹并将包裹分配给特定卡车。 Those Packages will be automatically added into the 'quantity' column on the truck page.这些包裹将自动添加到卡车页面的“数量”栏中。

The Package page looks like this on the interface. Package页面在界面上是这样的。 It's still empty because there's no records yet, and this is where my problem comes.它仍然是空的,因为还没有记录,这就是我的问题所在。 在此处输入图像描述

when user click on the New Package Details button, they'll be directed to this:当用户点击New Package Details按钮时,他们将被引导至此:

在此处输入图像描述

as you can see, the truck number is displayed in a drop down menu, and the values are taken from the truck table we seen above.如您所见,卡车编号显示在下拉菜单中,值取自我们上面看到的卡车表。

When user inserts a new truck data into the truck table, the dropdown list on this create Package is supposed to be updated automatically.当用户将新卡车数据插入卡车表时,此创建 Package 上的下拉列表应该会自动更新。 I hardcoded '10' into the menu because i was trying figure out how to do it.我将“10”硬编码到菜单中,因为我正在尝试弄清楚如何去做。 We should be seeing '12', '13', and '15' on this list.我们应该在此列表中看到“12”、“13”和“15”。 And when user click on the truck number, for example '12', the quantity inside the truck page should be updated as well.当用户点击卡车号码时,例如“12”,卡车页面内的数量也应该更新。

My Truck and Package Model:我的卡车和 Package Model:

class Truck extends Model
{
    use HasFactory;
    protected $primaryKey = 'truck_id';
    protected $fillable = ['truck_number', 'no_of_items', 'postman_name', 'date_of_operation', 'status'];
    
     public function Package()
    {
        return $this->hasMany(Package::class);
    }
}

class Package extends Model
{
    use HasFactory;
    protected $primaryKey = 'package_id';

    protected $fillable = ['truck_number', 'package_number', 'destination', 'date_of_operation'];
    
     public function Truck(){
        return $this->belongsTo(Truck::class);
    }

}

My migrations file:我的迁移文件:

Schema::create('trucks', function (Blueprint $table) {
            $table->increments('truck_id');
            $table->string('truck_number')->unique();
            $table->integer('no_of_items')->nullable();
            $table->string('postman_name');
            $table->date('date_of_operation');
            $table->string('status');
            $table->timestamps();
        });

Schema::create('packages', function (Blueprint $table) {
            $table->increments('package_id');
            $table->string('truck_number')->nullable(); 
            $table->foreign('truck_number')->references('truck_number')->on('trucks');
            $table->string('package_number')->unique();
            $table->string('destination');
            $table->date('date_of_operation');
            $table->timestamps();
        });

package's create.blade.php file:包的create.blade.php文件:

<form method="post" action="{{ route('package.store') }}">
          @csrf
          <div class="form-group">    
              <label for="truck_number">Truck Number</label>
              <select name="truck_number" class="form-control">
              <option> 10 </option>
              </select>
          </div>
          <div class="form-group">
              <label for="package_name">Package Number</label>
              <input type="text" class="form-control" name="package_number"/>
          </div>
          <div class="form-group">
              <label for="destination">Destination</label>
              <input type="text" class="form-control" name="destination"/>
          </div>
          <div class="form-group">
              <label for="date_of_operation">Date of Operation</label>
              <input type="date" class="form-control" name="date_of_operation"/>
          </div>
          
          <div class="row justify-content-center">
          <a href="{{ route('package.index')}}" class="btn btn-primary bg-danger">Return</a>&nbsp;&nbsp;                        
          <button type="submit" class="btn btn-primary text-center bg-success">Save Details</button>
          </div>
      </form>

Package controller: Package controller:

public function create()
    {
        $trucks = Truck::all('truck_number');
        return view('package.create')->with('truck_number', $trucks);
    }

 public function store(Request $request)
    {
        //Validate
        $request->validate([
            'truck_number' => 'required',
            'package_number' => 'required|unique:packages',
            'destination' => 'required',
            'date_of_operation' => 'required'
        ]);
        $package = Package::create([
            'truck_number' => $request->truck_number,
            'package_number' => $request->package_number,
            'destination' => $request->destination,
            'date_of_operation' => $request->date_of_operation
        ]);
        return redirect('package');
       
    }

how do I connect the truck_number value in Truck Details page to the truck_number values in Package Details' drop down menu list and make the dropdown list automatically updated everytime a new truck_number is assigned?如何将Truck Details页面中的truck_number值连接到Package Details下拉菜单列表中的truck_number值,并使下拉列表在每次分配新的truck_number自动更新?

I think you are trying to loop through your truck ids to populate a select HTML element.我认为您正在尝试遍历您的卡车 ID 以填充 select HTML 元素。

Try updating your blade to this.尝试将您的刀片更新为此。

<form method="post" action="{{ route('package.store') }}">
          @csrf
          <div class="form-group">    
              <label for="truck_number">Truck Number</label>
              <select name="truck_number" class="form-control">
              @foreach ($truck_number as $number)
                  <option value="{{$number['truck_number']}}">{{$number['truck_number']}}</option>
              @endforeach

              </select>
          </div>
          <div class="form-group">
              <label for="package_name">Package Number</label>
              <input type="text" class="form-control" name="package_number"/>
          </div>
          <div class="form-group">
              <label for="destination">Destination</label>
              <input type="text" class="form-control" name="destination"/>
          </div>
          <div class="form-group">
              <label for="date_of_operation">Date of Operation</label>
              <input type="date" class="form-control" name="date_of_operation"/>
          </div>
          
          <div class="row justify-content-center">
          <a href="{{ route('package.index')}}" class="btn btn-primary bg-danger">Return</a>&nbsp;&nbsp;                        
          <button type="submit" class="btn btn-primary text-center bg-success">Save Details</button>
          </div>
      </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM