MonkeyBusiness
MonkeyBusiness

Reputation: 593

Laravel show data from key column not from id

I have this route:

Route::resource('articles', 'ArticlesController');
Route::get('articles/aukcija/{key}', 'ArticlesController@aukcija');

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...

Upvotes: 0

Views: 449

Answers (1)

pespantelis
pespantelis

Reputation: 15382

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'));
}

Upvotes: 2

Related Questions