WebbieWorks
1 week ago

Trying to get property 'name' of non-object

Posted 1 week ago by WebbieWorks

I'm a bit confused by this one. I have search all around and havent found anything that fixes this.

The reason this is confusing is because it works when calling the category page, but does not work when calling the product page.

line that shows error in UsedController

 $categoryName = $categories->where('slug', request()->category)->first()->name;

UsedController

public function list()
    {

        $products = Product::with('categories')->whereHas('categories', function ($query) {
            $query->where('slug', request()->category);
        })->get();
        $categories = Category::with('children')->where('parent_id',2)->get();      
        $categoryName = $categories->where('slug', request()->category)->first()->name;
        $categoryDesc = $categories->where('slug', request()->category)->first()->description;

        return view('used-equipment/category')->with ([
            'products' => $products,
            'categories' => $categories, 
            'categoryName' => $categoryName,
            'categoryDesc' => $categoryDesc,
          ]);
    }
    
     /**
     * @param  string  $slug
     * @return \Illuminate\Http\Response
     */
    public function show($slug)
    {
        $products = Product::where('slug', $slug)->firstOrFail();
        $mightAlsoLike = Product::where('slug', '!=', $slug)->inRandomOrder()->take(4)->get();

        
        return view('/used-equipment/product')->with([
            'products' => $products,
            'mightAlsoLike' => $mightAlsoLike,
        ]);
    }

product.blade.php

@foreach ($products as $product)
    <div class="row mb3">
        <aside class="col-sm-5 border-right">
            <article class="gallery-wrap"> 
                <div class="img-big-wrap">
                  <div> <a href="#"><img src="https://www.valiantindustriesllc.com/wp-content/uploads/Adco-Cartoner9-400x337.jpg"></a></div>
                </div> <!-- slider-product.// -->
                <div class="img-small-wrap">
                  <div class="item-gallery"> <img src="https://www.valiantindustriesllc.com/wp-content/uploads/CanningLine-4-400x400.jpg"> </div>
                  <div class="item-gallery"> <img src="https://www.valiantindustriesllc.com/wp-content/uploads/CanningLine-4-400x400.jpg"> </div>
                  <div class="item-gallery"> <img src="https://www.valiantindustriesllc.com/wp-content/uploads/CanningLine-4-400x400.jpg"> </div>
                  <div class="item-gallery"> <img src="https://www.valiantindustriesllc.com/wp-content/uploads/CanningLine-4-400x400.jpg"> </div>
                </div> <!-- slider-nav.// -->
            </article> <!-- gallery-wrap .end// -->
        </aside>

        <aside class="col-sm-7">
        <article class="card-body">

            <h1 class="productTitle valRed">{{ $product->name }}</h1>
            <p class="breadcrumbs breads mb-3"><a href="/">Home</a> / <a href="/used-equipment">Used Equipment</a> / <a href="/">Complete Packaging Lines</a></p>
        <hr>
        <dl class="item-property">

          <dd><p>{{ $product->short_description }}</p></dd>
        </dl>
        

            <hr>
            <form action="{{ route('quote.store') }}" method="POST">
            {{ csrf_field() }}
            <input type="hidden" name="id" value="{{ $product->id }}">
            <input type="hidden" name="name" value="{{ $product->name }}">
            <button type="submit" class="btn btn-lg btn-danger text-uppercase">Request Quote</button>
            </form> 
            

        </article> <!-- card-body.// -->
        </aside> <!-- col.// -->
    </div> <!-- row.// -->
    <div class="row section-lg">
       <div class="col-sm-12">
            <h3 class="productTitle valRed">Additional Information</h3>
        </div>    
        <div class="col-sm-12">
        <dd><p>
            {{ $product->long_description }}
          
            </p></dd>
        </div>
    </div>
    @endforeach

web.php

Route::get('/used-equipment', '[email protected]')->name('usedequipment.index');
Route::get('/used-equipment/{category}', '[email protected]')->name('usedequipment.list');
Route::get('/used-equipment/{product}', '[email protected]')->name('usedequipment.show');

But it works fine on the category page.

<div class="prodlistDiv">
                    <div class="row mb-40">
                        <h1 class="valRed">Used {{ $categoryName }} for Sale</h1>
                        <p>{{ $categoryDesc }}</p>
                    </div>
                    
                    @foreach ($products as $product)
                    <div class="row">
                        <div class="col-lg-1" style="padding-left:0">
                            <a href="{{ route('usedequipment.show', $product->slug) }}"><img src="https://www.valiantindustriesllc.com/wp-content/uploads/Whole-line-view-400x400.jpg" width="100px"></a>
                        </div>
                        
                        <div class="col-lg-9" style="padding-left:50px">

                            <h2 class="productTitle valRed"><a href="{{ route('usedequipment.show', $product->slug) }}">{{ $product->name }}</a></h2>
                            <p class="justify paragraph16">{{ $product->short_description }}</p>
                        </div>
                        <div class="col-lg-2" style="padding-left:20px">
                            <div class="snipcart-details top_brand_home_details item_add single-item hvr-outline-out button2">
                                    <a href="{{ route('usedequipment.show', $product->slug) }}">View Details</a>
                                </div>
                        </div>
                        <hr height="10px" width="90%" color="#ffffff" style="border-top: 3px solid rgba(0,0,0,.1);">
                    </div>
                    @endforeach

Please sign in or create an account to participate in this conversation.