【问题标题】:Cannot display the variable in the blade template from the controller无法从控制器显示刀片模板中的变量
【发布时间】:2017-08-02 20:37:01
【问题描述】:

我不知道如何在 laravel 的刀片模板中显示路线名称。请在下面找到示例代码。谢谢。

来自控制器 (StaffsController.php)

public function index()
{
    $thisRoute = Route::current()->uri();
    return view('staff.list')>with(compact('thisRoute'));
}

刀片:

{{ $thisRoute }}

这是 var_dump

/home/vagrant/Code/spark/app/Http/Controllers/StaffsController.php:20:string 'staffs' (length=6)

错误:

(1/1) UnexpectedValueException
The Response content must be a string or object implementing __toString(), "boolean" given.

当我将控制器中的代码更改为:

public function index()
{
    $thisRoute = Route::current()->uri();
    return dd($thisRoute);

}

我得到“staffs”作为正确的输出,它是来自转储的字符串,对吧?

【问题讨论】:

  • 您希望打印什么?
  • 为什么在dd 中使用Route::current()->uri() 而在第一个示例中只使用Route::current();
  • @u_mulder,对不起,这是一个错字。我更正了代码。
  • @Laerte 我想在刀片中将五线谱输出为字符串

标签: php laravel laravel-blade


【解决方案1】:

将您的退货声明更改为:

return view('staff.list', compact('thisRoute')); 

如果你使用的是 laravel 5.3 或更高版本,你可以得到这样的路由名称:

Route::currentRouteName();

有了这个,你真的不必将它从控制器传递到视图,只需直接从刀片视图中使用它:

{{ Route::currentRouteName() }}

【讨论】:

  • 对不起伙计,将代码更改为$thisRoute = Route::currentRouteName(); return view('staff.list')>with(compact('thisRoute'));,但仍然遇到同样的错误
  • 请像这样更改您的退货声明:return view('staff.list', compact('thisRoute'));
【解决方案2】:

对不起,我解决了这个问题。我错过了- 排队 return view('staff.list')>with(compact('thisRoute'));

改成

return view('staff.list')->with(compact('thisRoute'));

我错了。

谢谢

【讨论】:

  • 你也可以使用短语法:return view('staff.list', compact('thisRoute'));
猜你喜欢
  • 2020-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 2023-03-26
  • 2014-12-08
  • 2016-07-13
  • 2016-10-01
相关资源
最近更新 更多