Skip to content
Advertisement

Laravel translations

can someone tell me how can I search (with livewire) products which have a translatable title? Currently, I am using astrotomic/laravel-translatable package. Translations are working just fine and I can statically pull the right data but when I try to search it I have an issue because the package is expecting to have a title in a different table and not in the main products table.

Here are the code snippets.

Translatable product fields table:

Schema::create('product_translations', function (Blueprint $table) {
        $table->id();
        $table->string('locale')->index();
        $table->foreignId('product_id')->constrained()->onDelete('cascade');
        $table->string('title');
        $table->timestamps();
    });

The main products table:

Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->foreignId('category_id')->constrained()->onDelete('cascade');
        $table->foreignId('user_id')->constrained()->onDelete('cascade');
        $table->timestamps();
    });

In the first table I need to put only the data which can be translated and in the main products table I must put only the data which does not need to be translated

Just not to write the whole process of creating products etc. I will put a short note on how it is working.

When I create 1 product it will loop through the config.app available locales and create for all languages same product (same title) then we can choose different languages and translate it based on it.

When I want to pull static data it looks like this:

$products = Product::select('id', 'category_id', 'price', 'created_at', 'image')
        ->whereHas('translations', function ($query) {
            $query->where('locale', app()->getLocale());
        })
        ->with('category:id,name')
        ->orderBy('category_id', 'asc')
        ->get();

Now I want to make live search with livewire but can’t pull title like this:

$products = Product::select('id', 'category_id', 'price', 'created_at', 'image')
        ->whereHas('translations', function ($query) {
            $query->where('locale', app()->getLocale());
        })
        ->when($this->searchProducts != '', function($query) {
            $query->where('title', 'like', '%'.$this->searchProducts.'%');
        })
        ->with('category:id,name')
        ->orderBy('category_id', 'asc')
        ->get();

because the title is in a different table.

Any ideas? Thanks

Advertisement

Answer

I didn’t test it but can work

$products = Product::select('id', 'category_id', 'price', 'created_at', 'image')
        ->whereHas('translations', function ($query) use ($this->searchProducts) {
            if($this->searchProducts) {
                $query->where('locale', app()->getLocale());
                $query->where('title', 'like', '%'.$this->searchProducts.'%');
            }
        })
        ->with('category:id,name')
        ->orderBy('category_id', 'asc')
        ->get();
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement