Laravel 尝试从不同的数据库表中搜索名称时,尝试获取非对象的属性“名称”时发生错误异常?
Hye,我想做一个用户id、姓名和移位模式id的搜索功能。所有搜索功能对所有人都有效,但对姓名搜索失败。仅供参考,名称在用户表中&用户id和换档模式id在用户换档模式表中 型号:- 视图:-Laravel 尝试从不同的数据库表中搜索名称时,尝试获取非对象的属性“名称”时发生错误异常?,laravel,eloquent,laravel-8,Laravel,Eloquent,Laravel 8,Hye,我想做一个用户id、姓名和移位模式id的搜索功能。所有搜索功能对所有人都有效,但对姓名搜索失败。仅供参考,名称在用户表中&用户id和换档模式id在用户换档模式表中 型号:- 视图:- 这是因为在某些$usps实例中删除了uspattern关系,要忽略这一点,可以使用null合并: <td>{{ $uspslist->uspattern->name ?? "" }}</td> 您只需使用上述解决方案之一即可 我现在让你们包括用户模型
这是因为在某些$usps实例中删除了uspattern关系,要忽略这一点,可以使用null合并:
<td>{{ $uspslist->uspattern->name ?? "" }}</td>
您只需使用上述解决方案之一即可
我现在让你们包括用户模型,就像我在评论中所展示的,并且
尝试像这样更改控制器,不要对所有变量名称使用小写字母,因为这样会很难读取$inputuserid而不是$inputuserid使用$inputuserid
我希望它能起作用
用户数据库:-
用户轮班模式数据库:-
它无法正常工作,因为表中的所有设计都变得凌乱并出现错误。例如,什么错误?我已经尝试了这两种解决方案,获取此选项卡错误->超过30秒的最大执行时间这是因为您的查询运行时间过长使用第一个解决方案仅空合并或增加php中的最大执行时间。如果您的数据库太大,请尝试限制或分页您的数据一切正常,但当我搜索名称时,出现错误->SQLSTATE[42S22]:Column not found:1054 where子句中的未知列“name”SQL:select*from user\u shift\u patterns where name=mas and exists select*from user\u shift\u patterns.user\u id=users.id您能告诉我该表的迁移情况吗可能您把它与fname混淆了,我想检查的是该列名还是fname,因为那个错误意味着不存在列名称。名称位于用户表中,而其他名称位于用户移位模式表中
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>
<td>{{ $uspslist->uspattern->name ?? "" }}</td>
$usershiftpatternlist = $usershiftpatternlist->has('uspattern')->get();
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();
});
}