Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从模型和订单描述中获取最受欢迎的文章_Php_Laravel - Fatal编程技术网

Php 如何从模型和订单描述中获取最受欢迎的文章

Php 如何从模型和订单描述中获取最受欢迎的文章,php,laravel,Php,Laravel,我在laravel 5.2项目中遇到了一个小问题 我想得到受欢迎的文章,访问量最大的文章和DESC的订单。 这是我的控制器代码 public function trending(){ $trends = DB::table('articles')->orderBy('hits', 'DESC')->get(); return view('frontend.index', compact('trends')) 这不显示任何值。。我的laravel说未定义的变量$tre

我在laravel 5.2项目中遇到了一个小问题

我想得到受欢迎的文章,访问量最大的文章和DESC的订单。 这是我的控制器代码

public function trending(){
    $trends = DB::table('articles')->orderBy('hits', 'DESC')->get();
    return view('frontend.index', compact('trends'))
这不显示任何值。。我的laravel说未定义的变量$trends

@foreach($trends as $trend)
<li>

       <a href="#"><img class="photo" src="uploads/thumbs/{{ $trend->image }}" width="90px" height="90px"/></a>

                <h2><a href="{{ route('single.show',$article->slug) }}">{{ $trend->title }}</a></h2>

                <div class="date">{{date('M j, Y h:ia', strtotime($trend->created_at))}}</div>
            </li>
@endforeach
@foreach($trends作为$trend)
  • {{date('mj,yh:ia',strottime($trend->created_at))}
  • @endforeach

    {{trends}}变量没有任何值。

    首先检查$trends是否有值

    @如果(计数($trends)>0) @foreach(趋势为$trend) ... ... @endforeach @恩迪夫


    然后在phpmyadmin中运行上面的查询并检查什么是return。

    我假设在实际代码中,
    return
    行的末尾有一个分号?如果不是的话,我会先这么做。抱歉,像往常一样,返回行末尾有分号。
    returnview('frontend.index',compact('trends'))
    需要在它的末尾有分号。是的。我在return.return视图('frontend.index',compact('trends')的末尾有一个分号;如果你加上“dump($trends)”,你会得到什么;在函数trending()中,只是为了确保db查询工作正常。将其添加到两行之间(返回之前和DB::table之后…)