Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 434
Samuel Henry Avatar asked Sep 07 '18 08:09

Samuel Henry


People also ask

How do I redirect to a specific page in Laravel?

Redirection with the Flashed Session Data in Laravel framework. 1. Redirection to the URL within Laravel. return redirect ('user/dashboard'); 2. Redirection back to the previous page within Laravel. return redirect ()->back (); OR. return redirect ()->back ()->withInput ();

How to pass data to a view in Laravel?

Laravel provides different ways to pass data to a view. We can pass data directly from routes or through the controller. Here are some of the ways we can pass data to the view: 1. Using view (): We can directly pass the data in the ‘ view () ’ helper function by using the second parameter in the function which takes an array as key and value pair.

How to pass dynamic parameters through route in Laravel?

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. Show activity on this post. The variable name (user_detail) should be same as the name in compact. Right syntax for laravel 5.4 and heigher versions.

What is the Laravel framework?

Being open-sourced itself, the laravel framework allows third-party programs to be integrated into their command lines. This is also the reason why developers prefer to use the laravel framework to create vast systems. Let us have a look a few examples to understand how does the laravel redirect to URL work:


2 Answers

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 !

like image 137
Leena Patel Avatar answered Nov 02 '22 22:11

Leena Patel


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')) }}
like image 41
Zakaria Acharki Avatar answered Nov 03 '22 00:11

Zakaria Acharki