doumu2172 2018-10-25 10:17 采纳率: 0%
浏览 224
已采纳

从laravel中的关系中删除全局范围

7 project and i have this global scope for Voucher_detale model

<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class getVouchers implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('vouchers_detale_state', '=', 1);
    }
}

and i have Voucher model and it has hasMany inside this is the code'

public function getDetales()
{
    return $this->hasMany('App\Vouchers_detale','vouchers_detale_voucher_id','id')->withTrashed();
}

inside the blade i have this code

@foreach($voucher['getDetales'] as $v)
<tr class='table-warning'>
    <td>
        <a href='/{{$path}}/Vouchers_detales/{{$v["id"]}}'>{{$v['vouchers_detale_user_id']}}</a>

        <div class='selected d-print-none'>
            @foreach($v->getUpdate as $update)
                <span style='color:black'>{{$update['update_history_old_amount']}}</span>
                {{$update['created_at']}}
                {{$update['getUserData']['user_name']}}
                <hr />
            @endforeach
        </div>
    </td>
    <td>{{$v['getUserData']['user_name']}}</td>
    @if($v['vouchers_detale_amount'] > 0)
        <td>{{$v['vouchers_detale_amount']}}</td>
        <td></td>
        @php $total_debt +=  $v['vouchers_detale_amount'] @endphp
    @else
        <td></td>
        <td>{{$v['vouchers_detale_amount'] * -1}}</td>
        @php $total_credit += $v['vouchers_detale_amount'] * -1 @endphp
    @endif
    <td>{{$v['vouchers_detale_desc']}}</td>
</tr>
@endforeach

now how can i ignore the global scope in the foreach i tried this

@foreach($voucher['getDetales']->withoutGlobalScopes() as $v)

i gat this error

Method Illuminate\Database\Eloquent\Collection::withoutGlobalScopes does not exist.

how can i ignore global scope come from relation in foreach thanks

  • 写回答

1条回答 默认 最新

  • duanjianqu3685 2018-10-25 10:35
    关注

    never mind i just created a new relation

    public function getDetalesWithout()
    {
        return $this->hasMany('App\Vouchers_detale','vouchers_detale_voucher_id','id')->withTrashed()->withoutGlobalScope('App\Scopes\getVouchers');
    }
    

    thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?