【问题标题】:Laravel 5.2: Data passed to the view not workingLaravel 5.2:传递给视图的数据不起作用
【发布时间】:2016-07-28 14:59:12
【问题描述】:

所以在我正在进行的这个 Laravel 项目中发生了一件非常奇怪的事情。 我将一个带有数据的数组传递给我的刀片视图,但是在视图中变量显示为空!

这只发生在实时服务器上,在我当地的宅基地流浪盒子上,一切正常。我不知道为什么会这样,而且似乎无法弄清楚。

我的控制器:

public function index()
{
    $this->view_data["representatives"] = array("Bruno", "Test", "Hey");
    return view("account.index")->with($this->view_data);
}

我的刀片视图:

@forelse($representatives as $representative)
    <p>{{ $representative }}</p>
@empty
    <p>There are no users yet!</p>
@endforelse

即使我这样做

print_r($representatives)

当我在本地正确获取数组时,它显示为空。

所以就像我说的,奇怪的是,在本地,对于每个工作都没有问题并打印名称,效果很好。

在服务器上时,它不起作用。

有趣的是,当在本地转储变量 $representatives 的内容时,我得到的数组没有问题,但在服务器上它显示好像数组是 empty .

甚至不是找不到变量。就像变量的内容被完全擦除一样。

有什么想法吗?我整个早上都在做这个,到目前为止还没有运气。 再次感谢!

【问题讨论】:

  • 可能无法修复它,但如果你说$representatives as $representative 然后输出$representative 会更具可读性
  • 是的!那是我的复制和粘贴失败。忘记前面的,如果我做一个 print_r($representatives) 它显示为空。
  • 尝试dd() 会话以查看您传递给视图的内容。可能有错字什么的。
  • dd(session()-&gt;all()) 应该可以解决问题
  • 您确定您的代码与上面显示的完全相同,并且您之前在此文件中没有任何其他内容,并且您将其仅用于测试吗?请确保您使用的是最新视图。使用php artisan view:clear 清除当前视图

标签: php laravel laravel-5.2 blade centos6


【解决方案1】:

使用这个

@forelse($representatives as $representative)
    <p>{{ $representative }}</p>
@empty
    <p>There are no users yet!</p>
@endforelse

试试这个

public function index()
{
    $this->view_data["representatives"] = array("Bruno", "Test", "Hey");
    return view("account.index")->with("representatives",$this->view_data);
}

试试这个

public function index()
{
    $this->view_data["representatives"] = array("Bruno", "Test", "Hey");
    return view("account.index",$this->view_data);
}

【讨论】:

  • 这有什么不同?
  • 你在 forelse 循环之前和之后使用了相同的变量。
  • PHP 将迭代数组的副本,因此在迭代时覆盖原始副本并不重要。除非这是版本差异的事情。
  • 对不起,我的复制和粘贴很糟糕。我已经有这样的了。就像我说的,不是语法问题。将变量发送到刀片视图时出现问题。忘记前面的,如果我做一个 print_r($representatives) 它显示为空。
  • 还是不行!我不知道为什么会这样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多