Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 以雄辩的方式获得具有筛选地址的客户集合_Jquery_Laravel_Laravel 5_Eloquent_Laravel Collection - Fatal编程技术网

Jquery 以雄辩的方式获得具有筛选地址的客户集合

Jquery 以雄辩的方式获得具有筛选地址的客户集合,jquery,laravel,laravel-5,eloquent,laravel-collection,Jquery,Laravel,Laravel 5,Eloquent,Laravel Collection,我有两种型号: customers(id,firstname,lastname,phone) addresses(id,province_id,address,customer_id) 关于: class Customer extends Model { public function addresses() { return $this->hasMany(Address::class); } } public f

我有两种型号:

   customers(id,firstname,lastname,phone)
   addresses(id,province_id,address,customer_id)
关于:

 class Customer extends Model
 {

     public function addresses()
     {
         return $this->hasMany(Address::class);
     }
 }


 public function customer()
     {
         return $this->belongsTo(Customer::class,'customer_id');
     }
我正试图获得地址在某个城市的
客户

  return new CustomerCollection(Customer::has('addresses')
                       ->with(array('addresses' => function($q) use ($field)
                        {
                          $q->where('province_id','like',$field)->get();

                        }))->orderBy('id','DESC')->paginate(100));
结果很好,当我只需要地址在给定省份的客户时,我获得了所有客户的集合(坏),但地址数组只包含给定省份地址的数据

怎么了?如何从结果中删除省内没有地址的客户

    Customer::with('addresses')->whereHas('addresses', function ($q) use ($field) {
        return $q->where('province_id', 'like', '%'.$field.'%');
    })

您不需要在关系的嵌套查询中使用
get()
。同样,你的问题中的语法也不正确。尝试上述解决方案

您是否尝试过此方法,如果您不在查询中提供省id,则它将摆脱该条件,并为您的所有客户提供该地址,如果您提供
省id
,则您将获得唯一居住在
省id

Customer::query()->when($province_id,function($query){
            $query->whereHas('addresses', function($query) use($province_id){
                $query->where('province_id', $province_id);
            }); 
        })
        ->with('addresses')
        ->get();
更新:

class Customer extends Model
{
   
    public function scopeFilterByProvince($query, $provinceId = null)
    {
        return $query->when($province_id,function($query){
            $query->whereHas('addresses', function($query) use($province_id){
                $query->where('province_id', $province_id);
            }); 

       // if you pass name of province use this
       return $query->when($province,function($query){
            $query->whereHas('addresses', function($query) use($province){
                $query->whereHas('province', function($q) use($province){
                   $q->where('name', 'like', '%{$province}%');
               });
            }); 
    }
}
然后是控制器或您的回购协议

Customer::filterByProvince($request->get('province_id'))
        ->with('addresses')
        ->get();

试试这个:$q->where('province_id','like','%.$field'%')->get();结果是一样的。已发送地址数组,但已选中空,且地址数组不为空。但是客户收集是针对所有客户的(不好),我只会得到带有给定省id的地址。我已更新我的问题此选项仅发送客户数据,但发送有关地址的任何信息。另外,现在不发送地址数组。通过这种方式,我获取所有客户及其地址:
返回新的CustomerCollection(Customer::with('addresses')->orderBy('id','DESC')->paginate(500))
但现在我需要相同的解决方案和结构,但在某些Province上如果要返回此集合,您也可以使用('addresses')。我编辑了我的答案。最好将这个
when()
添加到一个列表中,这样你就可以在任何需要的地方使用它,打电话时只使用
Customer::filterProvince($province\u id)->with('addresses')->……
不起作用。但是你的答案看起来不错。我将尝试修改。我也尝试了本地范围。但仅当客户只需要数据时才起作用。数据地址在另一个表中,无法获取。我假设您正在从
$request->get('province\u id')
获取
$province\u id
,如果
$province\u id
null
则它将跳过条件I get province\u id fine。但是“客户收集”会获取所有客户,并且只获取该省地址的地址信息。其余地址信息(针对其余客户)为空