Laravel show data from key column not from id

- 1 answer

Ad

I have this route:

Route::resource('articles', 'ArticlesController');
Route::get('articles/aukcija/{key}', '[email protected]');

and I have this function in Controller:

public function show($id)
    {
        $article = Auth::user()->articles()->findOrFail($id);

        return view('articles.show', compact('article'));
    }

    public function aukcija($key)
    {
        $article = Article::findOrFail($key);

        return view('articles.show', compact('article'));
    }

I need both of them... but how I can get Article with token stored in key column instead ID...

so when I write localhost:8888/article/1 and localhost:8888/article/aukcija/f4576ceusyfc674wr873cr48c7sefc to get the same article becouse article with ID=1 have key=f4576ceusyfc674wr873cr48c7sefc...

Ad

Answer

Ad

You could try this:

public function aukcija($key)
{
    $article = Article::where('key', $key)->firstOrFail();

    // or
    $article = Article::where(compact('key'))->firstOrFail();

    return view('articles.show', compact('article'));
}
Ad
source: stackoverflow.com
Ad