Laravel 未定义变量

Laravel 未定义变量,laravel,laravel-5,laravel-5.2,Laravel,Laravel 5,Laravel 5.2,我现在有个问题,我无法在我的查看页面中显示我喜欢的内容。 这是我想在我的视图刀片上显示的行(比如_计数器),但一旦我调用它,就会得到未定义的变量,比如_计数器 public function getLikeCounter($post_id) { $post = Post::find($post_id); $number = null; $like_counter = \DB::table('likes')->where('post_id', $post-&g

我现在有个问题,我无法在我的查看页面中显示我喜欢的内容。 这是我想在我的视图刀片上显示的行(比如_计数器),但一旦我调用它,就会得到未定义的变量,比如_计数器

    public function getLikeCounter($post_id) {
    $post = Post::find($post_id);
    $number = null;
    $like_counter = \DB::table('likes')->where('post_id', $post->id)->where('like',!$number)->count();
    return View::make('layouts.viewvideo', ['like_counter' => $like_counter]);
}
视图:

]))

谢谢你的帮助。

试试这个

return View::make('layouts.viewvideo')->with('like_counter', $like_counter);
试试这个

return View::make('layouts.viewvideo')->with('like_counter', $like_counter);

Nvm。。通过直接添加到视图中修复了此问题:

<span class="badge">{{ $post->likes->where('post_id', $post->id)->where('like', 1)->count() }}</span>
{{$post->likes->where('post_id',$post->id)->where('like',1)->count()}

Nvm。。通过直接添加到视图中修复了此问题:

<span class="badge">{{ $post->likes->where('post_id', $post->id)->where('like', 1)->count() }}</span>
{{$post->likes->where('post_id',$post->id)->where('like',1)->count()}
试试这个:

return view('layouts.viewvideo',compact('like_counter'));
希望这对您有所帮助。

尝试一下:

return view('layouts.viewvideo',compact('like_counter'));

希望这对您有所帮助。

我认为问题出在您的代码这一行where子句上:

$like_counter = \DB::table('likes')->where('post_id', $post->id)->where('like',!$number)->count();
拉威尔5.2

更改此项:

->where('like', '!=', $number)->count();

差异:其中
类似于
!=null

我认为问题出在您的代码这一行的where子句上:

$like_counter = \DB::table('likes')->where('post_id', $post->id)->where('like',!$number)->count();
拉威尔5.2

更改此项:

->where('like', '!=', $number)->count();

差异:其中
类似于
!=空

更简单的方法是获取记录并对其进行计数

 public function getLikeCounter($post_id) {
    $post = Post::find($post_id);
    $number = null;
    $like_counter = count(\DB::table('likes')->where('post_id', $post->id)
    ->where('like','!=',$number)->get());
    return View::make('layouts.viewvideo', ['like_counter' => $like_counter]);
 }

一个更简单的方法是获取记录并进行计数

 public function getLikeCounter($post_id) {
    $post = Post::find($post_id);
    $number = null;
    $like_counter = count(\DB::table('likes')->where('post_id', $post->id)
    ->where('like','!=',$number)->get());
    return View::make('layouts.viewvideo', ['like_counter' => $like_counter]);
 }