Laravel Bulk Insert - php

I am trying to find solution to below use case, using Laravel I am inserting bulk products with respective quantity and description. For example product1 with id:pro_1 with its quantity and description, same with other products. If I submit the form how I know for which product the quantity and description is.
#foreach(get_products_by_parent_name("Content") as $product_id => $product_name)
<div class="col-md-4">
<div class="form-group">
<h4><label class="p_name"><input type="checkbox" value="{{ $product_id }}"> {{ $product_name}} </label></h4>
<label>Quantity</label>
<input type="number" min="1" class="form-control"
name="qn_{{$product_id}}">
<label>Description</label>
<textarea class="form-control" name="desp_{{$product_id}}" style="resize: none;"></textarea>
</div>
</div>
#endforeach

Instead of giving all your field names a product ID, you could make them arrays (for example: desp_{{$product_id}} becomes desp[{{$product_id}}]). Looping through the results, you could create a new model with the data given.

Related

Laravel livewire: update text field based on first field value

I'm struggling to understand how livewire works.
I have two text fields. One is where the user enters information like prefixes and the second field with a read-only attribute where data will be displayed based on the first field value.
But for some reason, I can't populate the second field. All examples on the internet are how to take a value and return it back or generate a dropdown menu.
my blade template:
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">prefix</label>
<input wire:change="testing"
type="text"
class="form-control"
id="prefix"
name="prefix"
/>
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Code</label>
<input wire:model="part"
type="text"
class="form-control"
id="part"
name="part"
value="{{ $part }}"
/>
</div>
and
Livewire class:
class DoSomethingClass extends Component
{
public $prefix;
public $part;
public function testing()
{
$this->part = $this->prefix;
}
public function render()
{
return view('livewire.blade-template');
}
}
You should use wire:model to both input fields. Than, if you want to update the 2nd field on the input of the first, you can use the Livewire Lifecycle to update it.
public $prefix;
public $part;
public function updatedPrefix()
{
$this->part = $this->prefix;
}
Check this link for info on the updatedPrefix() hook: https://laravel-livewire.com/docs/2.x/lifecycle-hooks
(You can choose to use your testing() method too, but than you still need to use the wire:model directive to your second input.)
You can make what you're doing work just by adding wire:model="prefix" to your first input as suggested above.
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">prefix</label>
<input wire:change="testing"
type="text"
class="form-control"
id="prefix"
name="prefix"
wire:model="prefix"
/>
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Code</label>
<input wire:model="part"
type="text"
class="form-control"
id="part"
name="part"
value="{{ $part }}"
/>
</div>
And the component itself can just stay the same. When someone types anything in the first field then tabs or clicks away it will update the second field.
If you want the second field to update as the user types change it to wire:keydown="testing", it will make a call with every keystroke.
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">prefix</label>
<input wire:keydown="testing"
type="text"
class="form-control"
id="prefix"
name="prefix"
wire:model="prefix"
/>
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Code</label>
<input wire:model="part"
type="text"
class="form-control"
id="part"
name="part"
value="{{ $part }}"
/>
</div>
As mentioned above, if you use the updatedPrefix() method you won't need the wire:change or wire:keydown as Livewire will update the field as the user types.

How to get any column entry from another table of database based on the entry taken in form?

I am new to laravel and facing an issue. I have a database and there are two tables named groups and clients. I have a form and there are three fields: name, email and group. The group is dropdown select which is populated by groups entries from the database. Now while creating a new client, I want my clients table to have relation with groups table. It will work like this:
Will enter the name, email and will select the group it will associate with. The 'Client' table will get the id (There is a id column in the database, which is on auto_increament) of the 'Groups' table.
I have created one to one relation and when I put group_id manually in the database, it is showing the respective group. But I want the code to get the group_id while creating new entry.
Here is the code which is creating new entry:
Student::create([
'name' => request('name'),
'email' => request('email'),
'group_id' => request('group_name')// I want the group Id here,
'user_id' => auth()->user()->id
]);
return back()->with('success', 'The registration is completed');
Here is the form:
#extends ('layouts.app')
#section('main')
<div class="col-md-6">
<form method="post" action="/student" class="">
#csrf
<div class="form-group">
<label for="name">Client Name:</label>
<input class="form-control" type="text" name="name" required>
</div>
<div class="form-group">
<label for="email">Client Email:</label>
<input class="form-control" type="text" name="email" required>
</div>
<div class="form-group">
<label for="group_name">Select Group:</label>
<select class="form-control" name="group_name">
#foreach ($groups as $group)
<option>{{ $group->group_name}}</option>
#endforeach
</select>
</div>
<div class="form-group ">
<input class="btn btn-primary" type="submit" name="submit">
</div>
</form>
</div>
#endsection
Here is the error code:
SQLSTATE[HY000]: General error: 1364 Field 'group_group_name' doesn't have a default value (SQL: insert into `students` (`name`, `email`, `group_id`, `user_id`) values (Nayan Chowdhury, nayan.1aacl#gmail.com, Marketing, 1))
You are doing wrong. Try
<select class="form-control" name="group_name">
#foreach ($groups as $group)
<option value="{{ $group->id }}">{{ $group->group_name}}</option>
#endforeach
</select>

