Rohan
Rohan

Reputation: 13791

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?

Upvotes: 9

Views: 884

Answers (1)

Alex Kyriakidis
Alex Kyriakidis

Reputation: 2881

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.

Upvotes: 4

Related Questions