remove global scope from relation in laravel
Asked Answered
S

2

6

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

Schoolmistress answered 25/10, 2018 at 10:17 Comment(0)
S
14

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

Schoolmistress answered 25/10, 2018 at 10:35 Comment(3)
That works of course, but I just wonder if there is a way to do it dynamically on an existing relaitonMultiform
For me only works without withTrashed. With withoutGlobalScope directly after hasMany.Mullin
This is great! This is a snippet: return $this->hasOne(QuestionMulti::class, 'dimension_id')->withoutGlobalScope(MainQuestionScope::class);Mclendon
G
0

To remove global scopes from a models relation, you need to eager load the relation and pass a closure to modify its query:

Model::with([
   'relation' => fn ($query) => $query->withoutGlobalScopes()
]);

If you use Model::has('relation') to query for the existence of relationships, you can use whereHas instead, to remove global scopes:

Model::whereHas(
   'relation', fn ($query) => $query->withoutGlobalScopes()
);
Glacial answered 6/3, 2023 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.