ChristianKuri / laravel-favorite

Allows Laravel Eloquent models to implement a 'favorite', 'like', 'remember' and 'follow' features.
MIT License
226 stars 48 forks source link

Trying to get property of non-object (View: D:\xampp\htdocs\2018\project\resources\views\backend\articles\index.blade.php) #8

Closed ghost closed 6 years ago

ghost commented 6 years ago

This is how my controller code looks like

public function index()
  {
    $userid = Auth::user()->id;
    $user = Auth::user();
    $favoritevideos = $user->favorite(Video::class);
    $videos = Video::where('created_by', $userid)->latest()->paginate(12);
    return view('backend.videos.index', compact('videos', 'favoritevideos'));
  }

And in my view when i do this

@forelse ($favoritearticles as $article)
                      <div class="col-md-4">
                        <div class="item r-t">
                          <div class="item-media item-media-16by9">
                            <a href="{{ url('articles/'.$article->category->slug.'/'.$article->slug) }}" class="item-media-content" style="background-image: url({{ asset('uploads/articles/'.$article->image) }})"></a>
                          </div>
                          <div class="item-overlay active top p-3">
                            <a href="{{ url('articles/'.$article->category->slug) }}" class="text-u-c badge">{{ $article->category->name }}</a>
                          </div>
                        </div>
                        <div class="p-2 border">
                          <div class="mb-2 h-2x">
                            <a href="{{ url('articles/'.$article->category->slug.'/'.$article->slug) }}" class="_800">{{ $article->title }}</a>
                          </div>
                          <span class="mr-2"><i class="fa fa-eye"></i> {{ $article->getPageViews() }} views</span> <a href="#" class="mr-2"><i class="fa fa-heart-o"></i> {{ $article->favoritesCount }}</a> likes
                        </div>
                      </div>
                    @empty
                      <div class="padding">
                          <span>You have not liked any articles.</span>
                      </div>
                    @endforelse

I see the error Trying to get property of non-object (View: D:\xampp\htdocs\2018\project\resources\views\backend\articles\index.blade.php)

All the tables are currently empty. When i add an article and favorite it via tinker the error is gone. When i move the project to the live production server initially the project would be empty and it would show error to my client. How to solve this?

Your environment

Environment : Localhost XAMPP Laravel 5.6 Php 7.1 Windows 7

ChristianKuri commented 6 years ago

can you dd() $favoritearticles when the table is empty and post it?

Also the issue is on another controller since that one is about videos and the correct one is about articles