1

I'm trying to create a set of rules under the new \HTTP\Requests\UpdateArticle class for the slug field, which needs to have unique filter applied, but only when the id is not equal the url parameter of the route name article/{article}.

What I got so far is:

public function rules()
{
    return [
        'title' => 'required|min:3',
        'excerpt' => 'required',
        'body' => 'required',
        'slug' => 'required|unique:articles,slug,?',
        'active' => 'required',
        'published_at' => 'required|date'
    ];

}

I need to replace the question mark at the end of the unique filter for slug field with the id from the url, but don't know how to obtain it.

Sebastian Sulinski
  • 5,815
  • 7
  • 39
  • 61

2 Answers2

2

To get URL parameters from the URL:

$this->route('id');

A great discussion on this were also asked here.

Community
  • 1
  • 1
doncadavona
  • 7,162
  • 9
  • 41
  • 54
1

You can retrieve route parameters by name with input():

$id = Route::input('id');
return [
    // ...
    'slug' => 'required|unique:articles,id,' . $id,
    // ...
];

It's right there in the docs (scroll down to Accessing A Route Parameter Value)

lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • you should share the docs link. Guys nowdays does not like to read: http://laravel.com/docs/5.0/routing – manix Feb 15 '15 at 20:46
  • Thanks - that seem to work fine. Also - is using Route facade inside of the Request class considered a good practice you think? – Sebastian Sulinski Feb 15 '15 at 21:05
  • Although I'm not sure I think what you actually want is this rule: `'required|unique:articles,slug,'.$id.'`. Regarding the facade. I'd rather use dependency injection for that. Make sure to type hint `Illuminate\Routing\Router` and not `Route` – lukasgeiter Feb 15 '15 at 21:09
  • Thanks - that's what I thought - and yes - it was a slug not id in the filter - just forgotten to update the ticket. – Sebastian Sulinski Feb 16 '15 at 19:06
  • As of Laravel 5.1, Class 'App\Http\Requests\Route' is not found or it may have been deprecated. And I can't see anyways to get URL parameters from the 5.1 documentation. How can we resolve this to get parameters from the URL? – doncadavona Jul 18 '15 at 15:37
  • @DonCadavona This class never existed. Just use the global alias.. `use Route;` – lukasgeiter Jul 18 '15 at 20:26
  • When I try to use Route::input('id'), it Laravel 5.1 returns this error: "Class 'App\Http\Requests\Route' is not found". How could that be? – doncadavona Jul 19 '15 at 04:18
  • @Don You have to **import** the class at the top with `use Request;` – lukasgeiter Jul 20 '15 at 04:51