WCaaan

WCaaan

Member Since 2 Years Ago

Experience Points
140
Total
Experience

4,860 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 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
140 XP
Sep
24
1 month ago
Activity icon

Replied to Pivot Model Relationship With Another Table

I have solved the problem myself, I will write the detailed answer here in case if someone ran into same problem in future. I have also answered the question i asked on stackoverflow https://stackoverflow.com/questions/63989814/laravel-pivot-model-relationship-with-another-table

I have modified the database but the concept of problem remains the same.

https://i.stack.imgur.com/Y4ie8.png

Explination:

  • items has many tiers
  • tiers has one type
  • tiers has many options and options has many tiers (bringing the option_tier table in scene)
  • option_tier has many sizes and sizes has many option_tier (bringing the option_tier_price table in scene, with an additional field of 'price')

Solutions: I am using [AjCastro\EagerLoadPivotRelations\EagerLoadPivotTrait][2] for eager loading pivot relations. The concept is taken from this article [https://medium.com/@ajcastro29/laravel-eloquent-eager-load-pivot-relations-dba579f3fd3a][3] The only difference is i have changed the relationship from belongTo to hasMany and it works like a charm.

Tier Model:

use AjCastro\EagerLoadPivotRelations\EagerLoadPivotTrait;

class Tier extends Model
{
    use EagerLoadPivotTrait;
    protected $table = 'tiers';

	public function options()
	{
		return $this->belongsToMany(Option::class, 'option_tier')
			->using(OptionTier::class)
			->withPivot('id');
	}
}

Option Model:

use AjCastro\EagerLoadPivotRelations\EagerLoadPivotTrait;

class Option extends Model
{
    use EagerLoadPivotTrait;

    protected $table = 'options';
	
    public function tiers()
	{
		return $this->belongsToMany(Tier::class, 'option_tier');
	}
}

OptionTier Model:

use AjCastro\EagerLoadPivotRelations\EagerLoadPivotTrait;
use Illuminate\Database\Eloquent\Relations\Pivot;

class OptionTier extends Pivot
{
	use EagerLoadPivotTrait;
	
	protected $table = 'option_tier';
	
	public function price()
	{
		return $this->hasMany(OptionTierPrice::class, 'option_tier_id');
	}
}

OptionTierPrice Model:

use AjCastro\EagerLoadPivotRelations\EagerLoadPivotTrait;

class OptionTierPrice extends Model
{
	use EagerLoadPivotTrait;
	
	protected $table = 'option_tier_price';
	
	public function size()
	{
		return $this->belongsTo(Size::class);
	}
}

Controller:

$tiers = Product::with(['tiers' => function($query) {
    $query->with(['type'])
		  ->with(['sizes'])
		  ->with(['options.pivot.price.size'])
		  ->with(['addons.pivot.price.size']);
}])
->get();
return $tiers;
Sep
21
1 month ago
Activity icon

Started a new Conversation Pivot Model Relationship With Another Table

Table structure of types:

id, title

Table structure of options:

id, title, description

Table structure of option_type:

id, option_id, type_id

Table structure of option_type_size:

option_type_id, size_id, price

Table structure of size:

id, title, description

Database Explanation:

  • Item has many tiers
  • Tier has one type (Size, Option OR Addon)
  • type has many-to-many relationship with option
  • optionType has many-to-many relationship with size, in the pivot table the is additional field called price.

The Problem: In eloquent query I am able to fetch data until option_type, I don't know how to create relationship of option_type pivot model with size model.

Eloquent Query:

$items = Item::with(['tiers' => function ($query) {
			$query->with(['type' => function($query) {
				$query->with('sizes')
					->with('options')
					->with('addons');
			}]);
		}])->where('id', 1)->get();
		return $items;

What I want to Achieve: I want to eager load option_type with sizes, for every optiontype row i may have size and its price.

The Problem: when i write a query like:

$query->with(['options' => function($query) {
    $query->with('sizes);
}])

It is basically saying that options have relationship with sizes which is not right, it should be optionType have relationship with size. how to can i get the data so that i would show me for record for each optiontype_id, size_id and price.

Type Model:

class Type extends Model
{
    protected $table = 'types';

    public function options()
    {
	    return $this->belongsToMany(Option::class);
    }
}

Option Model:

class Option extends Model
{
    public function type()
    {
	    return $this->belongsToMany(Type::class);
    }
}

OptionType Model:

use Illuminate\Database\Eloquent\Relations\Pivot;

class OptionType extends Pivot
{
	protected $table = 'option_type';

    public function optiontypesize()
	{
		return $this->belongsToMany(Size::class, 'option_type_size', 'size_id', 'option_type_id');
	}
}

Size Model:

class Size extends Model
{
	public function optiontypesize()
	{
		return $this->belongsToMany(OptionType::class, 'option_type_size', 'size_id', 'option_type_id');
	}
}