Laravel: Getting wrong results with model::where

47 views Asked by At

I'm very new to laravel, but actually I'm getting wrong results in so basic staff that i'm very confused.

Well i have a table:

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();
    });

After some maths its filled with values of teams ids, goals, misses etc. Ok, now i want to get all raws where team1 wins, (team1_goals > team2_goals)

in an artisan tinker i type:

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

and get so unexpected results(last 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"
   }

As you can see match:

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

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

Gives result 222 (of 240) so its not all() or smth

Why this is happening to me ?

1

There are 1 answers

0
Vitaliy Gura On

Looks like a got it: In where function 3rd argument cannot be set to field name, so im my case comparison was like

select * from matches where team1_goals > 'team2_goals'

I should use

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