Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pain12's avatar

orWhere

Hey guys, I am trying something like this:

$today = strToLower(now()->format('l'));
$posts = auth()->user()->posts()->latest()->where($today,'<>',0)->orwhere('immer','=','1')->paginate(10);
       
return view('posts.index')->with('posts', $posts);

What is happening is now that the first where the statement works fine. I only get the posts from the specific user. But the second where clause gives back all tables with the name 'immer' with the value 1 from All users.

Does anyone have any idea how to fix this?

0 likes
5 replies
Pain12's avatar

OK nice. But I get the error "Undefined variable: today". So somehow it can't find my defined

$today = strToLower(now()->format('l'));

variable now.

My whole function looks like this:

  public function index()
    {

        $today = strToLower(now()->format('l'));

        $posts = auth()->user()->posts()
        ->latest()
        ->where(function($query) {
            $query->where($today,'<>',0)
                  ->orwhere('immer','=','1');
        })
        ->paginate(10);
                                                    
       
        return view('posts.index')->with('posts', $posts);
       
    }

PovilasKorop's avatar

You need to pass that variable like this

function($query) use ($today) { 
1 like
Snapey's avatar

please mark it solved if it is?

Please or to participate in this conversation.