6

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Samuel Henry
  • 153
  • 1
  • 3
  • 10

4 Answers4

9

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 !

Leena Patel
  • 2,423
  • 1
  • 14
  • 28
1

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')) }}
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
1

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.

Gurpal singh
  • 1,511
  • 1
  • 14
  • 27
-1

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.

Niket Joshi
  • 739
  • 5
  • 23
  • @SamuelHenry you pass this in your controller file from where you want to complete process and redirect to URL and then after it's not working then okay i will send you another example if i have any. sorry for this inconvenience. – Niket Joshi Sep 08 '18 at 04:08