【问题标题】:Cannot retrieve data with eloquent无法用 eloquent 检索数据
【发布时间】:2021-02-16 00:24:55
【问题描述】:

我正在为我的客户表构建搜索功能,但由于某种原因,当我搜索具有特定手机号码的条目时,即使该数据存在,它也不会显示。我只是在我的“移动”属性上体验到这一点,而其他所有属性都很好。

这是我的客户表架构:

 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>

任何帮助将不胜感激!

【问题讨论】:

    标签: html mysql laravel eloquent laravel-blade


    【解决方案1】:

    我认为你需要 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);
    

    【讨论】:

    • 我也这么认为,但为什么他和你们都在移动搜索一个 bigint 类型的字符串
    • 我认为手机号码应该存储为字符串,这样它就可以包含像+这样的符号
    • 但这会增加它的搜索时间
    • 如果你把手机号存为号码,00969876之类的号码会存为969876,你会损失00,需要费用
    • 我已经更新了我的答案,看看是否有帮助,请确保您有电话和手机号码的字符串列
    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 2019-03-04
    • 2017-07-29
    • 2019-12-11
    • 1970-01-01
    • 2022-07-27
    • 2013-11-16
    相关资源
    最近更新 更多