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

Thursday, February 10, 2022

[FIXED] Filter record through dates not working laravel

 February 10, 2022     eloquent, laravel, laravel-4, laravel-5     No comments   

Issue

I have a expiry_date (type=date) column in my table

$currentDate = date('Y-m-d');
$Data = Post::whereDate('expiry_date','<=',$currentDate)->where(['status' => 'active'])->orWhere(['p_id' => 3])->select('id','title','status','p_id','expiry_date')->orderBy('id', 'DESC')->get();

i want to filter data if current date is greater than expiry date then those record should not be shown but in my scenario i'm still getting record.

Any solution Thanks.


Solution

You must group orWhere clause in closure. Grouping in closure is like () in real query.

$Data = Post::whereDate('expiry_date','<=',$currentDate)
    ->where(function($query){
        return $query
            ->where(['status' => 'active'])
            ->orWhere(['p_id' => 3]);
    })
    ->select('id','title','status','p_id','expiry_date')
    ->orderBy('id', 'DESC')
    ->get();

But, because I don't know your project - i may wrong with grouping.



Answered By - Maksim
  • 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