vendredi 13 août 2021

Get data from 2 tables with a join

I have these 2 tables : keyword and keyword_translated

keyword
id
name

keyword_translated
id
translation
keyword_id

I want to get all keyword, doesn't matter has or not relation with keyword_translated. At the end I want to get something like :

[
  [
      keyword_id => 1,
      keyword_name => 'firstKeyword'
      keyword_translated_id => 1, // if exist relation between `keyword` and `keyword_translated`
      keyword_translated_translation => 'This is translation of firstKeyword' // if exist relation between `keyword` and `keyword_translated`
  ],
  [
      keyword_id => 2,
      keyword_name => 'secondKeyword'
      keyword_translated_id => null, // if didn't exist relation between `keyword` and `keyword_translated`
      keyword_translated_translation => null // if didn't exist relation between `keyword` and `keyword_translated`
  ],
]

I tried like this :

$keywords = DB::table('keywords')
        ->join('keywords_translated', 'keywords.id', '=', 'keywords_translated.keyword_id')
        ->select('keywords.*', 'keywords_translated.*')
        ->get();

But I have empty data. Can you give me an advice please ? Thx in advance and sorry for my english.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire