英文:
Method Illuminate\Database\Eloquent\Collection::hasAnyUnit does not exist
问题
我想要在视图中从数据库中显示已选状态,当我在视图中添加 {{ $mod->hasAnyUnit($unit->kd_unit)?'checked':'' }}
时,我收到了错误。
我有一个单位表,类似于角色表:
-------------------
id | kd_unit | name
-------------------
1 | 01 | A
2 | 02 | B
3 | 03 | C
-------------------
第二个表是 rs 表,类似于用户表:
-------------------
id | kd_rs| name
-------------------
1 | 01 | ASD
2 | 02 | ZXC
-------------------
第三个表是 mod 表,类似于角色用户:
-------------------
id | kd_rs| kd_unit
-------------------
1 | 01 | 01
2 | 01 | 02
3 | 01 | 03
-------------------
我的模型来自于 mod 表:
class TManagerOnDuty extends Model
{
public function units() {
return $this->belongsToMany('App\Unit', 'kd_unit', 'kd_unit');
}
public function hasAnyUnits($units) {
return null !== $this->units()->whereIn('kd_unit', $units)->first();
}
public function hasAnyUnit($unit) {
return null !== $this->units()->where('kd_unit', $unit)->first();
}
}
我的控制器:
class ManagerOnDutyController extends Controller
{
public function edit($kd_rs)
{
$units = Unit::orderBy('nm_unit', 'asc')->where('is_active', true)->get();
$mod = TManagerOnDuty::where('kd_rs', $kd_rs)->get();
return view('layouts.manageronduty.edit')->with([
'mod' => $mod,
'units' => $units,
'kd_rs' => $kd_rs
]);
}
}
我的视图:
@foreach($units as $unit)
<div class="custom-control custom-checkbox mb-3">
<input name="unitRS[]" value="{{$unit->kd_unit}}" {{ $mod->hasAnyUnit($unit->kd_unit)?'checked':'' }} id="customCheck{{$unit->kd_unit}}" class="custom-control-input" type="checkbox">
<label class="custom-control-label" for="customCheck{{$unit->kd_unit}}">{{$unit->nm_unit}}</label>
</div>
@endforeach
英文:
i want to show checked state from database, when i adding {{ $mod->hasAnyUnit($unit->kd_unit)?'checked':'' }}
on view, i got error
i have units table, like a role table:
-------------------
id | kd_unit | name
-------------------
1 | 01 | A
2 | 02 | B
2 | 03 | C
-------------------
And 2nd is rs table, like a users table:
-------------------
id | kd_rs| name
-------------------
1 | 01 | ASD
2 | 02 | ZXC
-------------------
And 3rd is mod table, like a role_user:
-------------------
id | kd_rs| kd_unit
-------------------
1 | 01 | 01
2 | 01 | 02
3 | 01 | 03
-------------------
My Model from mod table:
class TManagerOnDuty extends Model
{
public function units() {
return $this->belongsToMany('App\Unit', 'kd_unit', 'kd_unit');
}
public function hasAnyUnits($units) {
return null !== $this->units()->whereIn('kd_unit', $units)->first();
}
public function hasAnyUnit($unit) {
return null !== $this->units()->where('kd_unit', $unit)->first();
}
}
My Controller:
class ManagerOnDutyController extends Controller
{
public function edit($kd_rs)
{
$units = Unit::orderBy('nm_unit', 'asc')->where('is_active', true)->get();
$mod = TManagerOnDuty::where('kd_rs', $kd_rs)->get();
return view('layouts.manageronduty.edit')->with([
'mod' => $mod,
'units' => $units,
'kd_rs' => $kd_rs
]);
}
}
My view:
@foreach($units as $unit)
<div class="custom-control custom-checkbox mb-3">
<input name="unitRS[]" value="{{$unit->kd_unit}}" {{ $mod->hasAnyUnit($unit->kd_unit)?'checked':'' }} id="customCheck{{$unit->kd_unit}}" class="custom-control-input" type="checkbox">
<label class="custom-control-label" for="customCheck{{$unit->kd_unit}}">{{$unit->nm_unit}}</label>
</div>
@endforeach
答案1
得分: 1
尝试将以下代码添加到您的控制器中,因为get
返回对象集合,您不能直接从该集合中调用hasAnyUnit()
,所以首先从查询中获取单个对象,然后使用hasAnyUnit()
进行链式调用:
$mod = TManagerOnDuty::where('kd_rs', $kd_rs)->first();
英文:
Try this one
$mod = TManagerOnDuty::where('kd_rs', $kd_rs)->first();
in your controller as get returns the set of object and you can not call your hasAnyUnit()
from that set so first call a single object from you query and than chained it with hasAnyUnit()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论