Answer the question
In order to leave comments, you need to log in
How to properly get items from today's DB in Laravel 8?
Good afternoon, I need to get the elements from the database for today using Carbon.
There are 3 tables: districts, addresses, orders. Here are their models:
class Districts extends Model
{
use HasFactory;
protected $primaryKey = 'id';
protected $guarded = [];
public function address()
{
return $this->hasMany('App\Models\Addresses', 'district_id', 'id');
}
}
class Addresses extends Model
{
use HasFactory;
protected $guarded = [];
public function orders()
{
return $this->hasMany('App\Models\Orders', 'address_id', 'id');
}
public function district()
{
return $this->belongsTo('App\Models\Districts', 'district_id', 'id');
}
}
class Orders extends Model
{
use HasFactory;
protected $guarded = [];
protected $searchable = [
'name',
];
public function address()
{
return $this->belongsTo('App\Models\Addresses', 'address_id', 'id');
}
}
$districts = Districts::where(function($ds)
{
$ds->address->where(function ($addr)
{
$addr->orders->whereDate('date_of_completion', Carbon::today());
});
})->get();
Answer the question
In order to leave comments, you need to log in
Fuck. Doc teach first. OOP. Turn on your brain, and then spank your shit code.
All of this is spelled wrong. First, learn how to write queries correctly. Then you'll figure out the date
$districts = Districts::where(function($ds) {
$ds->address->where(function ($addr)
{
$addr->orders->whereDate('date_of_completion', Carbon::today());
});
})->get();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question