Php 如何根据父对象的关系过滤laravel中的数据

Php 如何根据父对象的关系过滤laravel中的数据,php,laravel,laravel-6,Php,Laravel,Laravel 6,我有三种模式:客户、组织和行业。我想筛选和显示客户机,这些客户机分配给choosen行业中的任何组织。 关系: Client N-N Organization N-N Industry 一个客户可以有多个组织,一个组织可以有多个行业。我使用form和GET方法 我的控制器: class ClientListsController extends Controller { public function index() { $clients = new Client;

我有三种模式:客户、组织和行业。我想筛选和显示客户机,这些客户机分配给choosen行业中的任何组织。 关系:

Client N-N Organization N-N Industry
一个客户可以有多个组织,一个组织可以有多个行业。我使用form和GET方法

我的控制器:

class ClientListsController extends Controller
{
  public function index()
  {
      $clients = new Client;
      $queries = array();
      $columns = ['statuses'];

      foreach ($columns as $column) {
        if (request()->has($column)) {

          if ($column == 'industries') {
            // filter by industries
          }

          if ($column == 'statuses' && request($column) == 1 ) {
            $clients = optional($clients->has('organizations'))->paginate(100, ['*'], 'client_page');
          }else if($column == 'statuses' && request($column) == null ){
            $clients = Client::paginate(100, ['*'], 'client_page');
          }
          else{
            $clients = $clients->whereDoesntHave('organizations')->paginate(100, ['*'], 'client_page');
          }

          $queries[$column] = request($column);

        }else{
          $clients = Client::paginate(100, ['*'], 'client_page');
        }
      }

      $organizations = Organization::paginate(100, ['*'], 'org_page');
      return view('clientLists.index')->with(['clients' => $clients, 'organizations' => $organizations]);
  }
}
我尝试过多种方法,比如在组织中循环使用当前结果,但在使用时返回true或false。 我还尝试使用$clients=Client::all,但我的目的是进一步分页,因此我使用了Client::where'id','*',因此我没有获得集合,但这导致状态过滤器无法工作

我和拉威尔6号一起工作。您知道如何使用分页进行通用筛选吗?非常感谢

编辑 详细信息中的相关关系f.e.客户-组织的透视表是定义了外键的客户组织

App\Client
    public function Organizations()
    {
      return $this->belongsToMany('App\Organization')->withTimestamps();
    }
App\Organization
  public function Clients()
  {
    return $this->belongsToMany('App\Client')->withTimestamps();
  }
  public function Industries()
  {
    return $this->belongsToMany('App\Industry')->withTimestamps();
  }
App\Industry
  public function Organizations()
  {
    return $this->belongsToMany('App\Organization')->withTimestamps();
  }
编辑2: 我试图通过SQL查询来实现我的目标,但我一直在使用多对多关系的连接

$clients = DB::table('clients')
->leftJoin('client_organization', 'client_organization.client_id', '=', 'clients.id')->leftJoin('industry_organization', 'industry_organization.organization_id', '=', 'organizations.id')
->whereIn('industry_organization.industry_id', request($column))
->paginate(100, ['*'], 'client_page');
}
此查询返回“未找到”列:1054“on子句”中的未知列“organizations.id”

因此,如果您拥有$industry,您可以使用您的关系查找其中的所有组织:

$Organizations=$industry->Organizations

然后,您希望获得每个组织的所有客户:

$clients = [];

foreach ($organisations as $organisation) {
    $clients[$organisation] = $orgination->clients()->toArray();
}
或者,也可以在一个查询中对其进行全面检查:

$industries = request($column);

$clients = DB::table('clients')
    ->leftJoin('client_organization', 'client_organization.client_id', '=', 'clients.id')
    ->leftJoin('organizations', 'organizations.id', '=', 'client_organization.id')
    ->leftJoin('industry_organization', 'industry_organization.organization_id', '=', 'organizations.id')
    ->whereIn('organisations.industry_id', $industries)
    ->paginate(10);

一行一行地尝试检查并调试它,在第一次leftJoin之后尝试->获取并检查它是否工作;如果可以的话,在第二次之后再试试。

我是这样做的:

      if (request()->industries[0] != "") {
        $clients = $clients->with('organizations')->whereHas('organizations', function($query){
          $query->with('industries')->whereHas('industries', function($query2){
            $query2->whereIn('industry_id', request()->industries);
          });
        });
      }

谢谢你的帮助,男孩们:

你能发布你的关系吗?当然,给你:这很有趣!我已经尝试过了,但是你仍然不能对数组进行分页:@NorbertJurga我已经为你添加了一个查询,需要根据你的数据库结构进行一些调整-我还没有尝试过,但是如果你有任何问题,请告诉我,就我所知,当我有多对多关系时,请不要告诉我。另一个是我的$u GET['industries']是一个数组,因此我必须能够检查多个行业,因此,我无法进行查询并重置以前定义的$clients。“一体式”查询为我提供了一种替代方法,您可以使用分页,而不是像那样浏览关系并建立它。您需要从您离开join Organizations表的答案中添加行
      if (request()->industries[0] != "") {
        $clients = $clients->with('organizations')->whereHas('organizations', function($query){
          $query->with('industries')->whereHas('industries', function($query2){
            $query2->whereIn('industry_id', request()->industries);
          });
        });
      }