无法使用 eloquent 检索数据
Cannot retrieve data with eloquent
我正在为我的客户构建一个搜索功能 table,但出于某种原因,当我搜索具有特定手机号码的条目时,即使该数据存在,它也不会显示。我只有 'mobile' 属性遇到此问题,而其他属性都很好。
这是我的客户 table 架构:
Schema::create('customers', function (Blueprint $table) {
$table->id();
$table->string('firstName')->nullable();
$table->string('lastName');
$table->string('companyName');
$table->string('companyEmail')->unique();
$table->string('branch')->nullable();
$table->bigInteger('phone')->nullable();
$table->bigInteger('mobile')->nullable();
$table->string('address')->nullable();
$table->string('clientType')->nullable();
$table->string('reseller')->nullable();
$table->string('assignedTo');
$table->timestamps();
});
当我尝试在 PHPMyAdmin 上执行相同的查询时,它确实检索了数据:
但是在使用这段代码时,它不会:
$results = Customer::where('firstName', 'like', '%'.$request->input('search-customer-fname'). '%')
->where('lastName', 'like', '%'.$request->input('search-customer-lname'). '%')
->where('companyName', 'like', '%'.$request->input('search-company-name'). '%')
->where('companyEmail', 'like', '%'.$request->input('search-company-email'). '%')
->where('branch', 'like', '%'.$request->input('search-company-branch'). '%')
->where('phone', 'like', '%'.$request->input('search-company-phone'). '%')
->where('mobile', 'like', '%'.$request->input('search-company-mobile'). '%')
->paginate(10);
这是移动输入字段的 HTML/Blade 代码:
<label for="search-company-mobile">
Mobile:
<input class="form-control" type="number" name="search-company-mobile">
</label>
如有任何帮助,我们将不胜感激!
我认为你需要 orWhere ... 而不是 where:
$results = Customer::query();
if($request->input('search-customer-fname')!=null)
{
$results = $results->where('firstName', 'like', '%'.$request->input('search-customer-fname'). '%');
}
if($request->input('search-customer-lname')!=null)
{
$results = $results->where('lastName', 'like', '%'.$request->input('search-customer-lname'). '%');
}
if($request->input('search-company-name')!=null)
{
$results = $results->where('companyName', 'like', '%'.$request->input('search-company-name'). '%');
}
if($request->input('search-company-email')!=null)
{
$results = $results->where('companyEmail', 'like', '%'.$request->input('search-company-email'). '%');
}
if($request->input('search-company-branch')!=null)
{
$results = $results->where('branch', 'like', '%'.$request->input('search-company-branch'). '%');
}
if($request->input('search-company-phone')!=null)
{
$results = $results->where('phone', 'like', '%'.$request->input('search-company-phone'). '%');
}
if($request->input('search-company-mobile')!=null)
{
$results = $results->where('mobile', 'like', '%'.$request->input('search-company-mobile'). '%');
}
$results=$results->paginate(10);
我正在为我的客户构建一个搜索功能 table,但出于某种原因,当我搜索具有特定手机号码的条目时,即使该数据存在,它也不会显示。我只有 'mobile' 属性遇到此问题,而其他属性都很好。
这是我的客户 table 架构:
Schema::create('customers', function (Blueprint $table) {
$table->id();
$table->string('firstName')->nullable();
$table->string('lastName');
$table->string('companyName');
$table->string('companyEmail')->unique();
$table->string('branch')->nullable();
$table->bigInteger('phone')->nullable();
$table->bigInteger('mobile')->nullable();
$table->string('address')->nullable();
$table->string('clientType')->nullable();
$table->string('reseller')->nullable();
$table->string('assignedTo');
$table->timestamps();
});
当我尝试在 PHPMyAdmin 上执行相同的查询时,它确实检索了数据:
但是在使用这段代码时,它不会:
$results = Customer::where('firstName', 'like', '%'.$request->input('search-customer-fname'). '%')
->where('lastName', 'like', '%'.$request->input('search-customer-lname'). '%')
->where('companyName', 'like', '%'.$request->input('search-company-name'). '%')
->where('companyEmail', 'like', '%'.$request->input('search-company-email'). '%')
->where('branch', 'like', '%'.$request->input('search-company-branch'). '%')
->where('phone', 'like', '%'.$request->input('search-company-phone'). '%')
->where('mobile', 'like', '%'.$request->input('search-company-mobile'). '%')
->paginate(10);
这是移动输入字段的 HTML/Blade 代码:
<label for="search-company-mobile">
Mobile:
<input class="form-control" type="number" name="search-company-mobile">
</label>
如有任何帮助,我们将不胜感激!
我认为你需要 orWhere ... 而不是 where:
$results = Customer::query();
if($request->input('search-customer-fname')!=null)
{
$results = $results->where('firstName', 'like', '%'.$request->input('search-customer-fname'). '%');
}
if($request->input('search-customer-lname')!=null)
{
$results = $results->where('lastName', 'like', '%'.$request->input('search-customer-lname'). '%');
}
if($request->input('search-company-name')!=null)
{
$results = $results->where('companyName', 'like', '%'.$request->input('search-company-name'). '%');
}
if($request->input('search-company-email')!=null)
{
$results = $results->where('companyEmail', 'like', '%'.$request->input('search-company-email'). '%');
}
if($request->input('search-company-branch')!=null)
{
$results = $results->where('branch', 'like', '%'.$request->input('search-company-branch'). '%');
}
if($request->input('search-company-phone')!=null)
{
$results = $results->where('phone', 'like', '%'.$request->input('search-company-phone'). '%');
}
if($request->input('search-company-mobile')!=null)
{
$results = $results->where('mobile', 'like', '%'.$request->input('search-company-mobile'). '%');
}
$results=$results->paginate(10);