Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:55:36+00:00 2026-06-18T08:55:36+00:00

I have a query builder that works: $article = Page::where(‘slug’, ‘=’, $slug) ->where(‘hide’, ‘=’,

  • 0

I have a query builder that works:

        $article = Page::where('slug', '=', $slug)
                     ->where('hide', '=', $hidden)
                     ->first();

But I want to only add the second where statement if hidden is equal to 1. I’ve tried the code below which shows the logic of what I’m trying to do, but it doesn’t work.

$article = Page::where('slug', '=', $slug);
if ($hidden == 1) {
    $article->where('hide', '=', 1);
}
$article->first();

I’m using Laravel 4, but I think the question still stands with Laravel 3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:55:37+00:00Added an answer on June 18, 2026 at 8:55 am

    Yeah there’s a little “gotcha” with Eloquent and the query builder. Try the code below 😉

    $query = Page::where('slug', '=', $slug);
    
    if ($hidden == 1) {
        $query = $query->where('hide', '=', 1);
    }
    
    $article = $query->first();
    

    Note the assigning of $query within the conditional. This is becuase the first where (statically called) returns a different object to the query object within the conditional. One way to get around this, I believe due to a recent commit, is like so:

    $query = Page::where('slug', '=', $slug)->query();
    

    This will return the query object and you can do what you want as per normal (Instead of re-assigning $query).

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have query to show the table like this: but I want to PIVOT
I am using in C# MYsql .I have query that works if I run
I have built my own SQL Query builder that breaks apart an Expression, however,
I have to dynamically add OR expressions to the query builder returned by getListQueryBuilder
I have a query that successfully grabs the unique products from my products table
I have a query in Access 2007. It's worked fine for months, but I'm
We are trying to build a SQL query builder that would allow users to
I have a query that has 3 columns: Payment Method, Count, Amount. When I
i have a big query (in my query builder) and a lot of left
I am trying to use query builder to select all the categories that belong

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.