Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.23k stars 156 forks source link

Option to ignore checking null for SQL optimization #255

Closed HuyPham55 closed 2 years ago

HuyPham55 commented 2 years ago

I have a translatable model declared as below, nothing extra-ordinary: Slide.php

public $translationForeignKey = 'slide_id';
public $translationModel = SlideTranslation::class;
public $translatedAttributes = ['content'];

SlideTranslation.php

protected $table = 'slide_translations';
protected $fillable = ['content'];
public $timestamps = false;

When I want to retrieve translations for Slide model with a specific id, take 5 for example. This is the SQL statement that I observed:

$content = Slide::find(5)->content;
select * 
from `slide_translations` 
where `slide_translations`.`slide_id` = 5 
and `slide_translations`.`slide_id` is not null

It appears that the later part ( slide_translations.slide_id is not null` ) is un-neccessary. I wonder if there is an option that I'm missing to ignore the second condition, or the package needs a small improvement. Thanks for reading.

Gummibeer commented 2 years ago

Hey, the query isn't made by us but Laravel itself. https://github.com/laravel/framework/blob/fda820074cc888495c78c9a76e7fa98fa4980d79/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php#L75-L89