Laravel 5 在商店后重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30742074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:45:00  来源:igfitidea点击:

Laravel 5 redirect after store

phplaravelredirectlaravel-5laravel-routing

提问by Tartar

How to redirect a route after store process at L5 ?

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

回答by Marcin Nabia?ek

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

回答by Jey Yuu

I did it this way:

我是这样做的:

My route:

我的路线:

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

PortalController: (Store Function)

PortalController:(存储功能)

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

希望能帮助到你。