eldringoki

Experience

425

0 Best Reply Awards

  • Member Since 2 Years Ago
  • 0 Lessons Completed
  • 0 Favorites

26th October, 2015

eldringoki left a reply on Laravel Relationship Gets Different Eloquent Model With And Without First() Function • 2 years ago

checking back my User model relationship I forgot I added a ->first() in the return statement. so that means nesting first() functions give different results?

eldringoki left a reply on Laravel Relationship Gets Different Eloquent Model With And Without First() Function • 2 years ago

I think im good. they both return a cart instance

eldringoki started a new conversation Laravel Relationship Gets Different Eloquent Model With And Without First() Function • 2 years ago

I have these 2 models user and cart implemented with 1 to 1 relationship.

   $cart = $user->cart()->first(); // returns a wrong record of cart 
   $cart2 = $user->cart(); // returns the right one
  
   dd($cart,$cart2);
this is the screenshot of the dumped objects
http://imgur.com/mwu6kuK

24th October, 2015

eldringoki left a reply on Eloquent Relationship Throws Trying To Get Property Of Non-object • 2 years ago

@mstnorris

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model { protected $table = "books"; protected $fillable = [ 'title', 'author', 'unique', 'price', 'publish', 'description'];

public function cart()
{
    return $this->belongsToMany('App\Cart','cart_books');
}

public function in_user_cart()
{   //this function is the same with the shown controller
    $response = ($this->cart()->first()->user->id == \Auth::user()->id) ? 'true' : 'false';
    //dd($response); // this also gives off an error
    return $response;
}

}

class Cart extends Model { // protected $table = "cart";
protected $fillable = ['user_id'];

public function books() { return $this->belongsToMany('App\Book','cart_books'); }

}

eldringoki left a reply on Eloquent Relationship Throws Trying To Get Property Of Non-object • 2 years ago

$test = $cart->id;

eldringoki left a reply on Eloquent Relationship Throws Trying To Get Property Of Non-object • 2 years ago

@mstnorris heres the screen grab http://imgur.com/nsnsnPa as for my code. This is the function.

public function index() { $books = Book::all();

$id = Auth::user()->cart()->first()->id;   
        
    foreach($books as $book){

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

}

sorry if my code looks messy. I was trying to break down the flow

eldringoki left a reply on Eloquent Relationship Throws Trying To Get Property Of Non-object • 2 years ago

@Snapey It fails on the first loop. I dd'ed the cart->id and it shows the value. but when the error is triggered I see in the stack trace that 'cart' => null

eldringoki left a reply on Eloquent Relationship Throws Trying To Get Property Of Non-object • 2 years ago

@mstnorris I've isolated that part and I am still not using it in a view. I remove the button_text part and still the error shows up. i've isolated it into this part foreach($books as $book){

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

    $test = $cart->id > 0 ? 'true' : 'false'; //error here

} Reading the stack trace I also find that 'cart' => null.. which makes it more weird..

And I am also using many to many relationship with pivot table for this.

eldringoki left a reply on Eloquent Relationship Throws Trying To Get Property Of Non-object • 2 years ago

@mstnorris book doesn't have a button_text attribute but I dont plan to save it that way. I just want button_text to hitch a ride for the book object. is that not allowed?

eldringoki started a new conversation Eloquent Relationship Throws Trying To Get Property Of Non-object • 2 years ago

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;    
    }

eldringoki left a reply on Many To Many Relation With Unique Records In Pivot • 2 years ago

@GiantCowFilms

Filter user requests. Make use of "validators" to validate user inputs and apply various useful rules to it like uniqueness to a certain table and require a certain field to be numeric and such. You can also customize error messages returned from failed validations. Just make sure you control what inputs goes into your database.

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.