我们可以将@yield 的值存储在变量中吗

Can we store value of @yield in a variable

我有一个简单的问题,我们可以将产生的值存储在变量中吗?

举个例子

$var = @yield('title')

如果没有,那么有什么办法可以得到这个yield

的值

是的,您可以检索由 section 定义的 yield 的值,例如:

// Assumed you've the following in your view: @section('title', 'Some Title')

$title = app()->view->getSections()['title']; // Some Title

基本上,app()->view->getSections() 将为您 return 所有部分的关联数组,因此,要获得特定的 section,您只需要从数组中获得特定的索引.从视图中,您可以使用全局 $app 变量访问 app,即:$app->view$app['view'].