php - Laravel Relationship Getting All Children
Get the solution ↓↓↓I have a query which returns a table. I would like to also return a list of styles which relate to it from another table. I have tried many things but I can't find one which works. Any advice?
Query:
$data = YarnOrderLines::with('yarn_orders')
->with('yarns')->get(); //I would like to pull in YarnOrderLineStyles
YarnOrderLines:
protected $fillable = [
'yarn_orders_id',
'yarn_id',
'qty',
'price',
'invoice_no',
'qty_invoiced',
];
public function yarn_orders()
{
return $this->belongsTo(YarnOrders::class);
}
public function yarns()
{
return $this->belongsTo(Yarn::class);
}
public function yarn_order_line_styles()
{
return $this->hasMany(YarnOrderLineStyles::class)->with('yarn_orders')->with('yarns');
}
YarnOrderLineStyles (table with relating styles in I would like to pull in)
protected $fillable = [
'yarn_order_lines_id',
'order_id',
];
public function yarn_order_lines()
{
return $this->belongsTo(YarnOrderLines::class);
}
Answer
Solution:
You're already getting 2 relations from YardOrderLines, so you just need to add another with statement. You can combine them all into 1 line like this:
YarnOrderLines::with(['yarn_orders', 'yarns', 'yarn_order_line_styles'])
->get();
And remove thewith
statement on your relation inside of YarnOrderLines, so it really doesn't have a bearing on the Styles table.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: uncaught mysqli_sql_exception
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.