Kanchan186's avatar

ErrorException (E_WARNING) Invalid argument supplied for foreach()

error is occurred because it calls store function instead of update function, cant understand why it happens.

link to edit

<a href="{{url('/')}}/mechanic_brands/{{$br->mech_brand_id}}/edit"><i class="fas fa-edit"></i></a>

web.php

//add Mechanic Brands
Route::get('mechanic_brands/{mechanic_id}/Add','MechBrandController@Add');
//save Mechanic_Brands
Route::post('mechanic_brands/{mechanic_id}','MechBrandController@store');

//edit Mechanic Brands
Route::get('mechanic_brands/{mech_brand_id}/edit','MechBrandController@edit');

Route::post('mechanic_brands/{mech_brand_id}','MechBrandController@update');

//delete Mechanic Brands
Route::get('mechanic_brands/{mech_brand}','MechBrandController@destroy');
//get segment wise brand using ajax
Route::get('brand/Ajax/{segment}','MechBrandController@brandAjax');

edit view

 <form method="post" action="{{url('/')}}/mechanic_brands/{{$mech_brand->mech_brand_id}}" enctype="multipart/form-data" novalidate>
                                    {{csrf_field()}}

                                
                                    <div class="form-group">
                                        <label for="inputPassword3" class="col-sm-3 control-label">Segment Name 
                                        <span class="required"> * </span></label>
                                        <div class="col-sm-8">
                                        <select class="form-control" name="segment_id" required data-validation-required-message="This field is required" onchange="getBrand(this.value)" >
                                            @foreach($segment as $br)
                                            
                                                @if($br->segment_id == $mech_brand->segment_id )
                                                     <option value="{{$br->segment_id}}" selected >{{$br->segment_name}}</option>
                                                @else
                                                     <option value="{{$br->segment_id}}">{{$br->segment_name}}</option>
                                                @endif

                                            @endforeach
                                        </select>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label for="inputPassword3" class="col-sm-3 control-label">Brand Name 
                                        <span class="required"> * </span></label>
                                        <div class="col-sm-8">
                                        <select class="form-control" name="brand_id" required data-validation-required-message="This field is required">
                                            @foreach($brand as $br)
                                            
                                                @if($br->brand_id == $mech_brand->brand_id )
                                                     <option value="{{$br->brand_id}}" selected >{{$br->brand_name}}</option>
                                                @else
                                                     <option value="{{$br->brand_id}}">{{$br->brand_name}}</option>
                                                @endif

                                            @endforeach
                                        </select>
                                        </div>
                                    </div>
                              
                                               
                                            <div class="text-xs-right">
                                                <button type="submit" class="btn btn-info">Submit</button>
                                                <button type="reset" class="btn btn-inverse">Cancel</button>
                                            </div>
                                </form>

controller


public function edit(MechBrand $mech_brand_id)
  {
    //dd('hi');
    //dd($mech_brand_id);
    $mech_brand=MechBrand::where('mech_brands.mech_brand_id',$mech_brand_id->mech_brand_id)
              
              ->join('segments','mech_brands.segment_id','=','segments.segment_id')
              ->join('brands','mech_brands.brand_id','=','brands.brand_id')
              
              ->select('segment_name','brand_name', 'mech_brands.*')->first();
   
    $brand=Brands::get();
    $segment=Segment::get();
    
    //dd($statearea);
    return view('backend.mechanic_brands.editMechBrands',compact('mech_brand','segment','brand'));
  }


 public function update(Request $req,MechBrand $mech_brand_id)
  {
   // dd($mech_brand_id);
    //dd($req->all());

    $mechanic_id=request('mechanic_id');
    $segment_id=request('segment_id');
    $brand_id=request('brand_id');
        
    
    $mech_brand_id->update($req->all());

   
     return redirect('mechanic/view');
  }
0 likes
6 replies
tykus's avatar

The typical HTTP method for an update is PUT, you are using POST so there is nothing to differentiate the store and update endpoints; they have the same signature

//save Mechanic_Brands
Route::post('mechanic_brands/{mechanic_id}','MechBrandController@store');

Route::post('mechanic_brands/{mech_brand_id}','MechBrandController@update');
Kanchan186's avatar

sorry actually i used this

//edit Mechanic Brands
Route::get('mechanic_brands/{mech_brand_id}/edit','MechBrandController@edit');

Route::patch('mechanic_brands/{mech_brand_id}','MechBrandController@update');

still it calls store function

tykus's avatar

There is no _method field in the form, add a

@method('patch')
Kanchan186's avatar

@tykus can u please more explain about-@method('patch')

actually i cant understand where i need to update my code

tykus's avatar
tykus
Best Answer
Level 104

Laravel uses method spoofing to handle PUT PATCH and DELETE methods because of a limitation in HTML forms. You add the @method('PATCH') directive inside the form so Laravel's routing layer knows how to handle the incoming request.

 <form method="post"
    action="{{url('/')}}/mechanic_brands/{{$mech_brand->mech_brand_id}}"
    enctype="multipart/form-data" 
    novalidate
>
    {{csrf_field()}}
    @method('PATCH')
    // and the rest of your form

This directive is equivalent to:

{{ method_field('PATCH') }}

and both will echo onto the page the following markup:

<input type="hidden" name="_method" value="PATCH">

Please or to participate in this conversation.