0

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();
                   }
4
  • Have you tried printing out the content of the $category is it really an array? Add this ` dd($category);` after you get it from the request and make sure that it returns array of ids. Commented Aug 8, 2019 at 17:42
  • What does dd($request->all()) give you? Commented Aug 8, 2019 at 17:47
  • I made some changes to further clarify the issue Commented Aug 8, 2019 at 18:35
  • try ->whereIn('t_name', array_values($category)) Commented Aug 8, 2019 at 18:43

1 Answer 1

1

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');  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.