Redirect with compact value in laravel

Samuel Henry picture Samuel Henry · Sep 7, 2018 · Viewed 11.5k times · Source

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?

Answer

Leena Patel picture Leena Patel · Sep 7, 2018

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 !