Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
Laravel Relationship Getting All Children

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);
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 the with statement on your relation inside of YarnOrderLines, so it really doesn't have a bearing on the Styles table.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!