0

Currently I'm working on a ajax search filters on Laravel, but I cannot get the correct info, this is the scenario:

I have 2 tables:

  • Table1: SoftwareRequest

  • Table2: DenyCategory

with a select option I get the name as Value

enter image description here

and I added that select option like in top of the query on the controller function:

$deniedReason = $request->get('deniedReason');
        if($deniedReason == "All"){
            $deniedReason = "";
        }

So that means that every time I select "All" it will be empty so I can get all data like empty (this is the problem).

This is my current query:

$request_data = SoftwareRequest::leftJoin('DenyCategory', 'SoftwareRequest.DenyCategoryId', '=', 'DenyCategory.Id')->where('DenyCategory.Name', 'like', '%' . $deniedReason . '%')->paginate(20);

So the thing is that if I select another option rather than "All", for example "Already Available", I do get the excepted data, meaning all objects from table 1 joined with table 2 that has that option, but the problem comes when I select "All" it doesn't bring all the data it should and that's because not all objects have DenyCategoryId in Table1 meaning that some of those are Null/empty so it only brings the ones 'LIKE' Null/empty as I specified on the previous code block.

$deniedReason = $request->get('deniedReason');
        if($deniedReason == "All"){
            $deniedReason = "";
        }

How can I get all data empty or not empty when I select the option All and as well as to get the data when I select another option? I bet it would have something to do with the query not being 'Like' but that's out of my knowledge scope.

3
  • Use $request->input('deniedReason') Commented Aug 18, 2020 at 19:17
  • hello @Sobir I do get the value of the input, the problem is that I need to get ALL including Null data from the joined column SoftwareRequest.DenyCategoryId with the table DenyCategory Commented Aug 18, 2020 at 19:53
  • is there a way to change the parent value to NotNull, for ex. ->leftJoin('DenyCategory', 'NOTNULL(SoftwareRequest.DenyCategoryId)', '=', 'DenyCategory.Id') So I could only get the ones with data on it Commented Aug 18, 2020 at 19:54

1 Answer 1

2

Why not make the where clause optional?

$queryBuilder = SoftwareRequest::leftJoin('DenyCategory', 'SoftwareRequest.DenyCategoryId', '=', 'DenyCategory.Id')

// Only apply where-clause when denied reason has been provided.
if ($request->get('deniedReason') !== 'All') {
    $queryBuilder = $queryBuilder->where('DenyCategory.Name', 'like', '%' . $deniedReason . '%');
}

$request_data = $queryBuilder->paginate(20);

I think you can improve your code a bit better but I'll leave that up to you with some pointers:

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

2 Comments

thanks, i'll try that. Hmm is there a way to change the table1 value to NotNull on the leftJoin, for ex. ->leftJoin('DenyCategory', 'NOTNULL(SoftwareRequest.DenyCategoryId)', '=', 'DenyCategory.Id') So I could only get the ones with data on it
I tried what you said and worked as expected, thanks Danny

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.