简体   繁体   中英

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. 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. One truck can have many packages. One package belongsto a single truck.

There's a separate Package page exist for user to add new packages and assign the packages to specific truck. Those Packages will be automatically added into the 'quantity' column on the truck page.

The Package page looks like this on the interface. 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:

在此处输入图像描述

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. I hardcoded '10' into the menu because i was trying figure out how to do it. We should be seeing '12', '13', and '15' on this list. And when user click on the truck number, for example '12', the quantity inside the truck page should be updated as well.

My Truck and 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:

<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:

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?

I think you are trying to loop through your truck ids to populate a select HTML element.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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