guyinpv

guyinpv

Web Developer at Self employed

Member Since 8 Months Ago

Experience Points
290
Total
Experience

4,710 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
1
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your 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-in-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 Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist 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.

Level 1
290 XP
Sep
23
1 month ago
Activity icon

Replied to HasOneThrough Confusion

Correct, in my case it's:

User: return $this->belongsTo(Car::class);

Car: return $this->belongsTo(Maker::class);

The idea is that a user has one car and thus also one maker, but a maker could belong to many cars, and the same car could belong to many users. Just like a user can own a Honda Accord, but other users might also own Honda Accords. And an Accord is only made by Honda. So the user could only have the maker of Honda as well. I want to grab these in reverse too. If I have Honda, I want all the users. If I have Accord, I want all the users too.

I am perfectly able to get all the users who own Hondas:

Maker: return $this->hasManyThrough(User::class, Car::class, 'maker_id', 'car:id');

So in your example, in my User model if I want to directly grab their maker, this is what I ended up having to do yesterday.

User:

public function maker()
{
  if ($this->car) {
    return $this->car->maker->first();
  } else {
    return null;
  }
}

I'm using the car() method on the user model to first check if they even have a car at all, and only then use the maker() method on the car() model to pull the data. If I don't do it this way and the user doesn't have a car, I get null object errors all over the place.

In the view, I want to maintain a consistent way of accessing the data. For example when I want to grab the name of the car, it's pretty simple: {{optional($user->car)->name}}

So likewise I want to have a consistent way to access the maker too: {{optional($user->maker)->name}}

But this proves difficult. Given the maker() function I just posted, I have to write it like this, using the extra parenthesis around the maker function: {{optional($user->maker())->name}}

I don't want to have to use parenthesis on maker while not having to use it on car. It just leads to confusion.

Sep
21
1 month ago
Activity icon

Replied to HasOneThrough Confusion

Ok sure, but I also don't want to get a plugin for this either.

So what is the correct way to build my own relationship or query? I know the default relationships but not how to expand them to do what I need.

Do I do this as a custom attribute instead with a custom query?

Activity icon

Replied to HasOneThrough Confusion

Note that the "middle" table Car cannot be changed to a pivot table, having both user_id and maker_id columns, because many users might have this car. That's why each user has the car_id, rather than the Car table having a user_id, which seems to be what the hasOneThrough expects.

The hasOneThrough query looks something like this:

"select * from makers inner join cars on cars.maker_id = maker.id where cars.user_id is 1000"

The trick here is where it looks for cars.user_id, which doesn't exist as a column.

It seems like what has to happen is a second inner join to connect the users table to the cars table, like this perhaps:

"select * from makers inner join cars on cars.maker_id = maker.id inner join users on users.cars_id = cars.id where users.id = 1000"

hasOneThrough obviously doesn't use two inner joins, but I wonder if there is a way to adjust the query to make it work this way?

Activity icon

Started a new Conversation HasOneThrough Confusion

Simplified three tables: User (id, name, car_id) Car (id, name, maker_id) Maker (id, name)

On the Maker model, I used a hasManyThrough to grab all the users with that make of car, worked just fine. return $this->hasManyThrough(User::class, Car::class, 'maker_id', 'car_id')

But I also want to go the other way. If I have a user, I should be able to read who the maker is since they can only have one car, it's kind of a direct relationship that way. It doesn't work though: return $this->hasOneThrough(Maker::class, Car::class, 'maker_id', '?????')

I'm not sure how the syntax should work in this case, because it's the User that has a car_id column, not the Car model that has a user_id column. I don't know what would go in the method parameters. I've tried a bunch of different arrangements but it doesn't return the model.

So what's the easiest way to have my User model return the maker by traversing through the Car model?