Illuminate\Database\Eloquent\Collection::hasAnyUnit 方法不存在

huangapple go评论148阅读模式
英文:

Method Illuminate\Database\Eloquent\Collection::hasAnyUnit does not exist

问题

我想要在视图中从数据库中显示已选状态,当我在视图中添加 {{ $mod->hasAnyUnit($unit->kd_unit)?'checked':'' }} 时,我收到了错误。

我有一个单位表,类似于角色表:

  1. -------------------
  2. id | kd_unit | name
  3. -------------------
  4. 1 | 01 | A
  5. 2 | 02 | B
  6. 3 | 03 | C
  7. -------------------

第二个表是 rs 表,类似于用户表:

  1. -------------------
  2. id | kd_rs| name
  3. -------------------
  4. 1 | 01 | ASD
  5. 2 | 02 | ZXC
  6. -------------------

第三个表是 mod 表,类似于角色用户:

  1. -------------------
  2. id | kd_rs| kd_unit
  3. -------------------
  4. 1 | 01 | 01
  5. 2 | 01 | 02
  6. 3 | 01 | 03
  7. -------------------

我的模型来自于 mod 表:

  1. class TManagerOnDuty extends Model
  2. {
  3. public function units() {
  4. return $this->belongsToMany('App\Unit', 'kd_unit', 'kd_unit');
  5. }
  6. public function hasAnyUnits($units) {
  7. return null !== $this->units()->whereIn('kd_unit', $units)->first();
  8. }
  9. public function hasAnyUnit($unit) {
  10. return null !== $this->units()->where('kd_unit', $unit)->first();
  11. }
  12. }

我的控制器:

  1. class ManagerOnDutyController extends Controller
  2. {
  3. public function edit($kd_rs)
  4. {
  5. $units = Unit::orderBy('nm_unit', 'asc')->where('is_active', true)->get();
  6. $mod = TManagerOnDuty::where('kd_rs', $kd_rs)->get();
  7. return view('layouts.manageronduty.edit')->with([
  8. 'mod' => $mod,
  9. 'units' => $units,
  10. 'kd_rs' => $kd_rs
  11. ]);
  12. }
  13. }

我的视图:

  1. @foreach($units as $unit)
  2. <div class="custom-control custom-checkbox mb-3">
  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">
  4. <label class="custom-control-label" for="customCheck{{$unit->kd_unit}}">{{$unit->nm_unit}}</label>
  5. </div>
  6. @endforeach

我的错误:
Illuminate\Database\Eloquent\Collection::hasAnyUnit 方法不存在

我的期望:
Illuminate\Database\Eloquent\Collection::hasAnyUnit 方法不存在

英文:

i want to show checked state from database, when i adding {{ $mod-&gt;hasAnyUnit($unit-&gt;kd_unit)?&#39;checked&#39;:&#39;&#39; }} on view, i got error

i have units table, like a role table:

  1. -------------------
  2. id | kd_unit | name
  3. -------------------
  4. 1 | 01 | A
  5. 2 | 02 | B
  6. 2 | 03 | C
  7. -------------------

And 2nd is rs table, like a users table:

  1. -------------------
  2. id | kd_rs| name
  3. -------------------
  4. 1 | 01 | ASD
  5. 2 | 02 | ZXC
  6. -------------------

And 3rd is mod table, like a role_user:

  1. -------------------
  2. id | kd_rs| kd_unit
  3. -------------------
  4. 1 | 01 | 01
  5. 2 | 01 | 02
  6. 3 | 01 | 03
  7. -------------------

My Model from mod table:

  1. class TManagerOnDuty extends Model
  2. {
  3. public function units() {
  4. return $this-&gt;belongsToMany(&#39;App\Unit&#39;, &#39;kd_unit&#39;, &#39;kd_unit&#39;);
  5. }
  6. public function hasAnyUnits($units) {
  7. return null !== $this-&gt;units()-&gt;whereIn(&#39;kd_unit&#39;, $units)-&gt;first();
  8. }
  9. public function hasAnyUnit($unit) {
  10. return null !== $this-&gt;units()-&gt;where(&#39;kd_unit&#39;, $unit)-&gt;first();
  11. }
  12. }

My Controller:

  1. class ManagerOnDutyController extends Controller
  2. {
  3. public function edit($kd_rs)
  4. {
  5. $units = Unit::orderBy(&#39;nm_unit&#39;, &#39;asc&#39;)-&gt;where(&#39;is_active&#39;, true)-&gt;get();
  6. $mod = TManagerOnDuty::where(&#39;kd_rs&#39;, $kd_rs)-&gt;get();
  7. return view(&#39;layouts.manageronduty.edit&#39;)-&gt;with([
  8. &#39;mod&#39; =&gt; $mod,
  9. &#39;units&#39; =&gt; $units,
  10. &#39;kd_rs&#39; =&gt; $kd_rs
  11. ]);
  12. }
  13. }

My view:

  1. @foreach($units as $unit)
  2. &lt;div class=&quot;custom-control custom-checkbox mb-3&quot;&gt;
  3. &lt;input name=&quot;unitRS[]&quot; value=&quot;{{$unit-&gt;kd_unit}}&quot; {{ $mod-&gt;hasAnyUnit($unit-&gt;kd_unit)?&#39;checked&#39;:&#39;&#39; }} id=&quot;customCheck{{$unit-&gt;kd_unit}}&quot; class=&quot;custom-control-input&quot; type=&quot;checkbox&quot;&gt;
  4. &lt;label class=&quot;custom-control-label&quot; for=&quot;customCheck{{$unit-&gt;kd_unit}}&quot;&gt;{{$unit-&gt;nm_unit}}&lt;/label&gt;
  5. &lt;/div&gt;
  6. @endforeach

My error:
Illuminate\Database\Eloquent\Collection::hasAnyUnit 方法不存在

My expected:
Illuminate\Database\Eloquent\Collection::hasAnyUnit 方法不存在

答案1

得分: 1

尝试将以下代码添加到您的控制器中,因为get返回对象集合,您不能直接从该集合中调用hasAnyUnit(),所以首先从查询中获取单个对象,然后使用hasAnyUnit()进行链式调用:

  1. $mod = TManagerOnDuty::where('kd_rs', $kd_rs)->first();
英文:

Try this one

  1. $mod = TManagerOnDuty::where(&#39;kd_rs&#39;, $kd_rs)-&gt;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()

huangapple
  • 本文由 发表于 2020年1月3日 17:05:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定