TzuSun67's avatar

Trying to get property 'id' of non-object

Hi All,

I am working on a project for an AmpCompare website and I am having issues with my code. I have recently moved to using a different method to call me results as the "Lazy" way I called it before had some issues with mapping id's correctly. I am now using the Load() function to try and load in results which are related to the product.

This is what it should do.

  • Display all comments related to the selected product.

View Page @extends('layouts.app') @section('page_title') Amp Compare | {{$product -> make }} {{ $product -> model }} {{ $product -> watt }} @endsection @section('page_heading')

{{$product -> make }} {{ $product -> model }} {{ $product -> watt }}

@endsection @section('content')

{{$product -> make }} {{ $product -> model }} {{ $product -> watt }}

{{$product -> genre }}

{{$product -> description }}

{{$product -> updated_at }} {{ $product -> star_rating }}

<div class="back-button">
    <p><a class="button" href="/">Back</a></p>
</div>
@if(Auth::check())
    <h4 style="float: right">Create a new Comment <a class="button" href="/comment/{{ $product -> id }}/create/"><ion-icon name="create"></ion-icon></a></h4>
@else
@endif

        @foreach ($product as $c)
            <div><span><h4>{{ $c -> id }}</h4></span></div>
            <div><span><p>{{ $c -> comments }}</p></span></div>
            <div><p>{{ $c -> likes }}<a class="button" href="/comment/{{ $c -> id }}/like/"><ion-icon name="md-heart"></ion-icon></a>
                    <a class="button" href="/comment/{{ $c -> id }}/dislike"><ion-icon name="md-heart-empty"></ion-icon></a></p></div>
            <div>
                @if( Auth::id() ==  $c -> user_id or Auth::user() -> isAdmin() )
                    <div><a class="button" href="/comment/{{ $c -> id }}/edit"><ion-icon name="create"></ion-icon></a></div>
                    <div><a class="button" href="/comment/{{ $c -> id }}/delete"><ion-icon name="trash"></ion-icon></a></div>
                @else
                @endif
            </div>
        @endforeach

@endsection

"There is also pagination on this page however this is also broken so I will fix it after I am able to successfully call the results that I need"

Controller

0 likes
7 replies
Sinnbeck's avatar

Please add ``` on the line before and after your code

Sinnbeck's avatar

You seem to have a mix up in your variables

//here $product is a single instance it seems
href="/comment/{{ $product -> id }}/create/

//here $product is multiple items?
 @foreach ($product as $c)

Show your controller code please

TzuSun67's avatar

Controller

<?php

namespace App\Http\Controllers;

use App\Product;
use App\Comment;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ShowProductController extends Controller
{
    public function __construct()
    {
        $this -> middleware('auth')->except('show');
    }

    const COMMENTS_PER_PAGE = 2;

     public function show (Product $product, Comment $comment){

         $product->load('comments.user');
//         dd($product);
//         $comments = DB::table('comments')
//             -> join ('users','comments.user_id','users.id')
//             -> join ('products','comments.product_id','products.id')
//             -> select ('comments.*','products.*')
//             -> paginate(self::COMMENTS_PER_PAGE);
        return view ('product.show', compact('product'));
//            -> paginate(self::COMMENTS_PER_PAGE);
//         dd($product);
     }
}

Comment Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    protected $fillable = [
        'user_id',
        'product_id',
        'created_at',
        'comments',
        'likes',
    ];

    public function LikeUpVote ()
    {
        $this -> likes += 1;
        $this -> update ();
    }

    public function LikeDownVote ()
    {
        $this -> likes -= 1;
        $this -> update ();
    }

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

Product Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [
        'make',
        'model',
        'watt',
        'genre',
        'updated_at',
        'description',
        'star_rating',
        'image',
    ];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

Didn't know about the ``` sorry about that.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

No worries.

Change this

@foreach ($product->comments as $c)
TzuSun67's avatar

Thank you Sinnback. Had no idea it was that simple to fix didn't know you could pass things into foreach loops like that.

Thanks Again, Ben

Please or to participate in this conversation.