Home > Software engineering >  data is not passing from view to controller in laravel
data is not passing from view to controller in laravel

Time:01-18

While I click delete button to remove a data from laravel blade datatable it passes data with url but in controller it shows empty data.

Delete button in laravel blade

<button  data-toggle="modal" data-target="#destroyModal{{ $data->id }}">Delete</button>

delete modal

<div  id="destroyModal{{ $data->id }}" tabindex="-1" role="dialog" aria-labelledby="destroyModal{{ $data->id }}" aria-hidden="true">
    <div  role="document">
        <div >                                                        
            <div >
                <form action="{{ route('cat_wise_ingre_measurements.destroy', $data->id) }}" method="POST" >                                        
                    @csrf
                    @method('DELETE')
                    <div >
                        <p>Do you really want to delete Ingredient {{ $data->product_categories_name }}?</p> 
                    </div>    
                    <div >
                        <button type="submit"  data-dismiss="modal">Cancel</button>
                        <button type="submit" >Delete</button>
                    </div>                    
                </form>                                
            </div>
        </div>
    </div>
</div>

web.php

Route::resource('cat_wise_ingre_measurements', 'CategoryWiseIngredientMeasurementController')->middleware('auth');

controller destroy function

public function destroy(CategoryWiseIngredientMeasurement $categoryWiseIngredientMeasurement)
    {
        return $categoryWiseIngredientMeasurement;
    }

In return $categoryWiseIngredientMeasurement; it return empty data.

I can't find where is the problem? Anybody help please?

CodePudding user response:

Since your resource is called cat_wise_ingre_measurements Laravel route model binding expect a variable called like your resource but in singular.

In your case Laravel expect $cat_wise_ingre_measurement or $catWiseIngreMeasurement (Both will works)

If you want to have $categoryWiseIngredientMeasurement as variable name, you need to specify what variable name Laravel should look for on the resource:

Route::resource('cat_wise_ingre_measurements', '')
//->parameters(['cat_wise_ingre_measurements' => 'categoryWiseIngredientMeasurement'])
->parameter('cat_wise_ingre_measurements','categoryWiseIngredientMeasurement')

Note: paramters() can be used when you have nested resources

CodePudding user response:

This is not the correct way to send $data->id in URL parameter the better approach then this is to send hidden input in the form
id}}" hidden> you can send data to controller in this way easily im 100% sure this will work

  •  Tags:  
  • Related