0

I am using laravel, I have a variable in my dashboard controller (which extends the base controller) defined as follows:

Dashboard Controller:

public function dashboardData(){
    $toReturn = array();
    $toReturn['siteTitle'] = $this->data['panelInit']->settingsArray['siteTitle'];


    //------other things-----------//


    $toReturn['stats'] = array();

    //------some other things-----------//


    $toReturn['stats']['recieved'] = messagesList::where('userId',$this->data['users']->id)->where('messageStatus',1)->count();

This variable is shown in my main(index/default/home/whatever you call it) html template by using the {{}} syntax as follows:

HTML Template

   <div class="counter">   
   <div class="informational">{{dashboardData.stats.recieved}}</div>
   </div>

and it completely shows the result I expect.... but I want to show this same data in a layout blade that I am using... I am using the same html structure in the layout.blade (directory:/app/views) with {{dashboardData.stats.recieved}} but I am getting "Undefined Constant" and "undefined Variable" errors...

following is a glimpse of my routes.php:-

routes.php

Route::group(array('prefix'=>'/','before'=>'auth.Ui|auth.token|api.csrf'),function(){
Route::get('/','DashboardController@index');

Route::get('/dashboard','DashboardController@dashboardData');
Route::get('/dashboard/baseUser','DashboardController@baseUser');

//-------------some things-------------//

Route::post('/dashaboard','DashboardController@dashaboardData');

I am quite new on laravel, I have stumbled upon the "view::share" and "view:make" thing but didn't quite get it how to do it perfectly & securely without a single error... I also have a library setup in the "/app/libraries/initiation.php" that reflects some data to layout.blade....How do I show the data {{dashboardData.stats.recieved}} in the layout.blade without the errors "Undefined Constant" and "undefined Variable"...??? and also what would be the most correct, practical and most importantly secure way of doing so...???

1
  • Are you using angular or another JS framework? The syntax for {{dashboardData.stats.recieved}} is wrong for blade and should not 'completely shows the result I expect'. Commented Aug 3, 2016 at 17:23

1 Answer 1

4

You access variables in incorrect way in your Blade template.

Replace

{{dashboardData.stats.recieved}}

with

{{$dashboardData['stats']['recieved']}}

Make sure that you pass the variable to the view from your controller:

public function dashboardData() {
  // your logic that generates $toReturn array

  return view('your_view')->with('dashboardData', $toReturn);
}
Sign up to request clarification or add additional context in comments.

8 Comments

What I really don't get is why "it completely shows the result I expect" in OP's non-blade code... maybe it's another templating engine.
@jedrzej.kurylo It returns "Undefined variable: dashboardData"
Have you passed that variable to your blade template?
@jedrzej.kurylo I am not sure whether to use "view::make" or "view::share" or "@include" for it so that it works correctly........
Do you want to access that variable with PHP or JavaScript? It's a huge difference
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.