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

208
Views
Laravel 5 SQL Relations

I have some problem with a query in Laravel. I want to create a filter query by column in server table but I don't know how to do this.

Need help to modify the following line:

$data = $video->files()->with('server')->get();

Model: server

public function files()
{
    return $this->hasMany('App\Models\File', 'id', 'server_id');
}

Model: file

public function server()
{
    return $this->belongsTo('App\Models\Server', 'server_id', 'id')->ordered();
}

My current query return this: (but it returned all, I need return only FILE flitered by type = 6 which is server table) I don;t know how to do this. On screen below data about type server is on #relations array enter image description here

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use WhereHas. Takes the relation name and a closure as a parameter, where you can define sub query like functionality with the relation.

$data = $video->files()->with('server')->whereHas('server', function ($query) 
{
    $query->where('type', '6');
})->get();
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!