Laravel:使用model :: where获得错误的结果

时间:2015-06-21 12:47:58

标签: php laravel models artisan

我对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&gt; team2_goals
  • id 238:team1_goals = team2_goals,
  • id 239:team1_goals&lt; team2_goals,
  • id 240:team1_goals&lt; team2_goals,

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

给出结果222(240)所以它不是全部()或smth

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

看起来像得到它:在哪里函数3rd参数不能设置为字段名称,所以我的案例比较就像

select * from matches where team1_goals > 'team2_goals'

我应该使用

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