lundi 6 avril 2020

laravel 5 Search related models

I have two related model.

 class Girl extends Model
    {
     public function target()
        {
            //return $this->hasOne('App\Target');
            return $this->belongsToMany('App\Target', 'girl_target', 'girl_id',
                'target_id');
        }
    }

And

class Target extends Model
{
  public function girl()
    {
        return $this->belongsToMany('App\Girl', 'girl_target');
    }
}

How I can find girl model by related target id?

  $girls = Girl::where('banned', 0)
             ->with('target');

 $targets=$seachSettings->target()->get();
            if($targets!=null){
                foreach ($targets as $item) {
                    //     $girls->target()->array_where();
                    $girls->target()->where('girl_target','target_id',$item->id);
                }
            }

I want get only models where related targets with id from my $targets array.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire