Laravel multiple data update

 

 


 

edit.blade.php

 

@extends('layouts.admin.master')
@section('title','bazar')
@section('content')
<div class="">
@include('admin.messages.message')

{{Form::open(['url'=>'/bazars/'.$data->id ,'method'=>'PATCH','class'=>'form-horizontal','enctype'=>'multipart/form-data'])}}

<section class="panel">
<div class="panel panel-footer">
<center><h3>Daily Bazar List</h3></center>
<header >
  <div class="row">
    <div class="col-md-2">
      <select name="shopkeepers_id" class="form-control" required="">
       @foreach($shopkeepers as $shop)
       @if($data->shopkeepers_id==$shop->id)
        <option value="{{$shop->id}}" selected="">{{$shop->shop_name}}</option>
        @else
        <option value="{{$shop->id}}">{{$shop->shop_name}}</option>
        @endif
   @endforeach
      </select>
    </div>
    <div class="col-md-2">
      <select name="months_id" class="form-control" required="">
          <option value="{{$data->months_id}}">{{$data->months_id}}</option>
        @foreach($months as $month)
         
        <option value="{{$month->month}}">{{$month->month}}</option>
          @endforeach
      </select>
    </div>
     <div class="col-md-2">
      <select name="years_id" class="form-control" required="">
        @foreach($years as $year)
        @if($data->years_id==$year->id)
        <option value="{{$year->year}}" selected="">{{$year->year}}</option>
        @else
        <option value="{{$year->year}}">{{$year->year}}</option>
        @endif
          @endforeach
      </select>
    </div>
     <div class="col-md-2">
      <input type="date" name="date" class="form-control" required="" value="{{$data->date}}">
    </div>
     <div class="col-md-2">
      <input type="file" onchange="document.getElementById('photo').src = window.URL.createObjectURL(this.files[0])" name="image"><img id="photo" alt=" " width="100" height="100" src="{{asset('/images/bazar_images/'.$data->image)}}"/>
    </div>
  </div>
 </header>

 <input type="submit" name="" value="Update" class="btn btn-success">
 
</div>


<div class="panel panel-footer">
   <table class="table table-bordered">
     <thead>
         <tr>
       <th>Product Name</th>
       
       <th>Quantity</th>
       <th>Price</th>
       <th>Total</th>
       </tr>
     </thead>
     <tbody>
 @foreach($data->dailybazars as $bazar)
 <input type="hidden" name="id[]" value="{{$bazar->id}}">
       <tr>
         <td> <input type="text" name="product_name[]" value="{{$bazar->product_name}}"> </td>
        
         <td><input type="text" name="quantity[]" value="{{$bazar->quantity}}"> </td>
         <td><input type="text" name="price[]" value="{{$bazar->price}}"></td>
         <td> <input type="text" name="total[]" value="{{$bazar->total}}"></td>
       </tr>
@endforeach
     </tbody>
     <tfoot>
     </tfoot>
   </table>
</div>

{!!Form::close()!!}



</section>
</div>
 
@endsection 


controller

public function edit($id)
    {
        $data=Bazars::find($id);
        return view('admin.bazar.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data=Bazars::find($id);
         
        $data->shopkeepers_id=$request->shopkeepers_id;
        $data->months_id=$request->months_id;
        $data->years_id=$request->years_id;  
        $data->date=$request->date;  
        $data->comment=$request->comment;  
        $data->save();

         if(count($request->id) > 0) {
        foreach ($request->id as $item=>$v) {
        $datad=array(
          
          'product_name'=>$request->product_name[$item],
          'brand'=>$request->brand[$item],
          'quantity'=>$request->quantity[$item],
          'price'=>$request->price[$item],
          'total'=>$request->total[$item]
        );
        $dbazar=Dailybazars::where('id',$request->id[$item])->first();
        $dbazar->update($datad);
      }
      }

Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Bazars extends Model
{
    protected $fillable=['shopkeepers_id','months_id','years_id','image','date','comment'];

    public function dailybazars()
    {
        return $this->hasMany(Dailybazars::class);
    }
    
    public function years()
    {
        return $this->belongsTo(Years::class);
    }
     public function months()
    {
        return $this->belongsTo(Months::class);
    }
     public function shopkeepers()
    {
        return $this->belongsTo(Shopkeepers::class);
    }

}

Route

  Route::resource('bazars','BazarController');
 
 

Note: Laracollective  package need for HTML Form



1 comment:

Powered by Blogger.