Mahendra Jella
Mahendra Jella

Reputation: 5596

How to use variable value in multiple views Laravel

controller::

$states=$this->States->get_States();
$data=array('states'=>$states);
echo  View::make('frontend.list-property')->with('data', $data);

list-property.blade.php:: // this is sub view

@extends('layouts.frontend.frontend_login') 

@section('content')
<select>
  {{$states_drpdown}} //when I use this statement I am getting error

 </select>
 @stop

frontend_login.blade.php //This is layout file

   <?php
    $states_drpdown='';
    foreach ($data['states'] as $state):
    $states_drpdown.='<option value="'.$state->sid.'">'.$state->statename.'</option>';
endforeach
   ?>
    <select>
     {{$states_drpdown}}  //I am getting list of options here
    </select>
    @yield('content')

I am getting following error, can any one help me out pls.

Symfony \ Component \ Debug \ Exception \ FatalErrorException

Method Illuminate\View\View::__toString() must not throw an exception

Upvotes: 0

Views: 158

Answers (2)

majidarif
majidarif

Reputation: 20105

You may also share a piece of data across all views:

View::share('name', 'Steve');

You can find more about this here.

In your case, try: (warning untested code)

$states = $this->States->get_States();
$data   = View::share('states', $states);
return View::make('frontend.list-property');

Upvotes: 2

Jono20201
Jono20201

Reputation: 3205

$data = array('states' => $this->States->get_States());
echo View::make('frontend.list-property', $data);

Assuming $this->States->get_States() returns a string of options, not an array?

<select>
    {{$states}}
</select>

If it $this->States->get_States() returns an array, you could just do this (no need for <select> tags):

{{ Input::select('name-of-field', $states, Input::old('name-of-field')) }}

Consider returning the view instead of echoing.

Upvotes: 1

Related Questions