skip to Main Content

In my Laravel/Lumen Project (Version 8) I try to retrieve data from my Oracle Database, but I get unexpected data.

My database contains following 4 entries:

ID FOREIGN_ID NAME
1 100 Entry1
2 100 Entry2
3 100 Entry3
4 200 Entry4

My model:

class Entry extends Model
{
    protected $connection = 'MyConnection';
    protected $table = 'MY_TABLE';
    protected $fillable = ['foreign_id', 'name'];
    protected $hidden =  ['foreign_id'];
    protected $casts = [
        'foreign_id' => 'integer'
    ];
}

When I execute the following line of code, only Entry1 with ID 1 is returned, while I would expect an empty collection:

Entry::where([['id', '!=', 1], 'foreign_id' => 100, 'name' => 'Entry1'])->get();

To analyze the problem, I also tried to write lined up where clauses:

//returns all but Entry1; correct
Entry::where(['id', '!=', 1])->get();

//returns Entry2 and Entry3; correct
Entry::where(['id', '!=', 1])->where(['foreign_id' => 100])->get(); 

//returns only Entry1; wrong, should be an empty collection
Entry::where(['id', '!=', 1])->where(['foreign_id' => 100])->where(['name' => 'Entry1'])->get(); 

The generated sql query looks like this:

"select * from "MY_TABLE" where ("ID" != ? and "FOREIGN_ID" = ? and "NAME" = ?)"

The Lumen Version is: 8.3.4

EDIT:

I have tried this on another Laptop now. There I get an empty collection.
Have you any idea what configuration/setting might do the trick, that my query is interpreted in two different ways?

2

Answers


  1. Try below Code

    Entry::where([
    ['id', '!=', 1], 
    ['foreign_id', 100], 
    ['name', 'Entry1']
    ])->get();
    
    Login or Signup to reply.
  2. Entry::where(['foreign_id' => 100,'name' => 'Entry1'])
    ->where('id', '!=', 1)
    ->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search