Support the ongoing development of Laravel.io →
Requests Session
Last updated 2 years ago.
0
Solution

this is a different approach but a little better in my opinion

Route::bind('project', function($value, $route)
{
    return Project::where('slug', '=', $value)->first();
});

Route::get('project/{project}', 'ProjectController@show');


class ProjectController extends Controller {

    public function show($project)
    {
          $project; // Project model
    }

}
Last updated 2 years ago.
0

zenry can you elaborate on your solution? I'm not following it completely :S

Last updated 2 years ago.
0

I think what zenry is saying, that for exampe if you have

Route::get('project/{project_id}', 'ProjectController@show');

then in you controller you could access project_id by passing variable to the controller like this

class ProjectController extends Controller {

    public function show($project)
    {
          $project; // Project model
    }

}

However, if you would add somewhere

Route::bind('project_id', function($value, $route)
{
    return Project::where('slug', '=', $value)->first();
});

instean of passing project_id, laravel would pass project object into your controller action.

Last updated 2 years ago.
0

What if I have a post request, like

Route::post('project', 'ProjectController@show');

and I have project_id in $_POST?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.