Answer the question
In order to leave comments, you need to log in
Laravel relationships or normal queries with LEFT JOIN?
Friends, help me put the information in my head on the shelves correctly.
I'll tell you honestly, I can't understand the better the relationship from regular queries
. Here's an example, I have 3 tables:
payment_services
id
type_object_id
object_id
type_service_id
type_objects
id
name
translit
type_services
id
name
translit
payment_services->object_id
type_objects->translit
SELECT *
FROM payment_services
LEFT JOIN type_objects ON type_objects.id = payment_services.type_object_id
LEFT JOIN type_services ON type_services.id = payment_services.type_service_id
WHERE type_services.translit='hot'
class paymentService extends Model
{
public static function ads()
{
typeService::where('translit', 'hot')->first()->actualAdsPayments();
}
}
class typeService extends Model
{
public function actualAdsPayments()
{
return typeService::hasManyThrough('App\paymentService','App\typeService','id')
}
}
#original: array:9 [▼
"id" => 3
"type_object_id" => 3
"object_id" => 1592148
"type_service_id" => 3
"period" => 7
"price" => "50.00"
"updated_at" => "2019-12-12 14:59:31"
"created_at" => "2019-12-11 22:20:42"
"laravel_through_key" => 3
]
Answer the question
In order to leave comments, you need to log in
Either ORM or SELECT.
ORM - preserves data integrity during database modification manipulations.
At select - without a difference.
When choosing data modification does not occur, they can be easily done through raw SQL.
Moreover, this is good practice, and entities are not, by definition, data stores at all.
There is no such rule.
It’s just that in Laravel, projects are often made blunder and in production, and in essence everything is shoved.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question