Laravel: Input old for date field - laravel

I want to have my date field remember my old input
Is there any way i can do this?
Here's my code
<div class="form-group">
<label for="date1">Date From: </label>
<input id="date1" name="date1" type="date" value="<?php echo date('Y-m-d'); ?>" class="form-control">
</div>
<div class="form-group">
<label for="date2">Date To: </label>
<input id="date2" name="date2" type="date" value="<?php echo date('Y-m-d'); ?>" class="form-control">
</div>
Please Help. Thank You!

If you want to have a default but also remember the user provided input in case of any error you could do something like this:
<input type="date" name="date2" value="{{ old('date2', date('Y-m-d')) }}">
Then in your controller when you redirect back make sure to call the ->withInput()method.

First you need to redirect from your controller "With inputs" something like this:
return redirect('some/form/route')->withInput();
Then in your template:
Request::old('date_field')
Or in a blade template:
{{ old('date_field') }}
More detail in the "Old Input" section in L5 docs

Related

How to save multiple inputs to the database when the input is in loop laraval 8

I am working on a Laravel 8 project. I have a row with 3 inputs and a button which can add more line of row. Doesn't matter how many lines I add only the first row is submitted in the database. Please help me with that. Thank you!
index.blade.php:
<h4>Add Products</h4>
<form action="{{ url('insert-products') }}" method="POST" enctype="multipart/form-data">
#csrf
<select class="form-select" name="cateId">
<option value="">Select a Category</option>
#foreach ($category as $item)
<option value="{{ $item->id }}">{{ $item->name }}</option>
#endforeach
</select>
<br>
<label for="">Product Name</label><br>
<input type="text" name="productName" id="quantity2" required><br>
<br>
<input type="checkbox" name="status" required>
<br>
<div class="customer_records">
<input type="text" name="productName" id="quantity_img2" required>
<input type="text" name="productVariant" required>
<input type="text" name="productValue" required>
<a class="extra-fields-customer" href="#">Add More Customer</a>
</div>
<div class="customer_records_dynamic"></div>
<button type="submit" class="btn btn-outline-success waves-effect" id="type-success">Add Product</button>
</form>
controller:
public function insert(Request $request){
$products = new Products();
$products->cateId = $request->input('cateId');
$products->productName = $request->input('productName');
$products->status = $request->input('status') == TRUE ? '1':'0';
$products->productVariant = $request->input('productVariant');
$products->productValue = $request->input('productValue');
if($products->save()){
return redirect('/categories')->with('status',"Products Added Succesfully");
}
else{
return redirect('/categories')->with('status',"Something went wrong");
}
}
You have several transmissions involved:
The request
First of all, you need to make sure that the values are sent at all. Seeing your code, I presuppose that on the request level a single value is being sent. You will need to indicate that there will be multiple values involved, such as
<label for="">Product Name</label><br>
<input type="text" name="productName[]" id="quantity2" required><br>
<br>
<input type="checkbox" name="status[]" required>
<br>
<div class="customer_records">
<input type="text" name="productName[]" id="quantity_img2" required>
<input type="text" name="productVariant[]" required>
<input type="text" name="productValue[]" required>
<a class="extra-fields-customer" href="#">Add More Customer</a>
</div>
Notice the [] indicating multiplicity.
Sidenote: I would strongly discourage you from hard-coding ids into repeated templates, as they will end up being duplicated, which makes your HTML invalid.
The application
While the previous section should fix an issue, it's not guaranteed that it's the only issue. It makes sense to debug and see what you have on application-side sent by the request. If you have all the values, then all is well and good. If not, then investigate the reason and fix it.
The database
You need to make sure that your code attempts to store everything. If so, then a further question is whether it's done successfully. If not, then you will need to debug your code to see what's wrong.

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')) }}">

How to put route url in the echo in laravel form inside a controller?

This is the code inside the controller of my laravel:
echo '
<form action="'.$link.'" method="POST">
';
csrf_token();
echo '
<div class="form-group">
<label for="reason">Total Worked Hours::</label>
<input type="text" class="form-control" disabled="disabled" id="hours" value="'.$totalhours.'"style="color:red;font-weight:bold;"">
</div>
<div class="form-group">
<label for="reason">Enter Your Reason:</label>
<input type="text" class="form-control" id="text">
</div>
<button type="submit" class="btn btn-default">Submit Reason</button>
</form>
';
and the error is 419 page expired what is the proper code for this.
change the line
csrf_token();
by
echo csrf_field();
Or, depending on your ajax, remove it completly.

How can I get the role name

<?php
$role_class = new RoleModel;
$role_name = "";
foreach ($role_class->getInfoById("role_id") as $row) {
# code...
$role_name = $row->role_name;
}
?>
<fieldset class="g12 go pb_s">
<legend>Basic Info</legend>
<div class="g12 go">
<div class="g7 go">
<div class="g2">
<label for="groupName">Role Name:</label>
</div>
<div class="g4">
<input type="text" placeholder="Type Role Name here..." name="role_name" value="<?php echo $role_name; ?>" required="required">
</div>
</div>
</div>
</fieldset>
You can use PHP's $post to retrieve the value of the input tag via the name of the HTML tag.
For Example, change the method in your form and then echo out the value by the name of the input:
In your Html
<form name="form" action="" method="post">
<input type="text" name="role_name" id="role_name" value="<?php echo $role_name;?>">
</form>
In your .php, you can get the value like below
$role_name=$post['role_name'];
Since the editor edited his questions, i will add more example.
For example, i got a list of data want to show in the html, in controller, i will put into an array first.
$form_fields = array('username', 'ref', 'password', 'password_confirm')
after that in html, i will do like this
<?php foreach($__form_fields as $field) {?>
<input type="text" placeholder="<?php echo $field?>" name="<?php echo $field? >" value="" required="required">
<?php }?>
which the above code will loop and the input field will have he name

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) }}"

Resources