fill fields with auth user info when checkbox is checked

When the checkbox "Fill with auth user info" is clicked I want to populate the fields with the auth user info (name and surname) but it's not working. When the checkboxes are clicked is not working the fields are not populated with the auth user info.
What I have for now is in, https://jsfiddle.net/LahL166o/2/ the auth user info appears in this span
<span id="userData" data-name="John"
data-surname="Keane"></span>
The issue should be how to get the ticket type name in the jquery with laravel because as it is ther is no id with "name" or "surname" the result ids are: "namegeneral_1" and "surnamegeneral_1".
laravel form:
<form method="post" id="step1" action="">
{{csrf_field()}}
#if (!empty($allParticipants))
#if($allParticipants == 1)
<p>Fill all fields.</p>
#foreach($selectedRtypes as $k => $selectedRtype)
#foreach(range(1,$selectedRtype['quantity']) as $val)
<h6 class="text-heading-blue mb-3 pb-2 font-weight-bold">Participant - {{$val}} - {{$k}} </h6>
#if ($loop->first)
<div class="form-check">
<input class="form-check-input" id="fill_auth_info" type="checkbox">
<label class="form-check-label d-flex align-items-center" for="exampleRadios1">
<span class="mr-auto">Fill with auth user info.</span>
</label>
</div>
#endif
<span id="userData" data-name="{{ auth()->user()->name }}"
data-surname="{{ auth()->user()->surname }}"></span>
<div class="form-group font-size-sm">
<label for="participant_name" class="text-gray">Name</label>
<input type="text" id="name" name="participant_name[]" required class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="participant_surname" class="text-gray">Surname</label>
<input type="text" id="surname" required class="form-control" name="participant_surname[]" value="">
</div>
#endforeach
#endforeach
#endif
#else
<p>Is not necessary additional info.</p>
#endif
<input type="submit" href="#step2free"
id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
</form>
Html result:
<form method="post" id="step1form" action="">
<input type="hidden" name="_token" value="">
<h6 class="text-heading-blue mb-3 pb-2 font-weight-bold">Participant - 1 - general </h6>
<div class="form-check">
<input class="form-check-input" name="fill_with_auth_info" id="fill_auth_infogeneral_1"
data-id="general_1" type="checkbox">
<label class="form-check-label d-flex align-items-center"
for="fill_auth_info1">
<span class="mr-auto">Fill with auth user info.</span>
</label>
</div>
<span id="userData" data-name="John"
data-surname="Keane"></span>
<div class="form-group font-size-sm">
<label for="namegeral_1" class="text-gray">Name</label>
<input type="text" id="namegeral_1" name="participant_name[]" required class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="surnamegeneral_1" class="text-gray">Surname</label>
<input type="text" id="surnamegeneral_1" required class="form-control" name="participant_surname[]" value="">
</div>
<input type="hidden" name="all_participants" value="1" />
<input type="hidden" name="rtypes[]" value="1"/>
<h6 class="text-heading-blue mb-3 pb-2 font-weight-bold">Participant - 1 - re </h6>
<div class="form-check">
<input class="form-check-input" name="fill_with_auth_info" id="fill_auth_infore_1"
data-id="re_1" type="checkbox">
<label class="form-check-label d-flex align-items-center"
for="fill_auth_info1">
<span class="mr-auto">Fill with auth user info.</span>
</label>
</div>
<span id="userData" data-name="John"
data-surname="Keane"></span>
<div class="form-group font-size-sm">
<label for="namere_1" class="text-gray">Name</label>
<input type="text" id="namere_1" name="participant_name[]" required class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="surnamere_1" class="text-gray">Surname</label>
<input type="text" id="surnamere_1" required class="form-control" name="participant_surname[]" value="">
</div>
<input type="hidden" name="all_participants" value="1" />
<input type="hidden" name="rtypes[]" value="2"/>
<input type="submit" href="#step2free"
id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
</form>
jQuery:
let userData = $('#userData');
$("input[name = 'fill_with_auth_info']").on('click', function(e) {
let checked = $(this).is(':checked');
if (checked) {
$('#name').val(userData.data('name'));
$('#surname').val(userData.data('surname'));
} else {
$('#name').val(userData.data(''));
$('#surname').val(userData.data(''));
}
});
I think you may have a number of issues that are preventing this from working. I think most are stemming from your looping and accidentally assigning the same id to many items.
However, the code displayed above is a bit confusing as well - the blade file should produce something different from the HTML you show. I assume this was a later iteration? I will speak to the blade file, as it is easier to see, since its smaller.
To help get this working, I suggest the following: First, you have only one user, so move this:
<span id="userData" data-name="{{ auth()->user()->name }}"
data-surname="{{ auth()->user()->surname }}"></span>
outside of the loop (or put it inside the $loop->first block), so that the #userData id is only assigned once in the DOM.
Next remove the singular id #name from the input. Same for the #surname. The way you have it, it keeps assigning multiple id fields with the same id. The name="participant_name[]" is fine, and will work on POST.
It's not clear if you want the user name & surname to populate ALL the input fields, or just one near the place he checks the box. If you want to populate them all, add a class to the two inputs in the loop like 'firstName' and 'surname'. Then its easy to populate everything with your jquery:
$('.firstName').val(userData.data('name'));
$('.surname').val(userData.data('surname'));
If, however, you wish to populate only the one that is checked, it is a little more complex (but not hard). You will need to have a check box within each loop next to the set of inputs. Easiest way to track the right checkbox against the input fields is to add an iterator into the loop (assign it and then iterate: $x ++) and then use this on the checkbox in a data field (eg data-count={{$x}} ) and now add an id onto the inputs like so:
id = 'name{{$x}}'
id = 'surname{{$x}}'
From here, add to your jquery code to check which box was checked by getting the count from the data-count field on the checkbox. Then, you can identify the right inputs to add the User's name into by id:
var count = $(this).data('count');
$('#name'+count).val(userData.data('name'));
$('#surname'+count).val(userData.data('surname'));
Hope this helps

