Search code examples
arrayslaraveleloquentlaravel-8laravel-query-builder

wherenotin() must be of type array, null given error


so im trying to update my data, and it shows this:

Illuminate\Database\Query\Builder::cleanBindings(): Argument #1 ($bindings) must be of type array, null given

so when i try to look into it, the error is here

        $tradeSetup->tags()->whereNotIn('user_tag_id', $request->userTag)->delete();

weird thing is, i already pass the value as array, but the error comes out when i dont want to return any userTag at all.

//form.blade.php

<div class="form-group row">
        <label class="col-md-2 col-form-label">@lang('Tags')</label>
            <div class="col-md-10">
                <select name="userTag[]" class="select2-tag form-control" multiple="multiple">
                    @foreach($userTags as $key => $userTag)
                        <option value="{{ $userTag->id }}" {{ $tradeSetup->tags->where('user_tag_id',$userTag->id)->first() ? 'selected' : '' }}>{{ $userTag->name }}</option>
                    @endforeach
                </select>
            </div>
    </div><!--form-group-->
//TradeSetup model

public function userTag()
    {
        return $this->hasMany(UserTag::class);
    }

    public function tags()
    {
        return $this->morphMany(Tag::class, 'taggable');
    }
//TradeSetupController.php

public function update(Request $request, TradeSetup $tradeSetup)
{
        $request->validate([
            'symbol' => 'required',
            'entry_price' => 'required',
            'target_price' => 'required',
            'stop_loss_price' => 'required',
            'notes' => 'required',
            // 'tags' => 'required',
            'side' => 'required',
            'confidence' => 'required',
            // 'ratio' => 'required',
        ]);


        $tradeSetups = $this->tradeSetupService->update($request, $tradeSetup);

        // dd($request->userTag);

        $tradeSetup->tags()->whereNotIn('user_tag_id', $request->userTag)->delete();

        foreach($request->userTag as $tag){
            if($tradeSetup->tags()->where('user_tag_id', $tag)->exists()){
            // if (Tag::where('user_tag_id', $tag )->exists()) {

            }else if (Tag::whereHas('userTag')->where('taggable_id', $tradeSetup->id)->where('taggable_type', 'App\Domains\TradeSetup\Models\TradeSetup')->get()){

                Tag::create([
                    'user_tag_id'=> $tag,
                    'taggable_type' => get_class($tradeSetups),
                    'taggable_id'=> $tradeSetups->id,
                    // 'type' => $request->type,
                    // 'state'=> $request->state,
                ]);
            }
        }


return redirect()->route('frontend.user.tradeSetup.index')->withFlashSuccess('TradeSetup updated');



}

i tried to dd($request->userTag) and the result is null

FYI, userTag is not required so the user can just not input any userTag when they store/update.


Solution

  • UPDATE

    my mistake, but i dont need to actually change the null to []. i just want to delete all the record in Tag.

    if(isset($request->userTag)){
            $tradeSetup->tags()->whereNotIn('user_tag_id', $request->userTag)->delete();
        }else{
            $tradeSetup->tags()->delete();
        }