eloquent relationship throws Trying to get property of non-object

Posted 3 years ago by eldringoki

Hi. I am stuck with this problem for some time now. here is my code and the error always points at the line with $cart->id;

its weird cause the echo works showing the id value but still throws a trying to get property of non object.

foreach($books as $book){

        $cart = $book->cart()->first();
        
        echo $cart->id;

        if($cart->id == $id)
        { 
            $text = "added";
        }
        else
        {
            $text = "not added";
        }

        //$text = ($boolean) ? 'added' : 'not_added';            
        $book->button_text = $text;    
    }

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

Reply to

Use Markdown with GitHub-flavored code blocks.