I am trying to authorize a delete request on a resource if the resource belongs to a user for which I have created a Delete Request in Laravel 5.
For another resource I could do something like:
public function authorize()
{
if(Pivot::findOrFail($this->route('pivots'))->user_id != Auth::user()->id){
return false;
}
return true;
}
So basically $this->route('pivots')
would return the id of the pivot that the user is trying to delete and I would check if it belongs to the current user.
But now I am trying for another resource similar to this one:
public function authorize()
{
if(CropSection::findOrFail($this->route('crop-sections'))->pivot->user_id != Auth::user()->id){
return false;
}
return true;
}
I tried to die and dump $this->route('crop-sections')
and it comes out to be null but the request was http://localhost:8000/crop-sections/10
which has the id as 10.
What am I doing wrong?
app/Http/routes.phpRoute::get('delete-records','StudDeleteController@index'); Route::get('delete/{id}','StudDeleteController@destroy'); Step 6 −The output will appear as shown in the following image. Step 7 − Click on delete link to delete that record from database.
All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider . The routes/web.php file defines routes that are for your web interface.
Second Method: The second way is to delete using the Laravel delete Function and User Model (Easy one). ->name( 'users. destroy' );
You have to change 'crop-sections' to 'crop_sections'.
Route parameters cannot contain the - character. Use an underscore (_) instead.
You can find more info here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With