简体   繁体   English

Laravel中出现“未定义的常数/变量”错误……无法在刀片中显示变量

[英]“Undefined Constant/Variable” Error in Laravel…Unable to show variable in blade

I am using laravel, I have a variable in my dashboard controller (which extends the base controller) defined as follows: 我正在使用laravel,我在仪表板控制器(扩展了基本控制器)中有一个变量,定义如下:

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: 通过使用{{}}语法,此变量​​显示在我的main(index / default / home /无论您如何称呼)html模板中:

HTML Template HTML模板

   <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... 它完全显示了我期望的结果...。但是我想在我使用的布局刀片中显示相同的数据...我在layout.blade中使用相同的html结构(目录:/ app / views )和{{dashboardData.stats.recieved}},但出现“ 未定义常量 ”和“ 未定义变量 ”错误...

following is a glimpse of my routes.php:- 以下是我的routes.php概览:-

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 "...??? 我对laravel还是很陌生,我偶然发现了“ view :: share”和“ view:make”的东西,但是却没有完全正确,安全地完成操作而没有一个错误……我也有一个“ /app/libraries/initiation.php”中的库设置,将一些数据反映到layout.blade ....如何在没有错误的情况下在layout.blade中显示数据{{dashboardData.stats.recieved}} 未定义常量 ”和“ 未定义变量 ” ... ??? and also what would be the most correct, practical and most importantly secure way of doing so...??? 以及这样做的最正确,实用和最重要的安全方法是什么?

You access variables in incorrect way in your Blade template. 您在Blade模板中以错误的方式访问变量。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM