eldringoki

eldringoki

Member Since 3 Years Ago

Experience Points 425
Experience Level 1

4,575 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed 0
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

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

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

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

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    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 Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

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

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    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.

$user = \Auth::user();
$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

<?php


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

its in

$test = $cart->id;

How the hell does $cart lose its value? Running this code I still get the value of ID from point a but disappears when on point B

echo $cart->id; //point A

$test = $cart->id; //point B where it doesnt have a value anymore

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.