“未定义的常数/变量"; Laravel中的错误......无法在刀片中显示变量 [英] "Undefined Constant/Variable" Error in Laravel......Unable to show variable in blade

查看:77
本文介绍了“未定义的常数/变量"; Laravel中的错误......无法在刀片中显示变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel,我的仪表板控制器(扩展了基本控制器)中有一个变量,定义如下:

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

仪表板控制器:

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

通过使用{{}}语法,此变量​​显示在我的main(index/default/home/无论您用什么名称)html模板中:

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

HTML模板

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

它完全显示了我期望的结果. 但是我想在我正在使用的布局刀片中显示相同的数据...我在{.dashboardData.stats.recieved}}中在layout.blade(目录:/app/views)中使用了相同的html结构,但是我收到"未定义常量"和"未定义变量"错误...

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...

以下是我的route.php的一瞥:-

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

我对laravel还是很陌生,我偶然发现了"view :: share"和"view:make"这两个东西,但并没有完全了解如何完美地做到这一点&安全地没有一个错误... 我还在"/app/libraries/initiation.php"中有一个库设置,该库设置将一些数据反映到layout.blade....如何在layout.blade中显示数据{{dashboardData.stats.recieved}}没有错误"未定义常量"和"未定义变量" ... ???以及这样做的最正确,实用和最重要的安全方法是什么???

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...???

推荐答案

您在 Blade 模板中以错误的方式访问变量.

You access variables in incorrect way in your Blade template.

替换

{{dashboardData.stats.recieved}}

使用

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

这篇关于“未定义的常数/变量"; Laravel中的错误......无法在刀片中显示变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