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

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

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

我的错误:
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:

-------------------
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-&gt;belongsToMany(&#39;App\Unit&#39;, &#39;kd_unit&#39;, &#39;kd_unit&#39;);
    }

    public function hasAnyUnits($units) {
        return null !== $this-&gt;units()-&gt;whereIn(&#39;kd_unit&#39;, $units)-&gt;first();
    }
    
    public function hasAnyUnit($unit) {
        return null !== $this-&gt;units()-&gt;where(&#39;kd_unit&#39;, $unit)-&gt;first();
    }
}

My Controller:

class ManagerOnDutyController extends Controller
{
  public function edit($kd_rs)
    {
        $units = Unit::orderBy(&#39;nm_unit&#39;, &#39;asc&#39;)-&gt;where(&#39;is_active&#39;, true)-&gt;get();
        $mod = TManagerOnDuty::where(&#39;kd_rs&#39;, $kd_rs)-&gt;get(); 
        return view(&#39;layouts.manageronduty.edit&#39;)-&gt;with([
            &#39;mod&#39;   =&gt; $mod,
            &#39;units&#39; =&gt; $units,
            &#39;kd_rs&#39; =&gt; $kd_rs
        ]);
    }
}  

My view:

@foreach($units as $unit)
    &lt;div class=&quot;custom-control custom-checkbox mb-3&quot;&gt;
        &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;
        &lt;label class=&quot;custom-control-label&quot; for=&quot;customCheck{{$unit-&gt;kd_unit}}&quot;&gt;{{$unit-&gt;nm_unit}}&lt;/label&gt;
    &lt;/div&gt;
@endforeach

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

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

答案1

得分: 1

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

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

Try this one

$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:

确定