#Return a view with ->withInput() and ->with()
110 messages · Page 1 of 1 (latest)
Remove which code?
You have tons of comments and messy indents
And 0 syntax highlighting
It makes it absurdly annoying to look at
@covert aurora Can you give an example on how I can highligt the code. I did clean up the code. But I am not sure if it is good enough now?
It is my first time to ask a question here and I don't know the best way to formulate the code. I mean if I should use gist or something else etc.
Add <?php to the start of the file
Done, I knew I was missing something 😅
Ok , deleted ->withInput from the code. But that was the question, how can I get access to ->withInput?
Cause I want to show the old input after user have made a search.
The "problem" is that you're keeping everything on the same page
So you can't do withInput since you're not redirecting anywhere
So I should make a hole new page for search with controller etc.
No necessarily, you just can't do withInput
Thats does not make any sense. So user will actually not be able to see their old input.
Just flash the values normally, the withInput is a helper on redirects because you often want to redirect somewhere WITH the input
In your case, you're never leaving the same page
"Just flash the values normally" can you give a code example?
@covert aurora Thanks a lot. Everything works fine now, but I only need to get access to withErrors now.
Any idea on how I can return withErrors?
Something like this: ->withErrors($validator)
This one works perfect for old input: session()->flashInput($request->all());
I'm not at my PC right now, but I believe this is the same issue as before that the withErrors are on redirects
Maybe, I'm not sure
Ok, thanks for teaching me limitations for view and help.
@shut mist Searching should should be done via GET requests. You’d then populate any inputs using the value in the query string: request()->input('field_name')
@shut mist And please don’t create multiple threads for the same topic, like you did here: https://discord.com/channels/297040613688475649/1061624527928430622
I deleted the other topic.
No, you didn’t. I just closed it.
But I clicked delete.
…
I don't think withErrors work with flash()
@frail anchor Ok. But do you know how I can get access to withErrors on view if I can not use withErrors with flash?
Hmm I'm thinking about this right now
I think you can return the view with some array
Does it absolutely need to be a flash ?
no, it does not need to be a flash. I just thought that was the only solution
But it's a bit strange that I can get access to oldInput but not errors 😅
If that is your code
🔗 https://gist.github.com/qaiswardag/c2fc8c4c969ebf3936170e3fff88dfa4#file-helpcentercontroller-php-L24
You can drastically improve the logic
@shut mist What are you actually trying to do here?
Why are you trying to “access” withErrors?
Yes, that is the code
I’ve already told you that searches should be done with GET requests.
So there’s no “input” or “errors” to access.
I think the index function is overkill right now.
And
$request->session()->forget("error");
$request->session()->forget("success");
Isn't really required cause once it's displayed it's forgetted
True, I updated the code now.
I am using get request.
Could you check if I am able to improve the code somehow. I mean the most common think to do when creating a website is to allow users to search, but somehow it's not to easy
So why are you don’t all that validation crap?
The logic here isn't really efficient
if ($request->search_query === null) {
// articles
$articles = HelpCenter::latest()->paginate(24);
// return view
return view("siteadmin.help.help", [
"articles" => $articles,
"categories" => $categories,
]);
}
As you use a validator
Just one line after
So, that logic is for when user vists the route for first time without any search.
I don't understand you. If you can not help, please dont reply.
lol
Isolate each function
I’ve literally told you twice what to do now.
Seems like a good idea using another route.
But using another route can maybe create a lot of new problems for me. I think. 😅
All you need to do is use query strings for filtering. That’s why they’re called query strings. You don’t need another route.
Can you give an example?
I already did that. I mean, my function is working perfect. I only need access to errors.
No, you don’t.
Because there should be any errors.
You don’t “validate” query strings in GET requests like that.
True, but it's more the idea of having access to errors.
If a user specifies a term and there’s no results, there’s no results.
You are right. But what if I want to 😅
I mean, I should have access to errors somehow. There will properly a case in future.
You only have errors after validating POST/PUT requests.
You don’t use validators like you’re trying to on GET requests
Thanks a lot. Now it makes a bit more sense.
Like, if I specify an incorrect user ID in Discord’s search, I don’t randomly get dumped to another page with errors. I just don’t get any results because the (incorrect) user ID I supplied didn’t match anything.
So the idea is, that when I am using PUT or POST, I dont have access to errors, as I will not need them?
Ok - I see that.
Or if I make a youtube search. I do not get any errors just no results?
Exactly.
Ah
But you have feedback for example that there's no results and i think that's what he tries to achieve
Which they’d have if they just showed the ->total() of pagination results after filtering.
Yes, what if I want to give user an errror like. Hi, you need to insert at least 2 characters in the search field.
But ok, youtube or other sites don't do that.
trans_choice('1 result|:count results', $articles->total());
Ok.
Put a minlength="2" attribute on your search input.
If you mean in the backend, don't I then need access to errors?
Mate. Stop going on about errors on the backend.
You need to set minlength on your input
If only I had explicitly said that? 🤷♂️
Ok.
I totally understand the idea now. And everything also works.
Can I ask about feedback on the code. I mean, would you guys have done it differently?
https://gist.github.com/qaiswardag/c2fc8c4c969ebf3936170e3fff88dfa4
You’ve already been given feedback on it by @frail anchor…
Just to be totally clear. So I should not use any input validation when using GET request?
Cause then I will delete that part of my code and just give user a message if there is no results.
This is all you need:
public function index(Request $request)
{
$articles = HelpCenter::latest()->when($request->query('search'), function ($query, $term) {
$query->where('headline', 'LIKE', '%' . $term . '%');
})->paginate(24);
$categories = HelpCenterCategory::all();
return view('siteadmin.help.help', [
'articles' => $articles,
'categories' => $categories,
]);
}
Query help centre model. If there’s a ?search in the query string, then use that to filter help centre models. Return results to view.
Your code seems really clean.
It's simple
Thanks so much. I have to read and understand it in order to be able to write it myself in future.
there's an error here 
So if you go to /help, it’ll show the first 24 articles. If you go to /help?search=subscription, it will show all articles with “subscription” in the headline.
Yes, totally and really easy to read and the code is simple.
Where dit you see an error?
you'll see when you use it
Don’t hold me in suspense like this 😅
actually i just realized i might be wrong, was thinking about the no return in closure