【问题标题】:Multiple Where Clause in Eloquent & LaravelEloquent 和 Laravel 中的多个 Where 子句
【发布时间】:2019-11-27 10:54:40
【问题描述】:

试图用 eloquent 做一个多重选择语句。

使用Blog::where() ... 函数,并在其中包含一个数组。

$matchThese = ['title' => post('title'), 'slug' => post('slug')];
 return Blog::where($matchThese)->get();

在测试时,即使输入匹配,它也会返回一个空值,数据库表名正确写入

Model 扩展了 October CMS 模型,因此 laravel 中的所有 eloquent 方法都包含在内。博客模型如下

<?php namespace Andre\Blogroutes\Models; 

use Model;
use ModelNotFoundException;

class Blog extends Model
{
    use \October\Rain\Database\Traits\Validation;
    public $table = 'blog';

}

【问题讨论】:

  • 请展示您的博客模型
  • 请在您的帖子中更新。不在评论中
  • 在下面查看我的答案,如果您还有问题,请告诉我?

标签: laravel-5 eloquent octobercms


【解决方案1】:

$table 变量必须是 protected 而不是 public

public $table = 'andre_blog';

protected $table = 'andre_blog';

【讨论】:

    【解决方案2】:

    以下两个查询完全相同:

    $matchThese = ['title' => post('title'), 'slug' => post('slug')];
    return Blog::where($matchThese)->get();
    

    return DB::table('blog')->where('title', '=',post('title'))->where('slug','=', post('slug'))->get();
    

    如果一个有效而其他无效,则意味着您的 Blog 模型实际上并未针对正确的表 blog。您应该在 Blog 模型中添加以下行并查看旧查询是否有效:

    protected $table = 'blog';

    在您的控制器中,不要忘记添加: use App\Blog;

    在它之上。试一试旧查询,看看是否可行。

    【讨论】:

      【解决方案3】:

      能够解决这个问题:

      DB::table('blog')->where('title', '=',post('title'))->where('slug','=', post('slug'))->get();
      

      【讨论】:

      • 你的表名 andre_blogblog 在数据库中?
      猜你喜欢
      • 2023-03-28
      • 2015-08-06
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      • 2020-09-28
      • 2018-09-05
      • 1970-01-01
      相关资源
      最近更新 更多