在 Laravel 中使用紧凑值重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52218545/
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
Redirect with compact value in laravel
提问by Samuel Henry
Route :
路线 :
Route::get('merchantTrans/{id}','MerchantController@merchant');
Merchant Controller :
商户控制器:
public function merchant($id){
$merchant = Merchant::whereId($id)->get();
return redirect('Merchant view')->with(compact('merchant'));
}
View Route :
查看路线:
Route::view('Merchant view','merchant.listview')->name('Merchant view');
I cannot pass merchant compact value to view.
我无法通过商家压缩值查看。
Produce error
产生错误
Undefined variable: merchant
未定义变量:商家
Any other best way?
还有其他最好的方法吗?
回答by Leena Patel
Try this
尝试这个
return redirect()->route('Merchant view')->with( ['merchant' => $merchant] );
In blade file :
在刀片文件中:
<?php $merchants = Session::get('merchant'); ?>
{{ @foreach ($merchants as $merchant)
//your code
@endforeach }}
Hope it helps you !
希望对你有帮助!
回答by Zakaria Acharki
The Route::view
is made for the static views with static parameters passed like :
将Route::view
被用于通过像静态参数静态的观点提出:
Route::view('Merchant view','merchant.listview', ['param1' => 'value1']);
Since you want to pass dynamic parameters then it will be better to use the regular route passing by the related controller action
and retrieving the desired data.
由于您想传递动态参数,因此最好使用相关控制器传递的常规路由action
并检索所需数据。
Anyway you could use Redirect::route()
like :
无论如何你可以使用Redirect::route()
像:
return Redirect::route('Merchant view',['merchant' => base64_encode($merchant)]);
And get the passed variable in the blade side as a HTTP parameter using :
并使用以下命令获取刀片端传递的变量作为 HTTP 参数:
{{ base64_decode(Request::get('merchant')) }}
回答by Gurpal singh
You can pass value from controller to view by using compact the Exact syntax should be like this
您可以使用compact将值从控制器传递给视图,确切的语法应该是这样的
$user_detail=array('field1'=>1,'field2'=>2);
return view('dashboard',compact('user_detail'));
The variable name(user_detail) should be same as the name in compact. Right syntax for laravel 5.4 and heigher versions.
变量名称(user_detail) 应该与compact 中的名称相同。laravel 5.4 和更高版本的正确语法。
回答by Niket Joshi
Hello you can use the following way to get compact data.
您好,您可以使用以下方式获取压缩数据。
return view('admin/users/userdetails', compact('transaction', 'title'));
OR
或者
return redirect('/userdetails', compact('transaction', 'title'));
i have use the following syntax in my project to compact data while redirect to other page.
我在我的项目中使用以下语法来压缩数据,同时重定向到其他页面。
thank you.
谢谢你。