0

Всем привет, пишу на Laravel неделю, я новичок! Связался с ошибкой "Attempt to read property "id" on null". Помогите решить Пожалуйста!!! По идеи нужно правильно провести путь, но я пытался, ничего не вышло!

Cheme

<?php

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

return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('categories', function (Blueprint $table) { $table->id(); $table->string('name')->unique(); $table->string('slug')->unique(); $table->timestamps(); }); }

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('categories');
}

};

CategoryComponent

<?php

namespace App\Http\Livewire;

use App\Models\Product; use Livewire\Component; use Livewire\WithPagination; use Cart; use App\Models\Category;

class CategoryComponent extends Component {

public $sorting;
public $pagesize;
public $category_slug;


public function mount($category_slug)
{
    $this-&gt;sorting = &quot;default&quot;;
    $this-&gt;pagesize = 12;
    $this-&gt;category_slug = $category_slug;
}
public function store($product_id,$product_name,$product_price)
{
    Cart::add($product_id,$product_name,1,$product_price)-&gt;associate('App\Models\Product');
    session()-&gt;flash('success_message','Item added in Cart');
    return redirect()-&gt;route('product.cart');
}
use WithPagination;
public function render()
{
    $category = Category::where('slug',$this-&gt;category_slug)-&gt;first();
    $category_id = $category-&gt;id;
    $category_name = $category-&gt;name;
    if($this-&gt;sorting=='data')
    {
        $products = Product::where('category_id',$category_id)-&gt;orderBy('created_at','DESC')-&gt;paginate($this-&gt;pagesize);
    }
    else if ($this-&gt;sorting=='price')
    {
        $products = Product::where('category_id',$category_id)-&gt;orderBy('regular_price','ASC')-&gt;paginate($this-&gt;pagesize);
    }
    else if ($this-&gt;sorting=='price-desc')
    {
        $products = Product::where('category_id',$category_id)-&gt;orderBy('regular_price','DESC')-&gt;paginate($this-&gt;pagesize);
    }
    else
    {
        $products = Product::where('category_id',$category_id)-&gt;paginate($this-&gt;pagesize);
    }
    $categories = Category::all();
    $products = Product::inRandomOrder()-&gt;limit(4)-&gt;get();

    return view('livewire.category-component',['products'=&gt;$products,'categories'=&gt;$categories,'category_name'=&gt;$category_name])-&gt;layout(&quot;layouts.base&quot;);
}

}

