Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel update request validation how to get the id

Tags:

laravel

I am using Laravel 5.2 and I have managed to set correctly my update request for my users but when I do exactly the same for another table (clusters), I cannot get the id in the clusterrequest validation.

Here is what I have for my clusters: routes:

Route::post('clusterFormUpdate/{id}', ['uses'=>'ClusterController@postFormUpdate','middleware' => ['permission:user-edit']]);

Controller:

public function postFormUpdate(ClusterUpdateRequest $request, $id)
{
    $inputs = $request->all();

ClusterUpdateRequest:

...
public function rules()
{

    $id = $this->id;
    dd($id);

    return [
        'name'      => 'required|max:255|unique:clusters,name,' . $id . ',id',
        'countries' => 'required',
    ];
}

When I run this, I get null.

If I try the same for my user table, I get the id that is printed correctly so I was wondering where I can look for this problem?

Thanks.

like image 333
Richard Avatar asked Dec 10 '22 10:12

Richard


1 Answers

To get the id in the Request you can use the route() method:

public function rules()
{
    $id = $this->route('n'); //or whatever it is named in the route
    
    return [ 
        'name'      => 'required|max:255|unique:clusters,name,' . $id . ',id',
        'countries' => 'required',
    ];
}
like image 144
Rwd Avatar answered Feb 17 '23 18:02

Rwd