The most efficient method for combining two datasets in laravel

Now you can blow me up if I ask the question too specifically, saying Laravel, but I wonder what is the most efficient way to combine two datasets with tools in Laravel.

Is there a specific way of thinking that I should use when choosing between using eloquent relationships and associations with the query builder? Or are there any flaws in using the query builder for small or large datasets that I have to consider?

I am creating a ticket system in which only one or two fields from the user table are required, and it seems very important to consider the possibility of using relations at this stage, but if the needs of the system increase, this can lead to dirty code. Is this a situation where relations are resolved there?

Is there any kind of join action when using an eloquent relationship that I should be aware of?

If I really do not understand how I ask this question, I will try to reformulate it.

+4
source share
1 answer

, , Eloquent, (, , ) Eloquent. , ​​:

foreach($user->tickets as $ticket) { ... etc ... }

, Laravel. Laravel. . - Laracast . .

:

<?php
class Ticket extends Model {
    public function user() {
        return $this->belongsTo('User');
    }
}

:

<?php
class User extends Model {
    public function tickets() {
        return $this->hasMany('Ticket');
    }
}

:

tickets (table)
- id
- user_id

users (table)
- id

, . :

$ticket->user->first_name
$ticket->user->last_name
+1

Source: https://habr.com/ru/post/1650601/


All Articles