我这里不需要什么支持
我正在使用laravel 5.6。我想用View Composer填充博客的侧边栏。它从sidebarComposer文件给我错误。我不知道我在做什么错。请注意,我已经在config-> app.php文件中注册了此提供程序。错误是
错误:传递给with()的参数2必须是可调用的或为null,给定数组, 呼入 C:\ xampp \ htdocs \ LaraSite2 \ app \ Http \ Composers \ SidebarComposer.php在 第13行
<?php
namespace App\Http\Composers;
use App\Post;
use App\User;
use Illuminate\Contracts\View\View;
class SidebarComposer {
public function compose(View $view){
$view-with('posts', Post::all());//This line is giving error
}
}
class SidebarComposerProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register services.
*
* @return void
*/
public function register()
{
//Register composeSidebar funtion
$this->composeSidebar();
}
public function composeSidebar(){
view()->composer('layouts.aside','App\Http\Composers\SidebarComposer');
}
}
@foreach($posts as $post)
<div class="blog-posts"><a href="#">
<div class="item d-flex align-items-center">
<div class="image"><img src="img/small-thumbnail-1.jpg" alt="..." class="img-fluid"></div>
<div class="title"><strong>{{$post->title}}}</strong>
<div class="d-flex align-items-center">
<div class="views"><i class="icon-eye"></i> 500</div>
<div class="comments"><i class="icon-comment"></i>12</div>
</div>
</div>
</div></a>
</div>
@endforeach
答案 0 :(得分:0)
您的代码SidebarComposer出现语法错误
$view-with('posts', Post::all());
使用语法
$view->with('posts', Post::all());