Can I save label title in laravel? - laravel

Is there a way to save label title in laravel.
<label for="name">Name</label>
Suppose I want to save "Name" in database.

You can use a hidden field and make its value the same as the label name.
<input name="label" value="Name" type="hidden">
Or you can use something like
<form action="youraction" >
<label for="male">Male</label>
<input type="radio" name="gender" id="male" value="male"><br>
<input type="submit" value="Submit">
</form>
W3Schools - documentation
It will also depend on your use-case may be more information will be helpful.

according to your structure, the label name comes from db, so when you are rendering the blade, you can make a hidden input with value of label name like below:
<form action="{{ route('sampleRoute') }}" >
<label for="{{ $DBVARIABLE }}">{{ $DBVARIABLE }}</label>
<input type="text" name="{{ $DBVARIABLE }}" id="{{ $DBVARIABLE }}" value="{{ $DBVARIABLE }}" style="visibility: hidden;">
<input type="submit" value="Submit">
</form>

Related

How to I can show date in laravel blade from my table in this input field

How to I can show date in laravel blade from my table in this input field
<div class="form-group">
<strong>Date:</strong>
<input type="date" name="date" class="form-control" value="{{ $productInovoice->date }}" placeholder="Date">
</div>
I think you should format the date to fix that.
try this:
<div class="form-group">
<strong>Date:</strong>
<input value="{{ $productInovoice->date->format('Y-m-d') }}" type="date" name="date" class="form-control" placeholder="Date">
</div>
or you can use the model attribute like below, put this function in your product model:
public function setDateAttribute($value)
{
$this->attributes['date'] = Carbon::parse($value)->format('Y-m-d');
}
This worked for me:
<input type="date" class="form-control" id="published_at" name="published_at" required value="{{ date_format(date_create($book->published_at), 'Y-m-d') }}">
<input type="date" name="date" class="form-control" value="{{ $productInovoice->created_at->format('d M y') }}" placeholder="Date">
You can try this:
<input type="date" name="date" class="form-control"
value="{{ date_format(date_create($productInovoice->date), 'd/m/Y')) }}">

I can't get back()->withInput() to work, the documentation seems a little sparse on how it should work

I'm using blade templates and this is how my form looks:
<form action="{{ route("user-sessions.store") }}" method="POST">
#csrf
<div class="form-group">
<label for="e-mail">E-mail Address</label>
<input name="email_address" class="form-control" type="email" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" type="password" />
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" />
</div>
</form>
Does back()->withInput() only work with {{ Form::open() }}? If so, the 7.x documentation doesn't say that it seems. I would think if you need a 3rd party library to get this to work, the documentation should say so. If that's not the issue, then what am I doing wrong?
I'm using Laravel 7.x.
if you use html collective, it automatically puts old values in form inputs. But when use pure html to create form inputs you have to use old method for input values. like this:
<form action="{{ route("user-sessions.store") }}" method="POST">
#csrf
<div class="form-group">
<label for="e-mail">E-mail Address</label>
<input name="email_address" class="form-control" type="email" value="{{old('email_address')}}"/>
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" type="password" value="{{old('password')}}"/>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" />
</div>
</form>

Submit checkbox values to Controller

I am having the following form, which I would like to submit to my backend.
<form id="revisionFilter" action="{{ route('revision.filter') }}" method='POST'>
<input class="form-check-input" type="checkbox" value="" id="checkbox1">
<label class="form-check-label" for="checkbox1">
1
</label>
<input class="form-check-input" type="checkbox" value="" id="checkbox0">
<label class="form-check-label" for="checkbox0">
0
</label>
<label class="form-check-label" for="checkboxNull">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="checkboxNull">
Null
</label>
<input type="hidden" name='_method' value='POST'>
<input id="revisionFilterSubmit" type="submit" class='btn btn-danger btn-sm' value='Filter'>
</form>
My backend looks like the following:
routes:
Route::post('/revision/filter', 'RevisionController#filter')->name('revision.filter');
RevisionController:
public function filter(Request $request)
{
Log::info("Request: ");
Log::info($request);
return redirect()->route('revision.rindex');
}
My problem is that when I press the button I am getting redirected to:
The page has expired due to inactivity.
Please refresh and try again.
Instead I would like to see the request with the values of the checkboxes to implement the db saving functionality.
I appreciate your replies!
since I've already made a comment. I will put my answer in the answer box lol.
To answer you, you're not getting any checkbox value since you're checkboxes don't have any name attribute.
You need to put a name attribute to them. E.g.,
<input class="form-check-input" type="checkbox" value="" name="checkbox0" id="checkbox0">
From there, you should now be able to see the value of the checkbox via its name.
Just put same name to your checkbox.For example
<input class="form-check-input" type="checkbox" value="" id="checkbox1" name="revisionFilter">
<input class="form-check-input" type="checkbox" value="" id="checkbox2" name="revisionFilter">

How do I save the oldvalue if the input name is variable?

The oldvalue is usually set with the name attribute, in my case the name attribute is variable depending on the id name="option{{$question->id}}", so I can't find the way to save and load the olvalue, this is my code:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" name="option{{$question->id}}" id="question" autocomplete="off" value="1" data-oldvalue="{{old('option'.$question->id) }}"> Yes
</label>
<label class="btn btn-primary">
<input type="radio" name="option{{$question->id}}" id="question" autocomplete="off" value="0" data-oldvalue="{{old('option'.$question->id) }}"> No
</label>
</div>
Not sure if the If there is any other approach to get my oldvalues loaded (after validation errors) I would like to know.
Extra info: I'm not sure if the reason the old values don't load is the autocomplete="off". So I would like to know how to alert or get my oldvalue.
Set the values directly and use the default value if no old value is available.
value="{{ old('option' . $question->id, 1) }}"
value="{{ old('option' . $question->id, 0) }}"

can't get old() to work in laravel

in my blade i have 2 radio inputs
<form class="" action="{{route('admin.album.search')}}" method="post">
<input id="slug" type="radio" name="search" value="slug" >
<input id="id" type="radio" name="search" value="id">
in the same blade file I test to see old value of radio input
{{ old('search')}}
When I select first radio button and submit the form and page reloads I expect that old will be selected radio button value but nothing comes out. What could I be doing wrong?
Try this:
<form class="" action="{{route('admin.album.search')}}" method="post">
{{ csrf_field() }}
<input id="slug"
type="radio"
name="search"
value="slug"
{{ (old('search') == 'slug') ? 'checked': '' }}>
<input id="id"
type="radio"
name="search"
value="id"
{{ (old('search') == 'id') ? 'checked': '' }}>
N.T. If you submit post form request you must be specify csrf_field(). Otherwise it gives you token mismatch exception.

Resources