Skip to content
Advertisement

Laravel pass parameter to its distant relationship (translate children)

I have categories with id, parent_id, slug , pivot table category_language which columns are id,category_id,language_id,value As you can see I can translate parent category, but can’t send desired $lang_id to children translations, so each children having all translations here is what I get:

{
    "id": 1,
    "parent_id": 0,
    "slug": "personal-computers",
    "created_at": "2019-12-27 15:05:31",
    "updated_at": "2019-12-27 15:05:31",
    "children": [
        {
            "id": 3,
            "parent_id": 1,
            "slug": "accessories-for-pc",
            "created_at": "2019-12-27 15:05:32",
            "updated_at": "2019-12-27 15:05:32",
            "translations": [
                {
                    "id": 1,
                    "code": "en",
                    "name": "English",
                    "pivot": {
                        "category_id": 3,
                        "language_id": 1,
                        "value": "Acc for PC",
                        "id": 7
                    }
                },
                {
                    "id": 2,
                    "code": "ru",
                    "name": "Русский",
                    "pivot": {
                        "category_id": 3,
                        "language_id": 2,
                        "value": "Аксессуары для ноутбуков и ПК",
                        "id": 8
                    }
                },
                {
                    "id": 3,
                    "code": "ro",
                    "name": "Romana",
                    "pivot": {
                        "category_id": 3,
                        "language_id": 3,
                        "value": "aksessuari-dlya-noutbukov-i-pk-ro",
                        "id": 9
                    }
                }
            ]
        }
    ],
    "translations": [
        {
            "id": 1,
            "code": "en",
            "name": "English",
            "pivot": {
                "category_id": 1,
                "language_id": 1,
                "value": "PC",
                "id": 1
            }
        }
    ]
}

Controller:

return Category::with('children')
                ->with(array('translations'=>function($query) use ($lang_id){
                    $query->where('language_id',$lang_id);
                }))
                ->where('parent_id',0)->first();

Model

class Category extends Model
{ ..
public function translations()
    {
        return $this->belongsToMany('AppModelsTranslation','category_language', 'category_id' ,'language_id' )->withPivot('value','id');
    }

    public function children()
    {
        return $this->hasMany( 'AppModelsCategory' , 'parent_id' , 'id' )->with('translations');
    }
}

Advertisement

Answer

Dry7 answer was close to the one I’ve implemented later, so I upvoted him. Finally in model I’ve added: ...->where('language_id',helper_SetCorrectLangIdForQuery());

and function helper_SetCorrectLangIdForQuery is using global helper of Laravel request()->lang . If lang=enz, than it takes default language from another helper.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement