eldringoki

eldringoki

Member Since 3 Years Ago

Experience Points 425
Experience
Level
Lessons Completed 0
Lessons
Completed
Best Reply Awards 0
Best Answer
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • Chatty Cathy Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

26 Oct
3 years ago

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

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 started a new conversation Laravel Relationship Gets Different Eloquent Model With And Without First() Function

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

24 Oct
3 years ago

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

@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

@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

@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

@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

@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

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

@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.