Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zync09's avatar

Get missing entries based on missing id's from another table

Hey all,

Very new to Laravel but loving it so far. Just wondering if there is a way to see the missing id's from a table when compared to another table. Here is my scenario.

I have 2 tables.

module_translations
            $table->increments('id');
            $table->integer('module_id')->unsigned();
            $table->integer('language_id')->unsigned();
            $table->string('name');
            $table->string('description');


languages
            $table->increments('id');
            $table->string('name');
            $table->string('code');

In my module_translations the data looks like this (for simplicity's sake have removed the name and description fields):

id  module_id   language_id
3   1   1
52  1   2
17  1   6
11  1   7
32  1   8
27  2   1
36  2   2
29  2   4
19  2   6
37  2   7
7   2   8
31  2   9
18  3   1
44  3   2
45  3   3
41  3   4
25  3   5
6   3   6
47  3   7
22  3   8

and languages_table

id  name    code
1   English UK  en-uk
2   English US  en-us
3   Chinese-Simplified  zh-hans-cn 
4   Chinese-Traditional zh-hant-tw
5   Bahasa Indonesian   id-id 
6   French  fr-fr
7   Italian it-it
8   Polish  pl-pl
9   Spanish es-es
10  Portuguese Brazillian   pt-br

What i'm trying to get is each of the missing translation language id's in the module_translations table. Is there an easy way to do this with Eloquent? For example module 1 is missing translations for language id 3,4,5, 9 and 10.

At the moment I've created an empty array called translations_done and loop though all the translations and push the language id into that table. then do another loop through the languages and check if the language_id is not in the translations_done array and then make an array for the missing ones and loop in my template with that array. Clunky as but it goes the job done sort of. But there must be a better way with a query?

Thanks all

0 likes
1 reply
zync09's avatar

Well found another way. Still probably not the best way but it's holding. Made a method in my Module to get the missing translations of the current module. Just a simple array difference between the ID's of the currently translated language_id 's and all the language_id's in the Languages table.

Must be a more efficient way though right? But guess it does get the job done for now.

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Language;

class Module extends Model
{
    protected $fillable = ['position'];
    protected $dates = ['release_date'];

    public function translations()
    {
      return $this->hasMany('App\ModuleTranslation');
    }

    public function missingTranslations()
    {
      $languageIDs = Language::all()->pluck('id')->toArray();
      $translatedIDs = ModuleTranslation::where('module_id', $this->id)->get()->pluck('language_id')->toArray();
      $missingTranslations = Language::find(array_diff($languageIDs, $translatedIDs));

      return $missingTranslations;
    }
}

Please or to participate in this conversation.