【问题标题】:ErrorException Trying to get property 'name' of non-object when try to search name from different database table?ErrorException 尝试从不同的数据库表中搜索名称时尝试获取非对象的属性“名称”?
【发布时间】:2021-08-10 06:46:55
【问题描述】:

嘿,我想做一个用户 id、姓名和班次模式 id 的搜索功能。所有搜索功能都适用于所有人,但名称搜索失败。仅供参考,名称在用户表内,用户 ID 和班次模式 ID 在用户班次模式表内。

型号:-

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserShiftPattern extends Model
{
  protected $fillable = ['name'];

  public function shiftpattern(){
    return $this->belongsTo(ShiftPattern::class, 'shift_pattern_id');
  }

  public function uspattern(){
    return $this->belongsTo(User::class, 'user_id');
  }

  public function name(){
    return $this->hasOne(User::class);
  }
}

控制器:-

public function index(Request $req) {
    $usershiftpattern = [];
    if($req->filled('searching')){
        $usershiftpattern = $this->fetch($req);
    }
    return view('admin.usershiftpattern', ['usps' => $usershiftpattern]);
}
public function fetch(Request $req) {
    $fuserid = $req->inputuserid;
    $fname = explode(",", str_replace(' ','',$req->inputname));
    $fshiftpatternid = explode(",", str_replace(' ','',$req->inputshiftpatternid));
    $usershiftpatternlist = UserShiftPattern::query();
    if(isset($req->inputuserid)) {
            $usershiftpatternlist = $usershiftpatternlist->where('user_id','LIKE','%' .$fuserid. '%');
        }
        
        if(isset($req->inputname)){
            $usershiftpatternlist = $usershiftpatternlist->whereIn('name',$fname);
        }
        
        if(isset($req->inputshiftpatternid)){
            $usershiftpatternlist = $usershiftpatternlist->whereIn('shift_pattern_id',$fshiftpatternid);
        }
        $usershiftpatternlist = $usershiftpatternlist->get();

        return $usershiftpatternlist;
    }  

查看:-

<div class="text-right">
            <br>
            <button type="submit" name="searching" value="filter" class="btn-up">SEARCH</button>
        </div>
            <br>
    </form>

            <thead>
                <tr>
                    <th>User ID</th>
                    <th>Name</th>
                    <th>Shift Pattern ID</th>
                </tr>
            </thead>
            <tbody>
                @foreach($usps as $uspslist)
                <tr>
                    <td>{{ $uspslist->user_id }}</td>
                    <td>{{ $uspslist->uspattern->name }}</td>
                    <td>{{ $uspslist->shift_pattern_id }}</td>
                </tr>
                @endforeach
            </tbody>

【问题讨论】:

    标签: laravel eloquent laravel-8


    【解决方案1】:

    这是因为在某些 $usps 实例中删除了 uspattern 关系,要忽略这一点,您可以使用 null 合并:

    <td>{{ $uspslist->uspattern->name ?? "" }}</td>
    

    或者过滤掉没有uspattern关系的记录:

    $usershiftpatternlist = $usershiftpatternlist->has('uspattern')->get();
    

    您只需要使用上述解决方案之一

    【讨论】:

    • 它没有正常工作,因为表格内的所有设计都变得凌乱并出错
    • 我已经尝试了这两种解决方案,并得到这个选项卡错误 --> 超过 30 秒的最大执行时间
    • 这是因为您的查询运行时间过长,仅使用第一个解决方案(空合并)或增加 php.ini 中的 max_execution_time
    • 如果您的数据库太大,请尝试限制或分页您的数据
    • omg,我尝试按名称搜索并出现其他错误--> SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where Clause' (SQL: select * from @ 987654323@ where name in (mas) and exists (select * from users where user_shift_patterns.user_id = users.id))
    【解决方案2】:

    更新 我现在让您包含用户模型,就像我在评论中显示的那样,并且 尝试像这样更改您的控制器,不要对所有变量名称使用小写字母,而不是$inputuserid 使用$inputUserId

      public function fetch(Request $req) {
            $fuserid = $req->inputuserid;
            $fname = explode(",", str_replace(' ','',$req->inputname));
            $fshiftpatternid = explode(",", str_replace(' ','',$req->inputshiftpatternid));
            $usershiftpatternlist = new UserShiftPattern(); //change this line
    
            $user = new User(); //check user table
            if(isset($req->inputuserid)) {
                    $usershiftpatternlist = $usershiftpatternlist->where('user_id','LIKE','%' .$fuserid. '%');
                }
                
                if(isset($req->inputname)){
                    $usershiftpatternlist = $user->whereIn('name',$fname); //change to user variable
                }
                
                if(isset($req->inputshiftpatternid)){
                    $usershiftpatternlist = $usershiftpatternlist->whereIn('shift_pattern_id',$fshiftpatternid);
                }
                $results = $usershiftpatternlist->get(); //change variable
        
                return $results; //change this too
            }  
    

    我希望它有效

    用户数据库:-

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
    
            $table->integer('id')->primary('id')->unsigned();
    
    
            $table->string('name');
            $table->string('email');
            $table->rememberToken();
    
            $table->string('staff_no');
            $table->integer('persno')->nullable();
            $table->string('new_ic')->unique();
            $table->string('company_id')->nullable();
            $table->integer('orgunit')->nullable();
            $table->string('persarea',10)->nullable();
            $table->string('perssubarea',10)->nullable();
            $table->string('state_id')->nullable();
            $table->integer('reptto')->nullable();
            $table->timestamps();
    
        });
    }
    

    用户转移模式数据库:-

    public function up()
    {
        Schema::create('user_shift_patterns', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('user_id');
            $table->integer('shift_pattern_id');
            $table->dateTime('start_date')->nullable();
            $table->dateTime('end_date')->nullable();
            $table->string('sap_code', 20);
            $table->integer('created_by')->default(0);
            $table->string('source','5')->default('SAP');
            $table->dateTime('upd_sap')->nullable();
            $table->timestamps();
        });
    }
    

    【讨论】:

    • 一切正常,但是当我搜索名称时,出现错误 --> SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' (SQL: select * from user_shift_patterns where name = mas and exists (select * from users where user_shift_patterns.user_id = users.id))
    • 你能告诉我那个表的迁移吗
    • 也许你把它和 fname 混淆了,我要检查的是那个列名还是 fname,因为那个错误意味着没有列名存在
    • 名称在用户表中
    • 而其他人在用户班次模式表内
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2021-09-03
    • 2019-05-23
    • 2019-07-11
    相关资源
    最近更新 更多