【问题标题】:Laravel: Getting wrong results with model::whereLaravel:使用 model::where 得到错误的结果
【发布时间】:2015-09-07 00:02:23
【问题描述】:

我对 laravel 很陌生,但实际上我在如此基本的人员中得到了错误的结果,以至于我很困惑。

我有一张桌子:

Schema::create('matches', function(Blueprint $table)
    {
        $table->increments('id')->unsigned()->index();
        $table->integer('season_id')->unsigned()->index();
        $table->foreign('season_id')
            ->references('id')
            ->on('seasons')
            ->onUpdate('cascade')
            ->onDelete('cascade')
        ;
        $table->integer('championship_id')->unsigned()->index();
        $table->foreign('championship_id')
            ->references('id')
            ->on('championships')
            ->onUpdate('cascade')
            ->onDelete('cascade')
        ;
        $table->integer('round')->unsigned()->index();
        $table->integer('match')->unsigned()->index();

        $table->integer('team1_id')->unsigned()->index();
        $table->foreign('team1_id')
            ->references('id')
            ->on('teams')
            ->onUpdate('cascade')
            ->onDelete('cascade')
        ;
        $table->integer('team2_id')->unsigned()->index();
        $table->foreign('team2_id')
            ->references('id')
            ->on('teams')
            ->onUpdate('cascade')
            ->onDelete('cascade')
        ;

        $table->integer('team1_goals')->unsigned()->nullable()->default(null);
        $table->integer('team2_goals')->unsigned()->nullable()->default(null);
        $table->integer('team1_misses')->unsigned()->nullable()->default(null);
        $table->integer('team2_misses')->unsigned()->nullable()->default(null);
        $table->timestamp('start_at');
        $table->boolean('finished')->default(0);
        $table->timestamps();
    });

经过一些数学运算后,它充满了球队 ID、目标、失误等的值。 好的,现在我想获取 team1 获胜的所有原始数据,(team1_goals > team2_goals)

在工匠修补匠中输入:

$matches = App\Match::where('team1_goals','>','team2_goals')->get()

并得到如此意想不到的结果(最后 4 个):

<App\Match #00000000455ad9a2000000015b65c7f8> {
       id: 237,
       season_id: 1,
       championship_id: 1,
       round: 30,
       match: 5,
       team1_id: 7,
       team2_id: 10,
       team1_goals: 2,
       team2_goals: 1,
       team1_misses: 2,
       team2_misses: 2,
       start_at: "2015-06-21 14:38:22",
       finished: 1,
       created_at: "2015-06-21 13:53:52",
       updated_at: "2015-06-21 13:57:00"
   },
   <App\Match #00000000455ad9dd000000015b65c7f8> {
       id: 238,
       season_id: 1,
       championship_id: 1,
       round: 30,
       match: 6,
       team1_id: 9,
       team2_id: 12,
       team1_goals: 2,
       team2_goals: 2,
       team1_misses: 1,
       team2_misses: 1,
       start_at: "2015-06-21 14:38:22",
       finished: 1,
       created_at: "2015-06-21 13:53:52",
       updated_at: "2015-06-21 13:57:00"
   },
   <App\Match #00000000455ad9dc000000015b65c7f8> {
       id: 239,
       season_id: 1,
       championship_id: 1,
       round: 30,
       match: 7,
       team1_id: 5,
       team2_id: 16,
       team1_goals: 1,
       team2_goals: 3,
       team1_misses: 0,
       team2_misses: 0,
       start_at: "2015-06-21 14:38:22",
       finished: 1,
       created_at: "2015-06-21 13:53:52",
       updated_at: "2015-06-21 13:57:00"
   },
   <App\Match #00000000455ad9df000000015b65c7f8> {
       id: 240,
       season_id: 1,
       championship_id: 1,
       round: 30,
       match: 8,
       team1_id: 8,
       team2_id: 1,
       team1_goals: 1,
       team2_goals: 2,
       team1_misses: 1,
       team2_misses: 1,
       start_at: "2015-06-21 14:38:22",
       finished: 1,
       created_at: "2015-06-21 13:53:52",
       updated_at: "2015-06-21 13:57:00"
   }

如你所见:

  • id 237:team1_goals > team2_goals
  • id 238:team1_goals = team2_goals,
  • id 239:team1_goals
  • id 240:team1_goals

    $matches = App\Match::where('team1_goals','>','team2_goals')->count()

给出结果 222 (of 240) 所以它不是 all() 或 smth

为什么这会发生在我身上?

【问题讨论】:

    标签: php laravel models laravel-artisan


    【解决方案1】:

    看起来明白了:在函数第 3 个参数不能设置为字段名称的地方,所以我的案例比较就像

    select * from matches where team1_goals > 'team2_goals'
    

    我应该使用

    $matches = App\Match::whereRaw('team1_goals > team2_goals')->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-01
      • 2023-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多