7

I'm trying to make an advanced search form with Laravel 4, and this is the query:

$result = DB::table('users_ads')
        ->join('ads', 'users_ads.ad_id', '=', 'ads.id')         
        ->orderBy($column, $method)
        ->where('status', TRUE)
        ->where(function($query) use ($input)
        {
            $query->where('short_description', $input['search'])
                    ->where('category', $input['category'])
                    ->where('product', $input['product']);

        })
        ->join('users', 'users_ads.user_id', '=', 'users.id')
        ->select('ads.id', 'ads.img1', 'ads.short_description', 'ads.category', 'ads.product', 'ads.price', 'users.city')
        ->get();

    return $result;

The problem is that the user might not use all the input fields. So i want to include some if conditions in this part:

$query->where('short_description', $input['search'])
                    ->where('category', $input['category'])
                    ->where('product', $input['product']);

.. so if the input is empty, to remove the "where" condition.

3 Answers 3

22

You could wrap each where in an if statement.

$query = DB::table('user_ads')
            ->join('ads', 'users_ads.ad_id', '=', 'ads.id')
            ->orderBy($column, $method);

if ($input['search']) {
    $query->where('short_description', $input['search']);
}

if ($input['category']) {
    $query->where('category', $input['category']);
}

$query->join('users', 'users_ads.user_id', '=', 'users.id')
    ->select('ads.id', 'ads.img1', 'ads.short_description', 'ads.category', 'ads.product', 'ads.price', 'users.city')

$result= $query->get();

return $result;

Something along those lines would work I believe.

Sign up to request clarification or add additional context in comments.

6 Comments

Not that it makes much of a difference, but you've got the terminology flipped. Before the get call it's a query, afterwards it's a result.
Oh ya, haha! I was just using the variable provided by the op, didnt really think about that. I shall amend it!
The OP chained everything till the get call, so it truly is a result.
Ya, true. I didnt look at the variable names that much. I prefer your answer either way anyway. A much simpler and cleaner method I believe.
If you are getting a plain white screen, then that can be good. Have you told the application to output a view or anything? If there is no output, then all you will see is a white screen.
|
13
$filters = [
    'short_description' => 'search',
    'category' => 'category',
    'product' => 'product',
];

.....

->where(function($query) use ($input, $filters)
{
    foreach ( $filters as $column => $key )
    {
        $value = array_get($input, $key);

        if ( ! is_null($value)) $query->where($column, $value);
    }
});

Newer version of Laravel have a when method that makes this much easier:

->where(function ($query) use ($input, $filters) {
    foreach ($filters as $column => $key) {
        $query->when(array_get($input, $key), function ($query, $value) use ($column) {
            $query->where($column, $value);
        });
    }
});

1 Comment

+1 For this, would work well, and better than my answer.
0
$role = $request->input('role');
 
$users = DB::table('users')
                
->when($role, function ($query) use ($role) {
                    
          return $query->where('role_id', $role);
                
})
->get();

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.