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

258
Views
Laravel: Donde la selección para la relación Eloquent Eager Loading

Tengo dos tablas DB:

Publicaciones

 $table->increments('id'); $table->integer('country_id')->unsigned(); $table->foreign('country_id')->references('id')->on('countries');

Países

 $table->increments('id'); $table->string('name', 70);

Uso laravel como back-end. Ahora quiero implementar el filtrado de datos para mi front-end. Entonces, el usuario puede seleccionar el nombre de un país y laravel debe responder la solicitud solo con publicaciones que tengan un país con el nombre especificado.

¿Cómo podría agregar esta condición a mi consulta de paginación existente? Intenté esto:

 $query = app(Post::class)->with('country')->newQuery(); // ... if ($request->exists('country')) { $query->where('country.name', $request->country); } // ...

... resultando en el siguiente error:

 Column not found: 1054 Unknown column 'country.name' in 'where clause' (SQL: select count(*) as aggregate from `posts` where `country`.`name` = Albania)
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

El método whereHas acepta el parámetro según la base de código de Laravel,

 /** * Add a relationship count / exists condition to the query with where clauses. * * @param string $relation * @param \Closure|null $callback * @param string $operator * @param int $count * @return \Illuminate\Database\Eloquent\Builder|static */ public function whereHas($relation, Closure $callback = null, $operator = '>=', $count = 1) { return $this->has($relation, $operator, $count, 'and', $callback); }

así que cambiando el código un poco como,

 $query = "" if ($request->has('country'){ $query = Post::with("country")->whereHas("country",function($q) use($request){ $q->where("name","=",$request->country); })->get() }else{ $query = Post::with("country")->get(); }

Por cierto, el código anterior se puede simplificar un poco de la siguiente manera;

 $query = "" if ($request->has('country'){ $query = Post::with(["country" => function($q) use($request){ $q->where("name","=",$request->country); }])->first() }else{ $query = Post::with("country")->get();

}

about 4 years ago · Santiago Trujillo Report

0

$query = "" if ($request->has('country'){ $query = Post::with("country")->whereHas("country", function($q) use($request){ $q->where("name","=",$request->country); })->get() }else{ $query = Post::with("country")->get(); }
about 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!