Question

I have a controller that uses a layout

class MainContentController extends BaseController {

  protected $layout = "desktop.contacts.main_contacts";

  public function index()
  {
     $persons = Person::with("contact.addresses", "companies")->has("companies", "=", "0")->get();
     $companies = Company::with("persons", "contact.addresses")->get();
     $data["persons"] = $persons;
     $data["companies"] = $companies;
     $this->layout->with($data);
  }
}

Now if I for example make a route Route::get('test', 'MainContentController@index'); I get the proper results of the view main_contacts generated by the controller.

But I want to nest this result inside my master view.
I tried to instantiate MainContentController and pass the result of the index() method as data to the view, but it says

Call to a member function with() on a non-object

for

$this->layout->with($data);

My question is: how can I pass the result of the layout controller into my master view. Can I nest it? Do I need to get the rendered html somehow and pass it as data to the master where I echo it?

I want this separation because I want to load the content later dynamically via Ajax based on search queries.

Thanks for your help and suggestions!

Was it helpful?

Solution 2

What if you moved your search function into the MainController and then

return View::make('desktop.contact.main_contacts")->with('data', $data);

Then you could make an AJAX request to

Route::get('/search/{terms}', ['uses' => 'MainController@search'); 

and return that whole page as a response

Then just do this as a callback to your AJAX :

function(resp) { 
    $('#contacts').html(resp); 
}

where $('#contacts') refers to a div#contacts in your 'desktop.contacts.index'.

OTHER TIPS

You may try this

class MainContentController extends BaseController {

    // Set the master layout here
    protected $layout = 'layouts.master';

    public function index()
    {
        $persons = Person::with("contact.addresses", "companies")->has("companies", "=", "0")->get();
        $companies = Company::with("persons", "contact.addresses")->get();
        $data = array("persons" => $persons, "companies" => $companies);

        // Set the content to master layout
        $this->layout->content = View::make('desktop.contacts.main_contacts', $data);
    }
}

Check the manual, it's all there.

Update: In your master layout you should have following code to render the contend:

@yield('content')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top