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

195
Views
¿Cómo puedo buscar con Pertenece a laravel?

Mi consulta para buscar un nombre de usuario en una lista de tiendas es la siguiente:

 $data = Store::join('users AS b', 'b.id', '=', 'stores.user_id') ->where('b.name', 'like', '%hazard%') ->paginate();

Funciona. Pero uso join para hacerlo.

¿Cómo puedo hacerlo usando pertenecen a?

El modelo de mi tienda es así:

 class Store extends Model { protected $fillable = ['name', 'user_id', ...]; public function user() { return $this->belongsTo(User::class,'user_id', 'id'); } ... }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Utilice whereHas() :

 Store::whereHas('user', function($q) use($username) { $q->where('name', 'like', '%'.$username.'%'); })->paginate(5);
over 4 years ago · Santiago Trujillo Report

0

Puede hacerlo utilizando el paquete de búsqueda para buscar a través de los campos del modelo y sus campos de relaciones.

 use Nicolaslopezj\Searchable\SearchableTrait; class User extends \Eloquent { use SearchableTrait; /** * Searchable rules. * * @var array */ protected $searchable = [ /** * Columns and their priority in search results. * Columns with higher values are more important. * Columns with equal values have equal importance. * * @var array */ 'columns' => [ 'users.first_name' => 10, 'users.last_name' => 10, 'users.bio' => 2, 'users.email' => 5, 'posts.title' => 2, 'posts.body' => 1, ], 'joins' => [ 'posts' => ['users.id','posts.user_id'], ], ]; public function posts() { return $this->hasMany('Post'); } }

usar :

 $users = User::search($query)->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!