I need to convert this Eloquent Model Function to a MySQL Query:
public function currentPrice(): HasOne
{
return $this->hasOne(Price::class)->ofMany([
'published_at' => 'max',
'id' => 'max',
], function(QueryBuilder $query) {
$query->where('published_at', '<', now());
});
}
So, the query should look something like this (missing the above HasOne relation logic that should be in the query):
SELECT items.id, items.article_name, prices.price, prices.published_at, weights.weight, weights.amount
FROM items
INNER JOIN prices ON items.id = prices.item_id
INNER JOIN weights ON items.id = weights.item_id
ORDER BY prices.published_at;
show variables like 'general%';
to see the current settings and log file), then turn on the general log, run the query, turn off the general log, then view the log file to see the queries that it's running.$this->currentPrice()->toSql()
will show you the underlying Eloquent-generated SQL.