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 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测