Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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 laravel数据库查询未按预期工作_Php_Laravel - Fatal编程技术网

Php laravel数据库查询未按预期工作

Php laravel数据库查询未按预期工作,php,laravel,Php,Laravel,我有一个问题如下: $this->_data['news'] = PageContent::orderBy('created_at', 'desc')->where('category_id', 3)->where('status', 1)->first(); 在我的index.blade.php文件中,数据以如下方式显示: @if(count($news) > 0) <div class="post_list_content_unit col-sm

我有一个问题如下:

$this->_data['news'] = PageContent::orderBy('created_at', 'desc')->where('category_id', 3)->where('status', 1)->first();
在我的index.blade.php文件中,数据以如下方式显示:

@if(count($news) > 0)
    <div class="post_list_content_unit col-sm-6">
        <div class="feature-image">
            <a href="{{URL::to('/')}}/about-us/news-event" class="entry-thumbnail">
                <img width="370" height="260"
                     src="{{URL::to('Uploads/pageContents/'.$news->image)}}"
                     alt="{{$news->title}}">
            </a>
        </div>
        <div class="post-list-content">
            <div class="post_list_inner_content_unit">
                <h3 class="post_list_title">
                    <a href="{{URL::to('/')}}/about-us/news-event"
                       rel="bookmark">{{$news->title}}</a>
                </h3>
                <div class="post_list_item_excerpt">
                    {!! str_limit($news->description, 300) !!}
                </div>
            </div>
        </div>
    </div>
@else
    <div></div>
@endif
@if(计数($news)>0)
{!!str_limit($news->description,300)!!}
@否则
@恩迪夫
但它不显示仅状态为1的记录,而是显示状态值为0的数据

查询是否有问题?

您应该尝试以下方法:

$this->_data['news'] = PageContent::orderBy('created_at', 'desc')
            ->where(function($query) {
              $query->where('category_id', 3)
              $query->where('status', 1);
            })
            ->first();

希望这为你工作

您可以将数组传递给
where
方法:

$this->_data['news'] = PageContent::orderBy('created_at', 'desc')
->where(array('category_id'=>3,'status'=>1))
->first();

通过在where子句中传递数组,可以采用两种方法:

$this->_data['news']  = DB::table('PageContent')
    ->orderBy('created_at', 'desc')
    ->where([
        ['category_id', '=', '3'],
        ['status', '=', '1'],
      ])->first();
或者

$this->_data['news']  = PageContent::orderBy('created_at', 'desc')
    ->where([
        ['category_id', '=', '3'],
        ['status', '=', '1'],
      ])->first();

事实上,如果你不知道过滤器的确切数量,我觉得这种方法有点灵活,但我不想显示被禁用的记录。。。我猜这个查询不适用于那个。那个也会获取状态值为0的记录。。。不起作用