PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 8, 2022

[FIXED] How to shorten this eloquent command

 February 08, 2022     laravel, laravel-8, php     No comments   

Issue

I have used this Eloquent command to get some data from the database table:

$categories = Category::where('category_type',2)->orWhere('category_type',3)->orWhere('category_type',4)->get();

return view('admin.categories.index', compact('categories'));

So I want category_type of 2,3 and 4 but I wanted to shorten this command, so I tried this instead:

$categories = Category::where('category_type',[2,3,4,5])->get();

But it does not work properly and only show data with category_type of 2!

So the question is how can I shorten these orWhere commands? Is there any way for doing that?


Solution

Try whereIn()...

<?php

$categories = Category::whereIn('category_type',[2,3,4])->get();

return view('admin.categories.index', compact('categories'));

https://laravel.com/docs/8.x/queries#additional-where-clauses



Answered By - Simon K
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing