Poznajemy whereIn, whereNotIn, whereKey oraz whereKeyNot, które przydają się w przeszukiwaniu baz danych. Do dzieła.
Ok, doimportujmy Address w console.php i napiszmy komendę:
Artisan::command('wherein', function () {
$sql = Address::whereIn('city', ["London", "Warsaw"])->get();
foreach($sql as $addr){
$this->comment("ID: {$addr->id}");
$this->comment("City: {$addr->city}");
$this->comment("Street: {$addr->street}");
$this->comment("----------------------------------");
}
});
Raczej widać, co ta komenda robi. Teraz whereNotIn:
Artisan::command('wherenotin', function () {
$sql = Address::whereNotIn('city', ["Paris", "Budapest"])->get();
foreach($sql as $addr){
$this->comment("ID: {$addr->id}");
$this->comment("City: {$addr->city}");
$this->comment("Street: {$addr->street}");
$this->comment("----------------------------------");
}
});
Natomiast jeżeli chodzi o pola id (czy szerzej, o pola primary key) to nie powinniśmy używać whereIn i whereNotIn. Jest od tego zupełnie inna metoda:
Artisan::command('wherekey', function () {
$sql = Address::whereKey([1,2])->get();
foreach($sql as $addr){
$this->comment("ID: {$addr->id}");
$this->comment("City: {$addr->city}");
$this->comment("Street: {$addr->street}");
$this->comment("----------------------------------");
}
});
Mamy też metodę whereKeyNot:
Artisan::command('wherekeynot', function () {
$sql = Address::whereKeyNot([2,3,4])->get();
foreach($sql as $addr){
$this->comment("ID: {$addr->id}");
$this->comment("City: {$addr->city}");
$this->comment("Street: {$addr->street}");
$this->comment("----------------------------------");
}
});