dongrao1862 2016-08-03 17:12
浏览 281

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

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条回答 默认 最新

  • dongqiyou0303 2016-08-03 17:18
    关注

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

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)