HTML

    <main id="main" class="main-site left-sidebar">
    &lt;div class=&quot;container&quot;&gt;

        &lt;div class=&quot;wrap-breadcrumb&quot;&gt;
            &lt;ul&gt;
                &lt;li class=&quot;item-link&quot;&gt;&lt;a href=&quot;/&quot; class=&quot;link&quot;&gt;home&lt;/a&gt;&lt;/li&gt;
                &lt;li class=&quot;item-link&quot;&gt;&lt;span&gt;Product Categories&lt;/span&gt;&lt;/li&gt;
                &lt;li class=&quot;item-link&quot;&gt;&lt;span&gt;{{$category_name}}&lt;/span&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/div&gt;
        &lt;div class=&quot;row&quot;&gt;

            &lt;div class=&quot;col-lg-9 col-md-8 col-sm-8 col-xs-12 main-content-area&quot;&gt;

                &lt;div class=&quot;banner-shop&quot;&gt;
                    &lt;a href=&quot;#&quot; class=&quot;banner-link&quot;&gt;
                        &lt;figure&gt;&lt;img src=&quot;{{ asset('assets/images/shop-banner.jpg') }}&quot; alt=&quot;&quot;&gt;&lt;/figure&gt;
                    &lt;/a&gt;
                &lt;/div&gt;

                &lt;div class=&quot;wrap-shop-control&quot;&gt;

                    &lt;h1 class=&quot;shop-title&quot;&gt;{{$category_name}}&lt;/h1&gt;

                    &lt;div class=&quot;wrap-right&quot;&gt;

                        &lt;div class=&quot;sort-item orderby &quot;&gt;
                            &lt;select name=&quot;orderby&quot; class=&quot;use-chosen&quot; wire:model=&quot;sorting&quot;&gt;
                                &lt;option value=&quot;default&quot; selected=&quot;selected&quot;&gt;Default sorting&lt;/option&gt;
                                &lt;option value=&quot;date&quot;&gt;Sort by newness&lt;/option&gt;
                                &lt;option value=&quot;price&quot;&gt;Sort by price: low to high&lt;/option&gt;
                                &lt;option value=&quot;price-desc&quot;&gt;Sort by price: high to low&lt;/option&gt;
                            &lt;/select&gt;
                        &lt;/div&gt;

                        &lt;div class=&quot;sort-item product-per-page&quot;&gt;
                            &lt;select name=&quot;post-per-page&quot; class=&quot;use-chosen&quot; wire:model=&quot;pagesize&quot;&gt;
                                &lt;option value=&quot;12&quot; selected=&quot;selected&quot;&gt;12 per page&lt;/option&gt;
                                &lt;option value=&quot;16&quot;&gt;16 per page&lt;/option&gt;
                                &lt;option value=&quot;18&quot;&gt;18 per page&lt;/option&gt;
                                &lt;option value=&quot;21&quot;&gt;21 per page&lt;/option&gt;
                                &lt;option value=&quot;24&quot;&gt;24 per page&lt;/option&gt;
                                &lt;option value=&quot;30&quot;&gt;30 per page&lt;/option&gt;
                                &lt;option value=&quot;32&quot;&gt;32 per page&lt;/option&gt;
                            &lt;/select&gt;
                        &lt;/div&gt;
                        &lt;div class=&quot;change-display-mode&quot;&gt;
                            &lt;a href=&quot;#&quot; class=&quot;grid-mode display-mode active&quot;&gt;&lt;i class=&quot;fa fa-th&quot;&gt;&lt;/i&gt;Grid&lt;/a&gt;
                            &lt;a href=&quot;list.html&quot; class=&quot;list-mode display-mode&quot;&gt;&lt;i class=&quot;fa fa-th-list&quot;&gt;&lt;/i&gt;List&lt;/a&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;&lt;!--end wrap shop control--&gt;
                &lt;div class=&quot;row&quot;&gt;
                    &lt;ul class=&quot;product-list grid-products equal-container&quot;&gt;
                        @foreach ($products as $product)

                        &lt;li class=&quot;col-lg-4 col-md-6 col-sm-6 col-xs-6 &quot;&gt;
                            &lt;div class=&quot;product product-style-3 equal-elem &quot;&gt;
                                &lt;div class=&quot;product-thumnail&quot;&gt;
                                    &lt;a href=&quot;{{route('product.details',['slug'=&gt;$product-&gt;slug])}}&quot; title=&quot;T-Shirt Raw Hem Organic Boro Constrast Denim&quot;&gt;
                                        &lt;figure&gt;&lt;img src=&quot;{{ asset('assets/images/products') }}/{{$product-&gt;image}}&quot; alt=&quot;{{$product-&gt;name}}&quot;&gt;&lt;/figure&gt;
                                    &lt;/a&gt;
                                &lt;/div&gt;
                                &lt;div class=&quot;product-info&quot;&gt;
                                    &lt;a href=&quot;{{route('product.details',['slug'=&gt;$product-&gt;slug])}}&quot; class=&quot;product-name&quot;&gt;&lt;span&gt;{{$product-&gt;name}}&lt;/span&gt;&lt;/a&gt;
                                    &lt;div class=&quot;wrap-price&quot;&gt;&lt;span class=&quot;product-price&quot;&gt;{{$product-&gt;regular_price}}&lt;/span&gt;&lt;/div&gt;
                                    &lt;a href=&quot;#&quot; class=&quot;btn add-to-cart&quot; wire:click.prevent=&quot;store({{$product-&gt;id}},'{{$product-&gt;name}}',{{$product-&gt;regular_price}})&quot;&gt;Add To Cart&lt;/a&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;
                        &lt;/li&gt;
                        @endforeach
                    &lt;/ul&gt;
                &lt;/div&gt;
                &lt;div class=&quot;wrap-pagination-info&quot;&gt;
                    {{$products-&gt;links()}}
                    {{-- &lt;ul class=&quot;page-numbers&quot;&gt;
                        &lt;li&gt;&lt;span class=&quot;page-number-item current&quot; &gt;1&lt;/span&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;a class=&quot;page-number-item&quot; href=&quot;#&quot; &gt;2&lt;/a&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;a class=&quot;page-number-item&quot; href=&quot;#&quot; &gt;3&lt;/a&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;a class=&quot;page-number-item next-link&quot; href=&quot;#&quot; &gt;Next&lt;/a&gt;&lt;/li&gt;
                    &lt;/ul&gt; --}}
                    &lt;p class=&quot;result-count&quot;&gt;Showing 1-8 of 12 result&lt;/p&gt;
                &lt;/div&gt;
            &lt;/div&gt;&lt;!--end main products area--&gt;

            &lt;div class=&quot;col-lg-3 col-md-4 col-sm-4 col-xs-12 sitebar&quot;&gt;
                &lt;div class=&quot;widget mercado-widget categories-widget&quot;&gt;
                    &lt;h2 class=&quot;widget-title&quot;&gt;All Categories&lt;/h2&gt;
                    &lt;div class=&quot;widget-content&quot;&gt;
                        &lt;ul class=&quot;list-category&quot;&gt;
                        @foreach($categories as $category)
                            &lt;li class=&quot;category-item&quot;&gt;
                                &lt;a href=&quot;{{route('product-category',['category_slug'=&gt;$category-&gt;slug])}}&quot; class=&quot;cate-link&quot;&gt;{{$category-&gt;name}}&lt;/a&gt;
                            &lt;/li&gt;
                        @endforeach
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/div&gt;&lt;!-- Categories widget--&gt;

                &lt;div class=&quot;widget mercado-widget filter-widget brand-widget&quot;&gt;
                    &lt;h2 class=&quot;widget-title&quot;&gt;Brand&lt;/h2&gt;
                    &lt;div class=&quot;widget-content&quot;&gt;
                        &lt;ul class=&quot;list-style vertical-list list-limited&quot; data-show=&quot;6&quot;&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link active&quot; href=&quot;#&quot;&gt;Fashion Clothings&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Laptop Batteries&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Printer &amp; Ink&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;CPUs &amp; Prosecsors&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Sound &amp; Speaker&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Shop Smartphone &amp; Tablets&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item default-hiden&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Printer &amp; Ink&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item default-hiden&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;CPUs &amp; Prosecsors&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item default-hiden&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Sound &amp; Speaker&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item default-hiden&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Shop Smartphone &amp; Tablets&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a data-label='Show less&lt;i class=&quot;fa fa-angle-up&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;' class=&quot;btn-control control-show-more&quot; href=&quot;#&quot;&gt;Show more&lt;i class=&quot;fa fa-angle-down&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/div&gt;&lt;!-- brand widget--&gt;

                &lt;div class=&quot;widget mercado-widget filter-widget price-filter&quot;&gt;
                    &lt;h2 class=&quot;widget-title&quot;&gt;Price&lt;/h2&gt;
                    &lt;div class=&quot;widget-content&quot;&gt;
                        &lt;div id=&quot;slider-range&quot;&gt;&lt;/div&gt;
                        &lt;p&gt;
                            &lt;label for=&quot;amount&quot;&gt;Price:&lt;/label&gt;
                            &lt;input type=&quot;text&quot; id=&quot;amount&quot; readonly&gt;
                            &lt;button class=&quot;filter-submit&quot;&gt;Filter&lt;/button&gt;
                        &lt;/p&gt;
                    &lt;/div&gt;
                &lt;/div&gt;&lt;!-- Price--&gt;

                &lt;div class=&quot;widget mercado-widget filter-widget&quot;&gt;
                    &lt;h2 class=&quot;widget-title&quot;&gt;Color&lt;/h2&gt;
                    &lt;div class=&quot;widget-content&quot;&gt;
                        &lt;ul class=&quot;list-style vertical-list has-count-index&quot;&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Red &lt;span&gt;(217)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Yellow &lt;span&gt;(179)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Black &lt;span&gt;(79)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Blue &lt;span&gt;(283)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Grey &lt;span&gt;(116)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;Pink &lt;span&gt;(29)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/div&gt;&lt;!-- Color --&gt;

                &lt;div class=&quot;widget mercado-widget filter-widget&quot;&gt;
                    &lt;h2 class=&quot;widget-title&quot;&gt;Size&lt;/h2&gt;
                    &lt;div class=&quot;widget-content&quot;&gt;
                        &lt;ul class=&quot;list-style inline-round &quot;&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link active&quot; href=&quot;#&quot;&gt;s&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;M&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;l&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=&quot;list-item&quot;&gt;&lt;a class=&quot;filter-link &quot; href=&quot;#&quot;&gt;xl&lt;/a&gt;&lt;/li&gt;
                        &lt;/ul&gt;
                        &lt;div class=&quot;widget-banner&quot;&gt;
                            &lt;figure&gt;&lt;img src=&quot;{{ asset('assets/images/size-banner-widget.jpg') }}&quot; width=&quot;270&quot; height=&quot;331&quot; alt=&quot;&quot;&gt;&lt;/figure&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;&lt;!-- Size --&gt;

                &lt;div class=&quot;widget mercado-widget widget-product&quot;&gt;
                    &lt;h2 class=&quot;widget-title&quot;&gt;Popular Products&lt;/h2&gt;
                    &lt;div class=&quot;widget-content&quot;&gt;
                        &lt;ul class=&quot;products&quot;&gt;
                            &lt;li class=&quot;product-item&quot;&gt;
                                &lt;div class=&quot;product product-widget-style&quot;&gt;
                                    &lt;div class=&quot;thumbnnail&quot;&gt;
                                        &lt;a href=&quot;{{route('product.details',['slug'=&gt;$product-&gt;slug])}}&quot; title=&quot;Radiant-360 R6 Wireless Omnidirectional Speaker [White]&quot;&gt;
                                            &lt;figure&gt;&lt;img src=&quot;{{ asset('assets/images/products/digital_01.jpg') }}&quot; alt=&quot;&quot;&gt;&lt;/figure&gt;
                                        &lt;/a&gt;
                                    &lt;/div&gt;
                                    &lt;div class=&quot;product-info&quot;&gt;
                                        &lt;a href=&quot;#&quot; class=&quot;product-name&quot;&gt;&lt;span&gt;Radiant-360 R6 Wireless Omnidirectional Speaker...&lt;/span&gt;&lt;/a&gt;
                                        &lt;div class=&quot;wrap-price&quot;&gt;&lt;span class=&quot;product-price&quot;&gt;$168.00&lt;/span&gt;&lt;/div&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/li&gt;

                            &lt;li class=&quot;product-item&quot;&gt;
                                &lt;div class=&quot;product product-widget-style&quot;&gt;
                                    &lt;div class=&quot;thumbnnail&quot;&gt;
                                        &lt;a href=&quot;{{route('product.details',['slug'=&gt;$product-&gt;slug])}}&quot; title=&quot;Radiant-360 R6 Wireless Omnidirectional Speaker [White]&quot;&gt;
                                            &lt;figure&gt;&lt;img src=&quot;{{ asset('assets/images/products/digital_17.jpg') }}&quot; alt=&quot;&quot;&gt;&lt;/figure&gt;
                                        &lt;/a&gt;
                                    &lt;/div&gt;
                                    &lt;div class=&quot;product-info&quot;&gt;
                                        &lt;a href=&quot;#&quot; class=&quot;product-name&quot;&gt;&lt;span&gt;Radiant-360 R6 Wireless Omnidirectional Speaker...&lt;/span&gt;&lt;/a&gt;
                                        &lt;div class=&quot;wrap-price&quot;&gt;&lt;span class=&quot;product-price&quot;&gt;$168.00&lt;/span&gt;&lt;/div&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/li&gt;

                            &lt;li class=&quot;product-item&quot;&gt;
                                &lt;div class=&quot;product product-widget-style&quot;&gt;
                                    &lt;div class=&quot;thumbnnail&quot;&gt;
                                        &lt;a href=&quot;{{route('product.details',['slug'=&gt;$product-&gt;slug])}}&quot; title=&quot;Radiant-360 R6 Wireless Omnidirectional Speaker [White]&quot;&gt;
                                            &lt;figure&gt;&lt;img src=&quot;{{ asset('assets/images/products/digital_18.jpg') }}&quot; alt=&quot;&quot;&gt;&lt;/figure&gt;
                                        &lt;/a&gt;
                                    &lt;/div&gt;
                                    &lt;div class=&quot;product-info&quot;&gt;
                                        &lt;a href=&quot;#&quot; class=&quot;product-name&quot;&gt;&lt;span&gt;Radiant-360 R6 Wireless Omnidirectional Speaker...&lt;/span&gt;&lt;/a&gt;
                                        &lt;div class=&quot;wrap-price&quot;&gt;&lt;span class=&quot;product-price&quot;&gt;$168.00&lt;/span&gt;&lt;/div&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/li&gt;

                            &lt;li class=&quot;product-item&quot;&gt;
                                &lt;div class=&quot;product product-widget-style&quot;&gt;
                                    &lt;div class=&quot;thumbnnail&quot;&gt;
                                        &lt;a href=&quot;{{route('product.details',['slug'=&gt;$product-&gt;slug])}}&quot; title=&quot;Radiant-360 R6 Wireless Omnidirectional Speaker [White]&quot;&gt;
                                            &lt;figure&gt;&lt;img src=&quot;{{ asset('assets/images/products/digital_20.jpg') }}&quot; alt=&quot;&quot;&gt;&lt;/figure&gt;
                                        &lt;/a&gt;
                                    &lt;/div&gt;
                                    &lt;div class=&quot;product-info&quot;&gt;
                                        &lt;a href=&quot;#&quot; class=&quot;product-name&quot;&gt;&lt;span&gt;Radiant-360 R6 Wireless Omnidirectional Speaker...&lt;/span&gt;&lt;/a&gt;
                                        &lt;div class=&quot;wrap-price&quot;&gt;&lt;span class=&quot;product-price&quot;&gt;$168.00&lt;/span&gt;&lt;/div&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/li&gt;

                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/div&gt;&lt;!-- brand widget--&gt;

            &lt;/div&gt;&lt;!--end sitebar--&gt;

        &lt;/div&gt;&lt;!--end row--&gt;

    &lt;/div&gt;&lt;!--end container--&gt;

&lt;/main&gt;
&lt;!--main area--&gt;


  [1]: https://i.stack.imgur.com/ptrbF.png
</code></pre>
quelle
  • 1
  • см. как отладить код и номер строки.. ↑↑↑ ошибка говорит что ты пытаешься обратиться к свойству объекта, а сам объект null – Алексей Шиманский Jun 01 '22 at 17:13
  • У тебя сортировка по продуктам никогда не будет работать, зачем ты пишешь получение продуктов в ифах, если потом все равно получаешь другие продукты?

  • Эта ошибка возникает когда ты пытаешься получить какое-то свойства у переменной null и скорее всего это происходит в категориях когда нет категории с таким slug

  • $category_id = $category->id;

    – Ricco381 Jun 01 '22 at 18:00
  • @Ricco381 https://youtu.be/LJ5H3ec3mrU 11:23, но у него же так написано и всё работает! – quelle Jun 01 '22 at 18:12
  • @quelle у него написано не так, пересмотри еще раз видео только внимательно
    $products = Product::inRandomOrder()->limit(4)->get(); эта строчка делает твои ифы бесполезным кодом!
    – Ricco381 Jun 01 '22 at 18:21