Laravel record display from database

I have this leftJoin in my Laravel project
products = DB::table('products')
->leftJoin('destinations','products.id','=','destinations.product_id')
->get();
Tables:
Product: id,name Destinations:id,product_id,destination,quantity,target_person
but i don't knwo how to display leftJoin,(i just want to display name form product and quantity,destination ,target_person from destinations)
and i want to add new record
This is my form :
<form action="." method="POST" id="formid">
{{ csrf_field() }}
<div class="form-group">
<label for="first_name">Product name</label>
<input class="form-control" name="name" >
</div>
<div class="form-group">
<label for="last_name">Quantity</label>
<input class="form-control" name='quantity'>
</div>
<div class="form-group">
<label for="last_name">Destination<label>
<input class="form-control" name='Destination'>
</div>
<div class="form-group">
<label for="last_name">Target Perosn</label>
<input class="form-control" name='target_person'>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
i never do this,please help me this,im beginner in this.
As you have mentioned, I just want to display name from product and quantity,destination ,target_person from destinations. You have to use table alias like:
products = DB::table('products as t1')
->leftJoin('destinations as t2','t1.id','=','t2.product_id')
->select('t1.name', 't2.quantity', 't2.destination', 't2.target_person')
->get();

Display form fields using loop in twig

I am working on a form that collects number of credit card loans a user has, a person can add as many credit card fields as he desires so the problem I am having is how to render the form fields when a user has multiple entries
Here is the twig dump and as you can see in this example user has 2 entries and I would like to use loop to render these fields.
At present the fields look like this
That's because I am using {{ form_rest(form) }} to dump the fields
While checking the source of the creditcard fields this is what i see
<div>
<label class="required">0</label>
<div id="examplebundle_debt_creditcards_0">
<div>
<label for="examplebundle_debt_creditcards_0_balance" class="required">Balance</label>
<input type="number" id="examplebundle_debt_creditcards_0_balance" name="examplebundle_debt[creditcards][0][balance]" required="required" value="3000">
</div>
<div>
<label for="examplebundle_debt_creditcards_0_amountDue" class="required">Amount due</label>
<input type="number" id="examplebundle_debt_creditcards_0_amountDue" name="examplebundle_debt[creditcards][0][amountDue]" required="required" value="300">
</div>
<div>
<label for="examplebundle_debt_creditcards_0_interestRate" class="required">Interest rate</label>
<input type="number" id="examplebundle_debt_creditcards_0_interestRate" name="examplebundle_debt[creditcards][0][interestRate]" required="required" value="3">
</div>
</div>
</div>
So my question is, how can i use a loop to display the correct number of creditcard related fields dynamically?

Categories