Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access resource id from route in Laravel 5 delete request?

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?

like image 676
Rohan Avatar asked Jul 02 '15 10:07

Rohan


People also ask

How do I delete a route in Laravel?

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.

How to define a route Laravel?

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.

How do I delete a post in Laravel?

Second Method: The second way is to delete using the Laravel delete Function and User Model (Easy one). ->name( 'users. destroy' );


1 Answers

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.

like image 171
Alex Kyriakidis Avatar answered Oct 21 '22 08:10

Alex Kyriakidis