Gondwana's avatar

Relationship tables with Laravel and livewire

Hi I'm new to laravel and livewire. I am making an ecommerce website. I am performing crud operations on single tables. But I had a problem with the category table. I have categories and category_names_table table. When listing categories_table data, I need to match category_id column from category_names_table and write category_name value from category_names_table. Models, livewire components.. how should it be. Thanks

0 likes
8 replies
Gondwana's avatar

@webrobert thanks for your interest.

MIGRATION category:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateCategoriesTable extends Migration {
public function up()
{
Schema::create('categories', function(Blueprint $table) {
$table->increments('id');
$table->integer('parent_id')->unsigned()->default('0');
$table->smallInteger('order_by')->nullable()->default('1');
$table->timestamps();
$table->enum('active', array('a', 'p'));
});
}
public function down()
{
Schema::drop('categories');
}
}

category_names:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateCategoryNamesTable extends Migration {

public function up()
{
Schema::create('category_names', function(Blueprint $table) {
$table->increments('id');
$table->integer('categories_id')->unsigned();
$table->string('language_id');
$table->string('category_slug', 200);
$table->string('category_name', 100);
$table->foreign('categories_id')->references('id')->on('categories')
->onDelete('cascade');
});
}

public function down()
{
Schema::drop('category_names');
}
}

MODELS App/Models/CategoryNames:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class CategoryNames extends Model
{
    use HasFactory;
    protected $table = 'category_names';
    protected $fillable = ['category_name','category_slug'];
    public $timestamps = false;
}

App/Models/Categories:

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Categories extends Model
{
    use HasFactory;
    protected $table = 'categories';
    public $timestamps = true;
    protected $fillable = [
        'ID',
        'parent_id',
        'order_by',
    ];
    public function getCategoryName()
    {
        return $this->hasMany(CategoryNames::class);
    }
}

LIVEWIRE COMPONENTS

app/http/Livewire/admin/CategoriesList:

namespace App\Http\Livewire\Admin;
use Livewire\Component;
use App\Models\Categories;
use App\Models\CategoryNames;

class CategoriesList extends Component
{
    public function render()
    {
        return view('livewire.admin.categories-list',[
            'categories' => Categories::all()->find(1)->getCategoryName
        ]);
    }
}

BLADE FILE views/livewire/admin/categories-list.blade.php

<div class="col-lg-12">
    <br>
    <div class="card">
        <div class="card-body">
            <div class="card-title">
                <h4>Categories</h4>
            </div>
            <hr>
            {{ $categories }}
        </div>
    </div>
</div>

As a result, there is always 1 record. I want all records in the categories table to come. I want it to list the category_name and category_slug fields in the CategoryNames table as well. Thanks.

automica's avatar

@Gondwana Can you reformat your code using three backticks before and after each code block?

1 like
Gondwana's avatar

@webrobert dynamic fields from the database. For example categories. I thought it was necessary for information such as shipping information. Is there a solution for this in localization? I would be glad if

webrobert's avatar

@Gondwana, I’m not certain. Others here may know better. It’s probably worth asking as a new thread the best way. Whether stored in the database or via localization. But I don’t see why you couldnt display shipping details or send them with localization in another language.

In any case, what do you see when you dd $categories.

{{ dd($categories) }}

Please or to participate in this conversation.