简体   繁体   English

在laravel中重定向具有紧凑值

[英]Redirect with compact value in laravel

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? 还有其他最佳方法吗?

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 ! 希望它能帮到你!

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

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

You can pass value from controller to view by using compact the Exact syntax should be like this 您可以使用compact将值从控制器传递到视图,Exact语法应该是这样的

$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. 变量名称(user_detail)应与compact中的名称相同。 Right syntax for laravel 5.4 and heigher versions. laravel 5.4和更高版本的正确语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM