简体   繁体   中英

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('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.

Anyway you could use Redirect::route() like :

return Redirect::route('Merchant view',['merchant' => base64_encode($merchant)]);

And get the passed variable in the blade side as a HTTP parameter using :

{{ 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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