mercredi 2 janvier 2019

Laravel many to many with input pivot table

This is a system for borrowing laboratory equipment, where users are required to fill in their personal data first before being able to borrow laboratory equipment. There is a tool lending form consisting of, name and address that will be stored in the "loan_equipment" table, selecting a list of tools taken from the "lab_equipment" table, and the data will be stored in the "loan_lists" table with the many to many method.

I have 3 tables loan_equipment table (id, name, address), lab_equipment table (id, name), loan_lists table (loan_equipment_id, lab_equipment_id, amount)

This is the LoanEquipment.php model:

public function labEquipment()
{
    return $this->belongsToMany('App\LabEquipment', 'loan_lists', 'loan_equipment_id',
    'lab_equipment_id')->withTimestamps();
}

This is the LabEquipment.php model:

public function loanEquipment()
{
    return $this->hasMany('App\LoanEquipment');
}

This is the LoanEquipmentController.php controller:

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required|max:128',
        'address' => 'required',
        'labEquipment' => 'required'
    ]);

    $input = LoanEquipment::create($request->all());
    $input->labEquipment()->attach($request->input('labEquipment'));

    Session::flash("flash_notification", [
        "level" => "success",
        "message" => "Success $input->name."
    ]);

    return redirect()->route('loan.index');
}

This is a view or form to fill in the loan list tool form.blade.php:

Input Name:

<div class="form-group ">
{!! Form::label('name', 'Nama', ['class' => 'col-md-2 control-label']) !!}
<div class="col-md-6">
    {!! Form::text('name', null, ['class' => 'form-control']) !!}
    <strong>{!! $errors->first('name', '<p class="help-block">:message</p>') !!}</strong>
</div>

Input Address:

<div class="form-group ">
{!! Form::label('address', 'Address', ['class' => 'col-md-2 control-label']) !!}
<div class="col-md-6">
    {!! Form::text('address', null, ['class' => 'form-control']) !!}
    <strong>{!! $errors->first('address', '<p class="help-block">:message</p>') !!}</strong>
</div>

Selected and Input labEquipment and amount

<div class="form-group ">
{!! Form::label('labEquipment', 'Daftar Alat Laboratorium', ['class' => 'col-md-2 control-label']) !!}
<div class="col-md-6">
    @if(count($listsLabEquipment) > 0)
        @foreach($listsLabEquipment as $key => $value)
            <div class="checkbox">
                <label for="">{!! Form::checkbox('labEquipment[]', $key, null) !!}
                    
                </label>
            </div>
            {!! Form::text('amount', null, ['class' => 'form-control']) !!}
        @endforeach
    @endif
    <strong>{!! $errors->first('labEquipment', '<p class="help-block">:message</p>') !!}</strong>
</div>

How can I add or enter data on the number of borrowed tools through the tool lending form? And stored in the "loan_lists" table with the name "amount". How should the code I make in the controller, model, and view?

Thank you for your help



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire