U
U
usver123452019-12-13 05:04:01
SQL query optimization
usver12345, 2019-12-13 05:04:01

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

where type_object_id is the foreign key of the type_objects table, type_service_id is the foreign key of the type_service table
I need to get a collection that will contain fields with the condition type_services.translit='hot'
payment_services->object_id
type_objects->translit

If you do a simple query, then here
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'

At the output, I will get all the data I need in abundance.
Here's what I managed to do with the relationship:
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')  
  }
}

Here I will not get all the data, in particular, I will not get type_objects->translit, this is what I get as an output
#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
      ]

fields: price, period, updated_at, created_at from the payment_services table, at the beginning I did not describe them as meaningless.
According to the rules of ORM, I need to do everything through relationships, but for the life of me I don’t understand why? If a regular request is much clearer and simpler for me, and even with a relationship, I don’t get all the fields I need.
Even if we take the classics, "one author and many books", why do I need relationships, if I can do everything with one query with LEFT JOIN operators.
Just don't send it to the dock right away, I read it several times, but I still didn't understand the need for relationships ( (

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2019-12-13
@xmoonlight

Either ORM or SELECT.
ORM - preserves data integrity during database modification manipulations.
At select - without a difference.

E
Evgeny Romashkan, 2019-12-13
@EvgeniiR

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 question

Ask a Question

731 491 924 answers to any question