Call to undefined method AppTag::getChildrenAttribute() – Livewire Laravel

  laravel, laravel-livewire, model, php, voyager

My application was working good but when I installed Livewire, I’m getting error in breadcrumbs like this.

Call to undefined method AppTag::getChildrenAttribute() (View: D:Websiteecommerceresourcesviewslayoutsratings.blade.php)

ShopController

public function show($slug) {
        $tag = Tag::where('slug', request()->tag)->get()->first();
        $tags = Tag::where('slug', request()->tags)->get()->first();

        $product = Product::where('slug', $slug)->firstOrFail();
        return view('layouts.product')->with([
            'product' => $product,
            'tags' => $tags,
            'tag'   => $tag,
        ]);
}

Livewire/ProductRatings.php

class ProductRatings extends Component
{
    public $rating;
    public $comment;
    public $currentId;
    public $product;
    public $hideForm;

    protected $rules = [
        'rating' => ['required', 'in:1,2,3,4,5'],
        'comment' => 'required',

    ];

    public function render()
    {
        $comments = Rating::where('product_id', $this->product->id)->where('status', 1)->with('user')->get();
        return view('livewire.product-ratings', compact('comments'));
    }

    public function mount()
    {
        if (auth()->user()) {
            $rating = Rating::where('user_id', auth()->user()->id)->where('product_id', $this->product->id)->first();
            if (!empty($rating)) {
                $this->rating  = $rating->rating;
                $this->comment = $rating->comment;
                $this->currentId = $rating->id;
            }
        }
        return view('livewire.product-ratings');
    }

    public function delete($id)
    {
        $rating = Rating::where('id', $id)->first();
        if ($rating && ($rating->user_id == auth()->user()->id)) {
            $rating->delete();
        }
        if ($this->currentId) {
            $this->currentId = '';
            $this->rating  = '';
            $this->comment = '';
        }
    }

    public function rate()
    {
        $rating = Rating::where('user_id', auth()->user()->id)->where('product_id', $this->product->id)->first();
        $this->validate();
        if (!empty($rating)) {
            $rating->user_id = auth()->user()->id;
            $rating->product_id = $this->product->id;
            $rating->rating = $this->rating;
            $rating->comment = $this->comment;
            $rating->status = 1;
            try {
                $rating->update();
            } catch (Throwable $th) {
                throw $th;
            }
            session()->flash('message', 'Success!');
        } else {
            $rating = new Rating;
            $rating->user_id = auth()->user()->id;
            $rating->product_id = $this->product->id;
            $rating->rating = $this->rating;
            $rating->comment = $this->comment;
            $rating->status = 1;
            try {
                $rating->save();
            } catch (Throwable $th) {
                throw $th;
            }
            $this->hideForm = true;
        }
    }
}

Product Model

public function parent()
    {
        return $this->belongsTo(ProductTag::class);
    }

    public function tags() {
        return $this->belongsToMany(Tag::class);
    }

Tag Model

public function products() {
        return $this->belongsToMany(Product::class);
    }
public function parent()
    {
        return $this->belongsTo(Tag::class, 'parent_id');
    }
    
    public function children()
    {
       return $this->hasMany(Tag::class, 'parent_id', 'id')->with('children');
    }

Rating Model

public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function products()
    {
        return $this->belongsTo(Product::class);
    }

View

@foreach($product->tags->take(1) as $tag)
  <li><a href="{{ route('shop.parent', ['tag' => $tag->parent->slug]) }}">{{$tag->parent->name}}</a></li>
@endforeach
@foreach($product->tags as $tag)
  <li><a href="{{ route('shop.index', ['tag' => $tag->parent->slug, 'tags' => $tag->slug]) }}">{{$tag->name}}</a></li>
@endforeach

I can’t get access to $product->tags when livewire is installed. Otherwise everything is working ok.

Source: Ask PHP

LEAVE A COMMENT