skip to Main Content

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

IlluminateDatabaseQueryBuilder::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', 'AppDomainsTradeSetupModelsTradeSetup')->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.

3

Answers


  1. Chosen as BEST ANSWER

    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();
        }
    

  2. Maybe u dont have tags <form> and <input type="submit"> to send request into your controller. Must be an action to provide all request info.

    If u want to do it dynamic, use axios or XmlHttpRequest in javascript to send request into controller.

    It’s not a solution, but also u can check csrf protection, there is full information about forms and their protection, check this out
    https://laravel.com/docs/9.x/csrf

    Login or Signup to reply.
  3. The method whereNotIn needs an array as second argument. Then no value is selected in your form, the request doesn’t contain any data for userTag and so it is null and not an empty array.

    But you can simply fix this by defining a default parameter for userTags for example like this:

    $tradeSetup->tags()->whereNotIn('user_tag_id', $request->userTag ?: [])->delete();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search