jeudi 18 juin 2020

how to insert multiple values from checkbox into database and display it in my view table

i am trying to insert multiple values checkbox into database and display it in my view table, i search some solution here that related to my problem but it doesn't work. the one that i try to insert multiple value is on my supplier phone, i have a table supplier that consist of "supplier name" and "supplier phone number" and it's relationship with my table stockin, so in my stockin add form i make a looping to display input checkbox of my supplier name from table supplier, so when i choose the supplier name then the supplier phone that i choose will be display in my view form instead of the supplier name.

StockinController.php

public function create(Request $request)
{

    \App\Stockin::create($request->all());
    return redirect('/stockin')->with('sukses','Data Stockin berhasil diinput');

    foreach ($request->input("sp_id") as $supplier){
        $add_supplier = new Stockin;
        $add_supplier->sp_id= $supplier;
        $add_supplier->save();
    }
}

addstockin.blade.php

  <div class="form-group">
    <label for="sp_id">Supplier</label>
    <br>
     @php $no = 1; @endphp   
        @foreach($data_supplier as $supplier)
        <tr>  
        <input type="checkbox" id="sp_id" value="" name="sp_id[]">
        
        <br>
        </tr>
     @endforeach
  </div>

model Stockin.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Stockin extends Model
{
    protected $table = 'stockin';
    protected $primaryKey = 'stockin_id';
    protected $fillable = ['item_id','detail','sp_id','qty','tanggal'];

    public function item_r(){
        return $this->belongsTo('App\Item','item_id','item_id');
    }
    public function supplier_r(){
        return $this->belongsTo('App\Supplier','sp_id','sp_id');
    }
    public function getTanggalAttribute()
    {
        return \Carbon\Carbon::parse($this->attributes['tanggal'])->format('d-M-Y');
    }
}

stockinform.blade.php

<tbody>
        @php $no = 1; @endphp                   
        @foreach($data_stockin as $stockin)
        <tr>
            <td style="width:5%;"></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td class="text-center" width="160px">
                <a id="set_dtl" class="btn btn-default btn-xs" data-toggle="modal" data-target="#modal-detail"
                    data-barcode=""
                    data-item_name=""
                    data-detail=""
                    data-suppliername=""
                    data-qty=""
                    data-tanggal="">
                    <i class="fa fa-eye"></i> Details
                </a>
                <a href="#" class="btn btn-danger btn-xs delete" stockin-id = "" stockin-name="">
                    <i class="fa fa-trash"></i>Delete
                </a>
            </td>
        </tr>
        @endforeach
    </tbody>

thank you in advance :)




Aucun commentaire:

Enregistrer un commentaire