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

bobmyles's avatar

Getting data from multiple tables via Laravel Eloquent

I have three tables; rate_params, review_form_languages, and review_form_translations. And corresponding models RateParam, ReviewFormLanguage and reviewFormTranslation.

rate_params has columns id, info_message, order and validation.

review_form_languages has id and name

review_form_translations has id, rate_params_id, review_form_languages_id, and text.

I would like to have query that fetches all the data from rate_params and the text from review_form_translations where review_form_languages.name is the passed parameter.

I have a query that fetches all the data from RateParam model as below

 $reviewForm = RateParam::select(
        'rate_params.id',
        'rate_params.validation',
        'rate_params.info_message',
    )->orderBy('order', 'asc')->get()->toArray();
    

How do I join to get the text from review_form_translations where review_form_languages.name is the passed parameter?

0 likes
1 reply
Tray2's avatar

Since I'm better with SQL than Eloquent I use it and would do something like this.

$reviewForm = DB::select (
'SELECT rp.id, 
               rp.info_message,
               rp.validation,
               rft.text 
FROM rate_params rp,
            review_form_translations rft
            review_form_language rfl
WHERE rp.id = rft.rate_params_id
AND rfl.id = rft.review_form_languages_id
AND rlf.name = ?
ORDER BY column ASC', $language);

Change column to the one you want to order by.

Please or to participate in this conversation.