Skip to content
Advertisement

Sql select * from `categories` inner join `category_product` on `categories`.`id`

Look at my codes

products migration

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('slug')->unique();
        $table->string('short_description')->nullable();
        $table->text('description');
        $table->decimal('regular_price');
        $table->decimal('sale_price')->nullable();
        $table->string('SKU');
        $table->enum('stock_status', ['instock', 'outofstock']);
        $table->boolean('featured')->default(false);
        $table->unsignedInteger('quantity')->default(10);
        $table->string('image')->nullable();
        $table->text('images')->nullable();
        $table->timestamps();
    });

    Schema::create('category_product', function (Blueprint $table) {
        $table->bigInteger('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

        $table->bigInteger('product_id')->unsigned();
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

        $table->primary(['category_id', 'product_id']);
    });
}

web.php

Route::get('/product/{slug}', DetailsComponent::class)->name('product.details');

DetailsComponent.php

<?php

namespace AppHttpLivewire;

use AppModelsProduct;
use LivewireComponent;

class DetailsComponent extends Component
{
    public $slug;

    public function mount($slug)
    {
        $this->slug = $slug;
    }
    public function render()
    {
        $product = Product::where('slug', $this->slug)->first();
        $popular_products = Product::with('categories')->inRandomOrder()->limit(4)->get();
        $related_products = Product::with('categories')
            ->whereHas('categories')
            ->where('category_id', $product->category_id)
            ->inRandomOrder()->limit(5)->get();
        return view('livewire.details-component', compact('product', 'popular_products', 'related_products'))->layout('Home.master');
    }
}

Product.php

public function categories()
{
    return $this->belongsToMany(Category::class);
}

details-component.blade.php

@foreach($related_products as $related_product)
    <div class="product product-style-2 equal-elem ">
        <div class="product-thumnail">
            <a href="{{ route('product.details', ['slug' => $popular_product->slug]) }}" title="{{ $related_product->name }}">
                <figure><img src="{{ asset('assets/images/products/'.$related_product->image) }}" width="214" height="214" alt="{{ $related_product->name }}"></figure>
            </a>
            <div class="group-flash">
                <span class="flash-item new-label">new</span>
            </div>
            <div class="wrap-btn">
                <a href="#" class="function-link">quick view</a>
            </div>
        </div>
        <div class="product-info">
            <a href="{{ route('product.details', ['slug' => $related_product->slug]) }}" class="product-name"><span>{{ $related_product->name }}</span></a>
            <div class="wrap-price"><span class="product-price">${{ $related_product->regular_price }}</span></div>
        </div>
    </div>
@endforeach

I am trying to get specific data from the database by using column category_id when a user clicks a link but I am getting this error:

I am showing categories for a product. I want to create a category manager section i have created the category manager but while i am submitting the form i am getting getting the errors as follows:

error

Advertisement

Answer

Check on this line:

    public function render()
    {
        $product = Product::where('slug', $this->slug)->first();

        $popular_products = Product::with('categories')
                            ->inRandomOrder()
                            ->limit(4)
                             ->get();

        $related_products = Product::with('categories')
            ->whereHas('categories', function ($q) use ($product) {
                  $q->where('category_id', $product->category_id)
             })
             ->inRandomOrder()
             ->limit(5)
             ->get();

        return view('livewire.details-component', compact('product', 'popular_products', 'related_products'))->layout('Home.master');
    }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement