#Return a view with ->withInput() and ->with()

110 messages · Page 1 of 1 (latest)

covert aurora
#

Actually nvm it is, I forgot. But either way, clean up this code share

shut mist
#

Remove which code?

covert aurora
#

You have tons of comments and messy indents

#

And 0 syntax highlighting

#

It makes it absurdly annoying to look at

shut mist
#

@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.

covert aurora
#

Add <?php to the start of the file

shut mist
#

Done, I knew I was missing something 😅

covert aurora
#

->withInput isn't something you can use on view()

#

Works for redirects, not views

shut mist
#

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.

covert aurora
#

The "problem" is that you're keeping everything on the same page

#

So you can't do withInput since you're not redirecting anywhere

shut mist
#

So I should make a hole new page for search with controller etc.

covert aurora
#

No necessarily, you just can't do withInput

shut mist
#

Thats does not make any sense. So user will actually not be able to see their old input.

covert aurora
#

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

shut mist
#

"Just flash the values normally" can you give a code example?

covert aurora
shut mist
#

@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());

covert aurora
#

I'm not at my PC right now, but I believe this is the same issue as before that the withErrors are on redirects

shut mist
#

True

#

Maybe I can use flash for withErrors also?

covert aurora
#

Maybe, I'm not sure

shut mist
#

Ok, thanks for teaching me limitations for view and help.

zealous oracle
#

@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
#

I deleted the other topic.

zealous oracle
#

No, you didn’t. I just closed it.

shut mist
#

But I clicked delete.

zealous oracle
#

frail anchor
#

I don't think withErrors work with flash()

shut mist
#

@frail anchor Ok. But do you know how I can get access to withErrors on view if I can not use withErrors with flash?

frail anchor
#

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 ?

shut mist
#

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 😅

frail anchor
zealous oracle
#

@shut mist What are you actually trying to do here?

#

Why are you trying to “access” withErrors?

zealous oracle
#

I’ve already told you that searches should be done with GET requests.

#

So there’s no “input” or “errors” to access.

frail anchor
#

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

shut mist
shut mist
zealous oracle
frail anchor
#

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

shut mist
frail anchor
#

You can do another route

#

for searching

shut mist
covert aurora
#

lol

frail anchor
#

Isolate each function

zealous oracle
shut mist
zealous oracle
#

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.

zealous oracle
#

/articles?search=Foo

#

Same route. Query string filters results.

shut mist
zealous oracle
#

No, you don’t.

#

Because there should be any errors.

#

You don’t “validate” query strings in GET requests like that.

shut mist
zealous oracle
#

If a user specifies a term and there’s no results, there’s no results.

shut mist
zealous oracle
#

You only have errors after validating POST/PUT requests.

#

You don’t use validators like you’re trying to on GET requests

shut mist
zealous oracle
shut mist
#

So the idea is, that when I am using PUT or POST, I dont have access to errors, as I will not need them?

shut mist
shut mist
#

Ah

frail anchor
zealous oracle
shut mist
#

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.

zealous oracle
#
trans_choice('1 result|:count results', $articles->total());
zealous oracle
shut mist
zealous oracle
#

Mate. Stop going on about errors on the backend.

frail anchor
#

You need to set minlength on your input

zealous oracle
#

If only I had explicitly said that? 🤷‍♂️

shut mist
#

Ok.

#

I totally understand the idea now. And everything also works.

zealous oracle
shut mist
zealous oracle
#

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.

shut mist
#

Your code seems really clean.

frail anchor
#

It's simple

shut mist
zealous oracle
#

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.

shut mist
shut mist
covert aurora
zealous oracle
covert aurora