Pass data from controller to view laravel

Rakesh Sharma      15 Comments  

Pass data from controller to view laravel 5 :-

Sometimes we need to pass data from controller to view in laravel 5 like working with database query, select option and more. it’s simple and easy with built in function in laravel 5. We can send data from controller to view easily with with() in laravel. Also there are more way to send or pass data to view from controller. I am describing some easy way how to pass data form controller to view in laravel 5.

1. Passing an array :-


$data = array(
    'name'  => 'Rakesh',
    'email' => 'sharmarakesh395@gmail.com'
);

return View::make('user')->with('data', $data);

//Accesing $data on view :-

{!!$data!!}
{!!$data['email']!!}

2. Working with query :- with with()


function view() {
  $q = Model::where('name', '=', 'Foo')->first(); 
  $users = Model::order_by('list_order', 'ASC')->get();
  return $view->with('users', $users)->with('q', $q);
}
//Accesing on view :-
{!! $q->name !!}

with compact() :-


function view() {
  $q = Model::where('name', '=', 'Foo')->first(); 
  $users = Model::order_by('list_order', 'ASC')->get();
  return $view->with('users', compact('q','users'));
}

3. Passing select options :-


public function oplist(){
  $options="";
  for($i=1;$i<10;$i++) {
    $options.="<option value=".$i.">".$i."</option>";
  }
  return View::make('viewname')->with('options',$options);
}

//Accessing on view :-
<select>{!!$options!!}</select>

Above code works with view::make() now we will see pass data from controller to view on redirect. it’s also easy to pass data on redirect in laravel. Let’s see how to pass data from controller on redirect.

1. Redirect with flash data :-


return Redirect::to('user/login')->with('message', 'Login Failed');

2. Redirect to named route with data :-


return Redirect::route('profile', array('user' => 1));

For More information follow official Documentation Views & Responses Laravel

  • johnny

    I got this issue when I get data variable from community blade
    “Undefined variable: data (View: /opt/bitnami/apache2/htdocs/app/views/layouts/community.blade.php)”
    Do you know why? Thanks.

    • cause of $data is not found on view you need to send data like

      $data = array(
      ‘name’ => ‘Rakesh’,
      ‘email’ => ‘sharmarakesh395@gmail.com’
      );
      return View::make(‘layouts.community’)->with($data);Then print {{$data[‘name’]}} and {{$data[‘email’]}}

      • johnny

        Thank you so much. Appreciate.

  • nimmy

    i am using this code for get email in blade.php page.

    <input type="text" name="email" value="”>
    but error occured.

    syntax error, unexpected ‘}’ this is the error

    • rakesh sharma

      Just use

      • nimmy

        but dont get the values?

        • rakesh sharma

          You are not sending any values on view, just response return pls check and follow article properly to send data on view.

          • nimmy

            i am use this code in controller
            public function match_profile()
            {

            $data=Input::all();
            $email=$data[‘email’];
            $query = DB::table(‘user_reg’)->where([‘email’ => $email])->get();
            return view(‘search’,array(‘data’=>$query));

            if($query)
            {
            echo 1;

            }
            else
            {
            echo 0;
            }
            }

            and view has look like this

            Name:username; ?>
            Email:email; ?>

            but dont work the this code ” return view(‘search’,array(‘data’=>$query));” in laravel 5….

          • rakesh sharma

            Try return View::make(‘search’)->with(‘data’,$query); Then on view try print_r($data); you will get an array of elements

          • nimmy

            thnk you

  • Rhea Saade

    I’ve tried to use the first one(passing an array) but i always get this error:undefined variable data…

    this is my routes:

    Route::get(‘portfolio’, function (){
    $data = array(
    ‘name’ => ‘Rakesh’,
    ‘email’ => ‘sharmarakesh395@gmail.com’
    );
    return view(‘portfolio’)->with($data);
    });

    and I put this in my view: {{$data[‘email’]}}

    can you please tell me whats wrong?

    • Hi, Updated just try return View::make(‘user’)->with(‘data’, $data); Thanks

  • Andrew Sychra

    Thanks Rakesh, for whatever reason I am unable to pass arrays as an argument without using with(). Got some results with your tutorial here.

  • If I have a single variable, is it possible to pass into view directly without wrapping in array??

    • Abid Ali

      ->with(‘your_variable_name’,$your_variable_value);