Home > OS >  get subcategoriesTranslations from maincategory Model laravel
get subcategoriesTranslations from maincategory Model laravel

Time:08-24

i have these tables structure

1. main_categories 

id -> integer = 1

name -> string

updated_at -> Null

created_at -> 2022/08/22

 2. sub_categories

 ***Row 1***

id -> integer = 10

name -> string

category_id-> 1

translation_lang -> en

translation_of -> 0

name -> string = Game

category_id-> 1

updated_at -> Null

created_at -> 2022/08/22

 2. sub_categories // Row 2

 ***Row 2*** // Row 2

id -> integer

name -> string

category_id-> 1

translation_lang -> ar

translation_of -> 10

name -> string = لعبة

category_id-> 1

updated_at -> Null

created_at -> 2022/08/22

The default language is En; the translation_lang is name -> string translation. the translation_of is translation to the Row 1 id. it has the default language

i want to get sub_categories Row 2 From main_categories table

These Are My Models:

MainCategory:

class main_category extends Model
{
  
protected $table = 'main_categories';

    protected $fillable = [
     'name' ,'id' ,'created_at' , 'updated_at'
];

      public function subCategoriesTranslation() {
   // Here i Want to make relationship to get ***Row 2*** from subcategories Table How can i make it?
   }

}

SubCategory:

class SubCategory extends Model
{
    use HasFactory;

    protected $table = 'sub_categories';

    protected $fillable = [
        'translation_lang','translation_of' ,'category_id' ,  'name' ,'created_at' , 'updated_at',
    ];


}

Summary of the question: I Want to get the row 2 in subcategories table from maincategory table

CodePudding user response:

What I had understood. You want something like this right?

    public function subCategoriesTranslation($query)
{
    $query
        ->Join('sub_categories', 'sub_categories.category_id', '=', 'main_categories.id')
        ->select('sub_categories')
        ->where('sub_categories.id', '=', '2')
        ->first();
}
  • Related