1

Allright,

Here is my index() method of UserController

public function index()
{

    $name = 'echoashu';

     return view('home', compact('name'));
}

As simple as that, and here my home.blade.php code

  <span class="info-box-number">{{$name}} </span>

This must work ideally as per documentation, but it returns undefined variable error

Undefined variable: name (View: C:\xampp\htdocs\laravel1\resources\views\home.blade.php)

Any guess??

tereško
  • 58,060
  • 25
  • 98
  • 150
echoashu
  • 912
  • 3
  • 14
  • 31
  • 1
    make sure you are calling the index function of UserController from your route – Khan Shahrukh Jun 06 '15 at 07:53
  • Ok, seems to work But, i dont want to change the route, that said i'm fine with `/home` route to `HomeController@index` and would like to avail Data from `UserController` [various methods], cant I do that?? – echoashu Jun 06 '15 at 08:00
  • 1
    you can do that but it is against the concept of Laravel/MVC if you want to share data in different views you can share it via AppServiceProvider's boot function or you can query the data in the base controller i.e Controller in Laravel 5 – Khan Shahrukh Jun 06 '15 at 08:03
  • well that idea for base controller make sense, i'll have to give that a try – echoashu Jun 06 '15 at 08:20

1 Answers1

5

Give a man a fish and you feed him for a day; Teach a man to fish and you feed him for a lifetime

Here's Let me say how to debug(fish) in this situation.

1st Step :

Make sure that your call is right

You can do it by

Route::get('yourcall', 'UserController@index');

Before passing it inside the view, Just print something inside your controller like

public function index()
{
echo 'Whao ! I landed correctly';
}

2st Step :

Make sure that you see what you call

Now make your return to the view, Make sure that your view exists and have the name with extension like yourview.blade.php

You can do it by

return view('yourview', compact($YourValue));

So, You should have a view named as yourview.blade.php

Inside the blade you can get the passed value like

{{$YourValue}}  // If you have your file name as yourview.blade.php

or

<?php
echo $YourValue // If you have your file name as yourview.php
?>
Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
  • understood, and that worked. However I am building a db based app, so would like to query data from different tables (Models) without changing the route. And since Controllers meant to query and retrive the data, how am i suppose to avail data from multiple tables to a view, say it is a dashboard page, and it needs data from multiple tables – echoashu Jun 06 '15 at 08:14
  • You mean, you want to have a global controller ? something like All in one function ? – Sulthan Allaudeen Jun 06 '15 at 08:21
  • 1
    you can call any Model class from any controller and that is how it goes. I suggest you to be more active on laravel IRC channel – Khan Shahrukh Jun 06 '15 at 08:22
  • 1
    @KhanShahrukh Well said, also [Laracast](https://laracasts.com/discuss) – Sulthan Allaudeen Jun 06 '15 at 08:25
  • @echoashu Its best practise to use one function for one purpose. As you can call any model function from any controller , you can considering having seperate calls. If you dump all your table in a single function, then it will result in weighted function leads to slow execution (which will create a bad impression of framework) – Sulthan Allaudeen Jun 06 '15 at 08:27
  • @SulthanAllaudeen no, not that I Just want to let the different controllers have the access to their respective models ( databse tables) for example i have `user` table `User` model and `UserController` as controller Now `UserController` should query the `User` model, but the results need to be made available to `HomeController` so that the respective view `home.blade.php` can show the data from `user` table ultimately – echoashu Jun 06 '15 at 08:27
  • so, something like calling `UserController` from `HomeCotroller` maybe like `$data = UserController::countUser('group_id');` – echoashu Jun 06 '15 at 08:29
  • Then you need to have a call from `HomeController` to `UserController` ! – Sulthan Allaudeen Jun 06 '15 at 08:30
  • @SulthanAllaudeen exactly that. You spoke my heart :-D – echoashu Jun 06 '15 at 08:31
  • 1
    Why not you prefer calling `User` model from `HomeController` It is not at all a bad practise, It's clean only :) – Sulthan Allaudeen Jun 06 '15 at 08:31
  • @echoashu You still wanted to know how you can do that ? – Sulthan Allaudeen Jun 06 '15 at 08:32
  • no, just wanted to have that flow clear, i'll write that code myself ;-) thanks – echoashu Jun 06 '15 at 08:33