0 votes

Argument 1 passed to Illuminate \Database\Query\Builder ::cleanBindings() must be of the type array, string given

I am making the edition of a product and this one has the possibility of putting product variations and I need to load in a multiple select, the marked ones and those that are not marked.

On the one hand, I retrieve from my Variations table all the variations generated from this product:

+----+------------+----------+-----------+
| id | product_id | variant  | valor     |
+----+------------+----------+-----------+
| 2  | 1          | color    | negro     |
| 3  | 1          | color    | rojo      |
| 4  | 1          | color    | blanco    |   
+----+------------+----------+-----------+

$variations =  Variation::select(DB::raw('GROUP_CONCAT(valor) AS valor'), 'variant')
                            ->where('product_id', $producto->id)
                            ->groupBy('variant')
                            ->get();

Then I print the following in my eyesight:

@foreach ($variations as $varia)
  <div class="form-group select-container-{{$varia->variant}}" style="position:relative;">
      <div class="deplegables" style="text-transform: capitalize; ">{{$varia->variant}}</div>
        <select class="js-example-basic-multiple form-control formNuevo variaciones" name="{{$varia->variant}}[]" multiple="multiple" id="{{$varia->variant}}">
           <option value="" selected>{{$varia->valor}}</option>
         </select>
         <a class="removeclass posiPapelera"><i class="far fa-trash-alt eye"></i></a>
   </div>
@endforeach

This works fine, I get as many selects as there are variants, although the concat separates it from me by , and I put everything in a selection, it is something that I do not know how to solve that if it comes: "option1,option2" it prints me in the select= option1 option2.

Ok but now the problem is that so far I pull out the ones I have selected/created. But the color variant for example has more colors than the selected ones.

I have it built like this:

Table Attributes:

+----+--------+-------+
| id | name   | slug  |     
+----+--------+-------+
| 2  | Color  | color |
| 3  | Talla  | talla |    
+----+--------+-------+

Table Terms:

+----+-------------+----------+-----------+
| id | atributo_id | termino  | slug      |
+----+-------------+----------+-----------+
| 2  | 2           | color    | negro     |
| 3  | 2           | color    | rojo      |
| 4  | 2           | color    | blanco    |   
+----+-------------+----------+-----------+

So what I want to get, is that if I bring a printed multiple select of colors, I get the values I have in the table Variations and then with a join bring the rest of available colors for example.

I am trying this way but without success.

In the controller under the query $variations I do this:

foreach($variations as $item){
   $todasOpciones = DB::table('atributos')
     ->where('slug', $item->variant)
     ->join('terminos', 'atributos.id', '=', 'terminos.atributo_id')
     ->select('terminos.*', 'atributos.id')
     ->whereNotIn('terminos.slug', $item->valor)->where('estado', 1)->get();
}

I do a foreach to be able to access the array, as you can see I first take the table attributes and compare it with ->where('slug', $item->valor) so I select the attribute I need, then I do a join to get terms that have that attribute and finally I tell it not to select the ones that are already there.

I hope I have explained myself well and you can help me, I get this error.

Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, string given, called in /var/www/highas/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php on line 870

Edition I have already solved the problem that they are concatenated and I put them together in the select by passing the string to an array.

$array=explode(',',$varia->valor); 

@foreach ($array as $item)
    <option value="" selected>{{str_replace("-"," ",$item)}}</option>
@endforeach

Following feedback from colleagues:

In the controller:

$variations =  Variation::select(DB::raw('GROUP_CONCAT(valor) AS valor'), 'variant')
   ->where('product_id', $producto->id)
   ->groupBy('variant')
   ->get();
foreach($variations as $item){
   $todasOpciones = DB::table('atributos')
      ->where('atributos.slug', $item->variant)
      ->join('terminos', 'atributos.id', '=', 'terminos.atributo_id')
      ->select('terminos.*', 'atributos.id')
      ->whereNotIn('terminos.slug', explode(',', $item->valor))->get();
}

On view:

@foreach ($variations as $varia)
   @php $array=explode(',',$varia->valor); @endphp
   <div class="form-group select-container-{{$varia->variant}}" style="position:relative;">
     <div class="deplegables" style="text-transform: capitalize; ">{{$varia->variant}}</div>
        <select class="js-example-basic-multiple form-control formNuevo variaciones" name="{{$varia->variant}}[]" multiple="multiple" id="{{$varia->variant}}">
        @foreach ($array as $item)
          <option value="{{ $item }}" selected>{{str_replace("-"," ",$item)}}</option>
        @endforeach
        @foreach ($todasOpciones as $item)
           <option value="{{ $item->slug }}">{{ $item->termino }}</option>
        @endforeach
        </select>
        <a class="removeclass posiPapelera"><i class="far fa-trash-alt eye"></i></a>
         </div>
@endforeach

I add two images to make myself better understood:

If there is only one select printed: It comes out perfect, I have two sizes selected, but you can choose from the remaining sizes:

introducir la descripción de la imagen aquí

If, on the other hand, for example, it is a product with two types of variations, I get the remainder of the second select in the first one:

introducir la descripción de la imagen aquí

As you can see I mix the paper attributes with the size attributes.

1voto

Aprendiz Points 1310

The error is at the level of your query derived from the following line:

->whereNotIn('terminos.slug', $item->valor)->where('estado', 1)->get();

Because as you note in the doc. the method wherNotIn expects as first argument the name of the column on which it will evaluate and the second argument is an array of values.

The result is as follows:

->whereNotIn('terminos.slug', [$item->valor])->where('estado', 1)->get();

0 votes

Hi @BetaM thanks for your answer, now it does not return error, but it does not exclude the ones already in the select multiple

0 votes

@Miguel in the absence of more time hopefully someone else will participate in the question.

0 votes

Thank you, I will continue to investigate...

1voto

Jonatan Lavado Points 2436

Your problem lies in the fact that the value attribute is a string and should be an array.

->whereNotIn('terminos.slug', explode(',', $item->valor))
->where('estado', 1)
->get();

If you only do [$item->valor] you will get only an array with one element which will be the concatenated string.


What you have to do is to add a new property to your array of objects called $variations . For each select to have its own "allOptions".

$variations = $variations->each(function($item, $key){
      $item->todasOpciones = DB::table('terminos')
          ->select('termino', 'slug')
          ->where('termino', $item->variant)
          ->whereNotIn('slug', explode(',', $item->valor))
          ->get();
});

Since $variations is an instance of Collections after using get() then you can use each().

Subsequently, in your view you do the following:

@foreach ($varia->todasOpciones as $item)
    <option value="{{ $item->slug }}">{{ $item->termino }}</option>
@endforeach

0 votes

Sorry, I keep getting the same error!

0 votes

In the view... In the view I still have to have the other foreach I had, don't I? @foreach ($array as $item)

0 votes

Ohhh!!! now yes!!! thank you so much @L.Flor you have been a great help!!!!

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X