May 5, 2020
0
Level 1
Laravel join and relation for membership and it's child
well, I have these three tables -
membership, plans, stocks, and stocks_with_plan - this is I am using for they have multiple stocks with a single plan
so now I am using the model to get the data off it
but I am not getting the right way to get the correct way to get the data I am getting his in dd
Membership Relation - Plans
now I want the stocks_with_plan under the plans relationship and stocks table under the stocks_with_plan
stocks.php
class Stocks extends Model
{
protected $table = 'stocks';
protected $guarded = ['id'];
}
stocks_with_plan.php
protected $table = 'stocks_with_plan';
protected $primaryKey = 'plan_id';
public function Membership() {
return $this->belongsToMany('app\plans');
}
Plans.php
protected $table = 'plans';
protected $guarded = ['id'];
public function Stocks() {
return $this->belongsToMany(Stocks::class, 'plans_id');
}
public function Stocks_with_plan() {
return $this->belongsToMany(Stocks_with_plan::class);
}
Membership.php
protected $table = 'membership';
protected $guarded = ['id'];
public function Users() {
return $this->belongsTo(User::class, 'user_id');
}
public function Plans() {
return $this->belongsTo(Plans::class, 'plan_id');
}
public function Stocks_with_plan() {
return $this->belongsToMany(Stocks_with_plan::class,'stocks_with_plan','plan_id','plan_id');
}
and using this to get my need -
public function store(){
Membership::with('plans', 'plans.Stocks_with_plan')->where('membership.user_id', auth()->user()->id)->where('membership.status', 'paid')->get();
}
Please or to participate in this conversation.