搜索查询Laravel输入

时间:2016-04-05 13:15:24

标签: php laravel search controller

我有一个很大的搜索查询,一切都很好但我需要为每个用户显示这个刀片

的包(研讨会)
 <tbody>
        @foreach($users as $i=>$user)
            <tr>
                <td>{{ $i+1 }}</td>
                <td>{{ $user->home_lastname }}</td>
                <td>{{ $user->home_firstname }} </td>
      //------> <td> HERE I NEED TO SHOW ALL THE PACKAGES NAMES(SEMINARS) FOR EACH USER</td>
                <td>{{ $user->home_id }}</td>
                <td></td>

UserController中

 $builder = User::query();

    if (Input::has('lastname')) {
        $queryString = Input::get('lastname');
        $builder->where('home_lastname', 'LIKE', "%$queryString%");
    }

    if (Input::has('name')) {
        $queryString = Input::get('name');
        $builder->where('home_firstname', 'LIKE', "%$queryString%");
    }

    if (Input::has('id')) {
        $queryString = Input::get('id');
        $builder->where('home_id', 'LIKE', "%$queryString%");
    }

    if (Input::has('type-select')){
        $userType = Input::get('type-select');
        $builder->select('*')
            ->where('Home_Students.home_elv_rights',$userType)
            ->get();
    }


    if(Input::has('seminar-select')){
        $seminar = Input::get('seminar-select');
        $builder->select('*')
            ->join('Home_StudentPackages','Home_StudentPackages.hsp_homeStudID','=','Home_Students.home_id')
            ->join('Home_Packages','Home_Packages.Package_ID','=','Home_StudentPackages.hsp_packid')
            ->where('Home_Packages.Package_ID',$seminar)
            ->get();
    }

    if(Input::has('class-select')){
        $class = Input::get('class-select');
        $builder->select('*')
            ->join('Hw_StudentClasses','Hw_StudentClasses.Stclass_studentid','=','Home_Students.home_id')
            ->join('Hw_Classes','Hw_Classes.Class_id','=','Hw_StudentClasses.Stclass_classid')
            ->where('Hw_Classes.Class_id',$class)
            ->get();
    }

    if(Input::has('date_created_from')) {
        $date = date("Y-m-d", strtotime(Input::get('date_created_from')));
        $builder->select('*')
            ->whereBetween('Home_Students.home_dateCreated', [$date, date("Y-m-d")])
            ->get();
        }
        elseif(Input::has('date_created_from','date_created_untill')){
            $date = date("Y-m-d", strtotime(Input::get('date_created_from')));
            $date2 = date("Y-m-d", strtotime(Input::get('date_created_untill')));
            $builder->select('*')
                ->whereBetween('Home_Students.home_dateCreated', [$date,$date2])
                ->get();
            }
            elseif(Input::has('date_created_untill')){
            $date2 = date("Y-m-d", strtotime(Input::get('date_created_untill')));
            $date = date("Y-m-d", strtotime(Input::get('date_created_from')));
                $builder->select('*')
                ->whereBetween('Home_Students.home_dateCreated',[$date,$date2])
                ->get();
            }




    // ... more clauses from the querystring
    $users = $builder->orderBy('home_lastname')->get();



        return View::make('user.show')->with(array('users' => $users));
    } 

我尝试将这些连接添加到启动$ builder查询和控制器末尾的构建器,但它没有工作。此外,每个 if 中的小部分都不起作用,所以我也不知道其他任何方法。

DB::table('Home_Students')
            ->select('home_lastname','home_firstname','home_id','Package_Name')
            ->join('Home_StudentPackages','Home_StudentPackages.hsp_homeStudID','=','Home_Students.home_id')
            ->join('Home_Packages','Home_Packages.Package_ID','=','Home_StudentPackages.hsp_PackID'
            ->get();

我只需要将每个用户的Packages_Names显示给我的刀片视图,但是研讨会(包)的搜索存在问题。

修改

我正在尝试在用户模型中创建公共功能,我将获得每个用户的所有包(研讨会);你知道怎么办?

有一个组合用户和包的数据透视表!

编辑2 我认为这是错误的,因为它会返回[{&#34; Package_Name&#34;:&#34; Excel&#34;}] - 是否可以将其作为字符串?

 public function getUserSeminars(){



        $users = Seminar::select('Package_Name')
            ->join('Home_StudentPackages','Home_StudentPackages.hsp_PackID','=','Home_Packages.Package_ID')
            ->join('Home_Students','Home_Students.home_id','=','Home_StudentPackages.hsp_homeStudID')
            ->where('Home_Students.home_id','=',$this->home_id)
            ->get();

        return $users;
    }

1 个答案:

答案 0 :(得分:1)

不要这么复杂的搜索查询和控制器内部我去了我的用户模型,我做了这个功能:

public function getUserSeminars(){


        $seminar = Seminar::select('Package_Name')
            ->join('Home_StudentPackages','Home_StudentPackages.hsp_PackID','=','Home_Packages.Package_ID')
            ->join('Home_Students','Home_Students.home_id','=','Home_StudentPackages.hsp_homeStudID')
            ->where('Home_Students.home_id','=',$this->home_id)
            ->get();

        return $seminar;



    }

然后我像这样在刀片中调用它

                <td>
                    @foreach($user->getUserSeminars() as $seminar)
                        <li>    {{$seminar->Package_Name}}  </li>
                    @endforeach
                </td>

它就像一个魅力。 希望这对某人也有帮助!