Abdallah Sakre
Abdallah Sakre

Reputation: 915

Loop through an array of multiple values

I have the following variable which holds an array :

$category = $request->get('catBox');

The variable has the following output :

array(2) { [0]=> string(7) "Zamalek" [1]=> string(4) "Ahly" }

How can I properly put the $category variable in the below query :

       $Tagids = DB::table('tags')
                    ->where('t_name', $category)                  
                    ->pluck('id');  

So that after that I will loop through the result to store a row for each result :

foreach($Tagids as $tagid){

                      $tagIns = new tagpost();
                      $tagIns->tag_id = $tagid;
                       $tagIns->save();
                   }

Upvotes: 0

Views: 38

Answers (1)

dparoli
dparoli

Reputation: 9161

You can to use a whereIn() it accepts an array of values as second argument, i.e.:

$Tagids = DB::table('tags')
            ->whereIn('t_name', array_values($category))                  
            ->pluck('id');  

Probably array_values() is redundant, you can try even without:

$Tagids = DB::table('tags')
            ->whereIn('t_name', $category)                  
            ->pluck('id');  

Upvotes: 1

Related Questions