I have the following URL:
https://example.com/?email=test#test.com
then I have the following blade template
<input value="{{Request::query('email') or old('email')}}">
But it's not displaying the email I passed in the get parameter into the input. Instead, it's displaying the value 1
Tried searching haven't found a solution that works.
That's because you have {{Request::query('email') or old('email')}} which is conditional. It will always return true or false.
Try any of these:
{{Request::query('email') || old('email')}}
or
{{Request::query('email') ? Request::query('email') : old('email')}}
You can try this:
<input value="{{ request()->email }}">
Related
I'm using soft deletes and trying to make a function to restore the deleted row. Really everything to do with submitting the form doesn't work, even deleting...
My tags are within a forelse loop, maybe that's the cause??
Route is (this is above my resource route):
Route::post('/post/restore/{id}', [PostController::class, 'restore'])
->name('post.restore');
Controller is:
public function restore($id)
{
dd($id);
}
Form/view is:
<form action="{{route('post.restore', $post->id)}}" method="POST">
#csrf
<button type="submit" class="dropdown-item popup" data-confirm="Would you like to restore?">Restore</button>
</form>
After submitting, it just takes me to:
domainURL/post/null
and gives a 404 error
Any advice?? I also tried it without the {id} at the end of the route, same results
I think you are using the wrong syntax for route function, instead of this:
route('post.restore', $post->id)
you should use it like this:
route('post.restore', ['id' => $post->id])
you can read more here as well.
I figured it out... my "would you like to restore?" javascript function was breaking the form. I was using javascript to popup a confirmation message before submitting and during that process it lost the ID
I got rid of that and it works fine
Thank you!
I'm using Laravel 5.8, and have several input fields which of course has an old() directive on every value="" tag.
This is my example right now:
<input class="form-control input-md" name="contact_name" type="text" value="#if($edit){{ $ad->contact_name }}#else{{ old('contact_name')}}#endif">
I now that if I use this: {{ old('contact_name', "John")}}
The default value will be "John"
But I want to do a check if there is a user logged in and prefill that input with the User contact name.
My idea is something like this:
value="#if($edit){{ $ad->contact_name }}#else{{ old('contact_name', Auth::user()->name)}}#endif
And it works! But of course, it throws: Trying to get property 'name' when I get an incognito window.
So, how do I evaluate logged in users and prefill this?
You can use the optional helper:
{{ old('contact_name', optional(Auth::user())->name) }}
I noticed that below construction is not correct for Blade
<div class="constant #if($some_condition)optional#endif">
cause this is converted to in PHP which is invalide
<div class="constant <?php if($some_condition): ?>optional<?php endif; ?>">
To make this statement work I need to put close tag on different line but it looks very bad, especially if there are many others attribute.
<div class="constant #if($some_condition)optional
#endif" data-id="1" tabindex="2" data-etc="...">
What is the right way of rendering such kind of conditions in Blade?
Try the short version of if statement.
$some_condition ? 'optional' : ''
Using the code above, if the condition is true it will return the the string 'optional', if the condition is not true it will return an empty string I mean this ''. Note, an empty string as attribute do nothing inside Html element.
In your case it should look like this:
<div class="constant {{ $some_condition ? 'optional' : '' }}">
I try to add class on a label if an the input has old datas.
I try this on my label:
<?php empty(old(email)) ? '' : 'active' ;?>
But it doesn't work. I also try with "isset" but I have an error exception.
Can you help me ? Thank you
The old method takes the input name as a string. You can also use blade syntax to make things easier.
<label class="label{{ old('email') ? ' active' : '' }}" >Test</label>
The old function accepts string as its first parameter. You're likely getting the error because constant email does not exists. Instead, call it with string old('email')
<?php empty(old('email')) ? '' : 'active' ;?>
if the validation fails it's not clear to me how i could pass the old input to fill again the form.
I mean, I know how to pass the data when using the Validator class and redirecting after fail with the method withInput(), but I'm trying to learn how to use Form Requests provided in laravel 5. Thanks
$username = Request::old('username');
or in view:
{{ old('username') }}
Read more: http://laravel.com/docs/5.0/requests#old-input
You can redirect with old data like below with validation error
return redirect()->back()->withErrors($validator)->withInput();
<input type="text" name="username" value="{{ old('username') }}">
you must also define withInput() for the http route when redirecting to a page for example
return back()->withInput();