0

I have a simple search/sort code snippet that I'm running on all of my models, and for some reason it is breaking on one of them. The model and controller file look pretty identical in what is brought in and such, so I'm not sure what the issue is. This code is designed to run off of URL tokens, such as: /doctors?search=bob&sortBy=first_name&sort=asc. Which works on all of my other models.

Code Block

// Searches, sorts, and filters
$search = $request->search;
$sort = ($request->sort == null ? 'asc' : $request->sort);
$sortBy = ($request->sortBy == null ? 'prescribe_date' : $request->sortBy);

This returns the following error: Undefined variable: request

And points to the lines in the code block above. I thought it might be because there was no ternary for a null search, but even fixing that points to the next $sort line.

1
  • can you show all function, I think you don't declare request on function?! Commented Nov 17, 2018 at 11:43

2 Answers 2

1

Did you set

Request $request

as a parameter when you define your function? Like in this snippet.

Otherwise $request would be undefined.

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

Comments

1

To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller method. The incoming request instance will automatically be injected by the service container:

public function store(Request $request)
    {
        $search = $request->search;
    }

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.