Laravel 5 redirect after store

14k views Asked by At

How to redirect a route after store process at L5 ?

My route is

Route::get('/pages/aracislemler/{id}', ['middleware' => ['roles'], 'uses' => 'PagesController@aracislemler', 'roles' => ['Admin']]);

And controller function

public function store()
{

        $brand_name = Request::get('brand_id');
        $type_id = Request::get('type_id');
        //$client_id = Request::get('representive_client_id');

        if (!Brand::find($brand_name)) {
            $brand = new Brand;
            $brand -> brand_name = $brand_name;
            $brand -> type_id = $type_id;
            $brand -> save();

            $last_brand_id = $brand -> id;

            $input = Request::except('brand_id');
            Vehicle::create($input);

            $vehicle = Vehicle::orderBy('created_at', 'desc')->first();
            $vehicle -> update(array('brand_id' => $last_brand_id));

        }else{         
            $input = Request::all();
            Vehicle::create($input);
        }

        return  redirect('pages/aracislemler');
}
2

There are 2 answers

0
Marcin Nabiałek On BEST ANSWER

If you want to redirect to record that was created/updated , you should change:

Vehicle::create($input);

into:

$vehicle = Vehicle::create($input);

and now:

return redirect('pages/aracislemler');

into

return redirect('pages/aracislemler/'.$vehicle->id);
0
Jey Yuu On

I did it this way:

My route:

Route::resource('portal', 'PortalController');
Route::resource('show', 'PortalController');

PortalController: (Store Function)

$portalNewID = $portal->id; //I retrieved the last ID
return redirect('portal/'.$portalNewID); //This will redirect to the latest page you just created

Hope it helps.