How to get old input array in blade laravel - laravel

I have some code like this
<div class="form-group">
<label for="tag">Tag</label><br>
<input type="text" data-role="tagsinput"
class="form-control form-control-lg #error('tag') is-invalid #enderror"
id="tag" name="tag[]" value="{{old('tag')}}" placeholder="Enter tag">
#error('tag') <div class="text-danger"> {{ $message }} </div> #enderror
</div>
how to get old value array in laravel blade, in this case i want to get old value of tag?

use dot notation with index
as suggested here https://laracasts.com/discuss/channels/laravel/input-old-and-array
<input type="text" data-role="tagsinput"
class="form-control form-control-lg #error('tag') is-invalid #enderror"
id="tag" name="tag[]" value="{{old('tag.0')}}" placeholder="Enter tag">
...
<input type="text" data-role="tagsinput"
class="form-control form-control-lg #error('tag') is-invalid #enderror"
id="tag" name="tag[]" value="{{old('tag.1')}}" placeholder="Enter tag">

I think the better solution for this is to do it with javascript if you have one input so you need to store the array in javascript variable then just add the value to the input.

<select name="groups[]" class="multi-default" id="groups" placeholder="Groups" multiple>
<option value="" placeholder>Groups</option>
#foreach ($groups as $group)
<option value="{{ $group->id }}" title="{{ $group->id }}"
{{is_array(old('groups',$groups??[]))&&in_array($group->id,old('groups',$group_data??[]))?'selected':null}}</option>
#endforeach
</select>

It will definitely work
#if (!is_null(old('product_highlights')))
#foreach (old('product_highlights') as $highlight)
{{$highlight}}
#endforeach
#endif

Related

How to get select menu session data in another page laravel 8

I can successfully get input session data on another page but I can't get select menu selected data in another page.
This is my first form
<form action="{{ route('admin.create.step.one.post') }}" method="POST">
#csrf
<label for=""> Student Name </label>
<input type="text" name="student_name" value="{{ $report->student_name ?? '' }}">
<br>
<label for=""> Email </label>
<input type="text" name="student_email" value="{{ $report->student_email ?? '' }}">
<br>
<label for=""> Phone number </label>
<input type="text" name="student_phone" value="{{ $report->student_phone ?? '' }}">
<label for=""> Group </label>
<select name="group" id="">
<option value="1" {{ $report->group ?? '' }}> Science </option>
<option value="2" {{ $report->group ?? '' }}> Arts </option>
</select>
<button type="submit"> Preview </button>
</form>
I want to get session data in this page.
I can't get select menu session data.
<form action="">
#csrf
<table>
<td> {{ $report->student_name }} </td>
<td> {{ $report->student_email }} </td>
<td> {{ $report->student_phone }} </td>
<td> {{ $report->group }} </td>
</table>
</form>
As far as I understood from your question you want to select the item within your select input.
You are just missing this line:
{{ $report->group == 1 ? 'selected' :'' }}
<form action="{{route('admin.create.step.one.post')}}" method="POST">
#csrf
<label for="student_name">Student Name</label>
<input type="text" name="student_name" id="student_name" value="{{ $report->student_name}}">
<br>
<label for="student_email">Email</label>
<input type="text" name="student_email" id="student_email" value="{{ $report->student_email}}">
<br>
<label for="student_phone">Phone number</label>
<input type="text" name="student_phone" id="student_phone" value="{{ $report->student_phone}}">
<br>
<label for="group">Group</label>
<select name="group" id="group">
<option value="1" {{ $report->group == 1 ? 'selected' :'' }}>Science</option>
<option value="2" {{ $report->group == 2 ? 'selected' :'' }}>Arts</option>
</select>
<button type="submit">Preview</button>
</form>
Now, if you need are storing in the session() something and not getting it please update your question with your Controller and I will update my answer to help you further.
I see that you want to get the selected item when you submitted and an error appeared so if you want to get the old value
you can do so using old() method in your blade.
{{ old('group') == 1 ? 'selected' :'' }}
<form action="{{route('admin.create.step.one.post')}}" method="POST">
#csrf
<label for="student_name">Student Name</label>
<input type="text" name="student_name" id="student_name" value="{{ $report->student_name}}">
<br>
<label for="student_email">Email</label>
<input type="text" name="student_email" id="student_email" value="{{ $report->student_email}}">
<br>
<label for="student_phone">Phone number</label>
<input type="text" name="student_phone" id="student_phone" value="{{ $report->student_phone}}">
<br>
<label for="group">Group</label>
<select name="group" id="group">
<option value="1" {{ old('group') == 1 ? 'selected' :'' }}>Science</option>
<option value="2" {{ old('group') == 2 ? 'selected' :'' }}>Arts</option>
</select>
<button type="submit">Preview</button>
</form>

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.

Modify Breeze template in Laravel

I am working with Breeze Template in Laravel. Here I altered HTML code
<input type="email" name="email" class="form-control" id="exampleInputEmail1">
to Breeze Code
`<x-input type="email" name="email" class="form-control" id="exampleInputEmail1" />`.
But now how can I alter this HTML code
<select class="form-control" id="exampleFormControlSelect1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
to Breeze Code ?
Probably this is what your looking for I guess !!
<x-dropdown class="form-control" id="exampleFormControlSelect1">
<xd-item>1</xd-item>
<xd-item>2</xd-item>
</x-dropdown>
It is probably too late, but I faced the same problem. Surprisingly, there is not wrapper for select in Breeze.
So you can make your own wrapper like that:
Blade component (resources/views/components/select.blade.php):
#props(['disabled' => false])
<select {!! $attributes->merge(['class' => 'rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50']) !!}>
{{ $slot }}
</select>
And use it this way:
<x-select id="select" class="block w-full" name="field">
<option value="" selected disabled hidden>{{ __('Choose an option') }}</option>
<option value="1">Some</option>
<option value="2">options</option>
<option value="3">here</option>
</x-select>

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

The recaptcha is above the button

I have my recaptcha, it's working but the problem that I have it's that the submit button is under the captcha, I mean when I push the submit button I click the captcha too, I have tried with z-index but it does not worrk, how can I fix it?
<form action="{{ url('ticket/store') }}" method="post" id="ticket_form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<input type="text" class="form-control" placeholder="RUT" name="rut" required="">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Nombre" name="name" required="">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Correo" name="email" required="">
</div>
<div class="form-group">
<input type="number" class="form-control" placeholder="Teléfono" name="phone" required="">
</div>
<div class="form-group">
<select name="case_type" class="form-control" required="">
<option value="">- Tipo de Caso -</option>
<option value="1">Felicitación</option>
<option value="2">Reclamo</option>
<option value="3">Sugerencia</option>
</select>
</div>
<div class="form-group">
<select name="id_branch_office" class="form-control" required="">
<option value="">- Sucursal -</option>
#foreach($branch_offices as $branch_office)
<option value="{{ $branch_office->id_branch_office }}">{{ $branch_office->view_name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<textarea placeholder="Mensaje" class="form-control" rows="5" name="message" required=""></textarea>
</div>
<center>{!! htmlFormSnippet() !!}</center>
<br>
<button id="send" type="submit" class="btn btn-primary btn-orange">Enviar Mensaje</button>
</form>
You can test it in the next website jisparking.cl
Thanks
If you inspect the page (in Chrome you can do right click -> inspect) and check the box of the generated recaptcha, you can see that it stretches well over the button.
To stop the extra content from overflowing the container, you can add some CSS.
Try this
<center style="overflow: hidden">{!! htmlFormSnippet() !!}</center>

Resources