1

Here is my code using mysql command

SELECT * FROM `order_list` WHERE currentdate like '%12%' and currentdate like '%2017%'

but Im having a problem converting it into laravel eloquent.

$orderlists = Orderlist::where('currentdate', 'like', "%$month%")
                                    ->where('currentdate', 'like', "%$year%")
                                    ->get();

is this correct?thanks

10
  • 2
    It's bad idea to search dates with LIKE. mysql has YEAR() or MONTH() function which fit better. Commented Nov 28, 2017 at 9:09
  • That could potentially return the 12th January 2017 (12/01/2017), something to think about. Commented Nov 28, 2017 at 9:10
  • 2
    In currentdate like '%12%' you search what? 12 as a day, or 12 as a month (december). Commented Nov 28, 2017 at 9:12
  • 1
    Please, make an effort and use a search engine. You will find plenty of answers. Commented Nov 28, 2017 at 9:13
  • 2
    So why are you using a varchar to store dates when you should be using a date datatype? This is going to cause you a lot of problems in the future Commented Nov 28, 2017 at 9:19

2 Answers 2

1
$orderlists = Orderlist::whereMonth('currentdate', 'LIKE','%'.$month.'%')
                                ->whereYear('currentdate', 'LIKE', '%'.$year.'%')
                                ->get();
Sign up to request clarification or add additional context in comments.

Comments

0

You can also use your raw sql query as shown below

$orderlists = DB::select("SELECT * FROM `order_list` WHERE currentdate like '%12%' and currentdate like '%2017%'");

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.