I have some unterstanding problem with the scope in Laravel 5.4
I try to make a scope with a simple where like syntax.
class Addresses extends Model
{
protected $table = "Addresses";
protected $primaryKey = 'adress';
public $timestamps = false;
public function scopeSearchAdress($query, $searchTerm)
{
return $query->where('adress', 'LIKE', '%'.$searchTerm.'%s');
}
public function company()
{
return $this->hasOne(Company::class, 'adress');
}
public function contactPerson()
{
return $this->hasMany(ContactPerson::class, 'adress');
}
}
when i start the php artisan tinker and write this command: App\Address::searchAdress('1330')->get()
i get this:
Illuminate\Database\Eloquent\Collection {#695
all: [],
}
and i don't understand why i get an empty array. If i make a query in MSSQL i get the address with this adress=1330
the problem is i guess with the string and int value
you are passing a string, work with this
App\Address::searchAdress(1330)->get();
change the scope query
return $query->where('adress', '=', $searchTerm);