Skip to content
Advertisement

Trying to get property ‘name’ of non-object laravel 8 and livewire

I am getting the error when I try to edit products in my eCommerce project. I can not find where i doing the mistake. when i am trying to edit product it give me the error. i am begginer to laravel 8. i can not find where to look for as it shows me the declared variables list.

my class file

<?php

namespace AppHttpLivewireAdmin;

use LivewireComponent;
use AppModelsCategory;
use AppModelsProduct;
use illuminateSupportStr;
use LivewireWithFileUploads;
use CarbonCarbon; 

class AdminEditProductComponent extends Component
{
    
    public $name;
    public $slug;
    public $short_description;
    public $description;
    public $regular_price;
    public $sale_price;
    public $SKU;
    public $stock_status;
    public $featured;
    public $quantity;
    public $image;
    public $category_id;
    public $newimage;
    public $product_id;
    use WithFileUploads;

    public function mount($product_slug) 
    {
        $product = Product::where('slug',$product_slug)->first();
        $this->name = $product->name;
        $this->slug = $product->slug;
        $this->short_description = $product->short_description;
        $this->description = $product->description;
        $this->regular_price = $product->regular_price;
        $this->sale_price = $product->sale_price;
        $this->SKU = $product->SKU;
        $this->stock_status = $product->stock_status;
        $this->featured = $product->featured;
        $this->quantity = $product->quantity;
        $this->image = $product->image;
        $this->category_id = $product->category_id;
        $this->product_id = $product->id;

    }
   

Advertisement

Answer

yes.query returning array or object? If you dump it out, you might find that it’s an array and all you need is an array access ([]) instead of an object access (->). product is not null and I found the answer. i changed -> to [] and it worked.

$product = Product::where('slug',$product_slug)->first();
    $this->name = $product['name'];
    $this->slug = $product['slug'];
    $this->short_description = $product['short_description'];
    $this->description = $product['description'];
    $this->regular_price = $product['regular_price'];
    $this->sale_price = $product['sale_price'];
    $this->SKU = $product['SKU'];
    $this->stock_status = $product['stock_status'];
    $this->featured = $product['featured'];
    $this->quantity = $product['quantity'];
    $this->image = $product['image'];
    $this->category_id = $product['category_id'];
    $this->product_id = $product['id'];
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement