laravel检查同一行中的两个变量是否有效

时间:2017-11-10 22:32:35

标签: php laravel

如何检查表中但是同一行中的两个变量是否存在然后执行 例 我有客户表有id(autoincreament),customer_id,包ID所以我只想检查customer_id和package_id是否等于请求然后做 我试过这个但是没有用

 public function redirectpackage(Request $request)
{
if(Customer::where('customer_id', 'LIKE', $request->customer_id) && 
 (Customer::where('package_id', 'LIKE', $request->package_id))->exists()) {

  return view('index.customer.customerabout'); 
} else
{
 return redirect()->intended(route('home'));
}
}

3 个答案:

答案 0 :(得分:0)

public function redirectpackage(Request $request)
{
if(Customer::where('customer_id','=',( Auth::user()->id))->where('package_id','>',0)->first())

{
return view('index.customer.customerabout'); 

}
else
{
return redirect()->intended(route('home'));
}
}

     }

答案 1 :(得分:0)

 public function redirectpackage(Request $request){

    if(Customer::where('customer_id', 'LIKE', $request->customer_id)->where('package_id', 'LIKE', $request->package_id))->first()) {
       return view('index.customer.customerabout'); 
   } else {
       return redirect()->intended(route('home'));
   }
}

答案 2 :(得分:-1)

您需要遍历客户以检查是否有匹配的客户ID和包ID。

例如:

public function redirectpackage(Request $request)
{

$customers = Customer::all();

foreach ($customers as $customer) {

    if($customer->id', $request->customer_id) and 
 ($customer->packages->id', $request->package_id) {

        return view('index.customer.customerabout'); 
    } else
    {
        return redirect()->intended(route('home'));
    }
}

这应该让你开始。您需要进行修改以适合您的模型和关系。