How to insert two serial numbers with one insert into - html

I try to make it possible to lend two books at once my problem is that only one value gets forwarded. The form looks like this:
In the view I tried to rename the field to "serial number2" because I thought that he just ignored the second serial number because of the same name.
This is my View:
<div class="card">
<div class="card-header">{{ __('Lend a book') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('borrow.store') }}">
#csrf
<div class="form-group row">
<label for="serialnumber" class="col-md-4 col-form-label text-md-right">{{ __('Please scan the Serialnumber of the book') }}</label>
<div class="col-md-6">
<input id="serialnumber" type="text" class="form-control{{ $errors->has('serialnumber') ? ' is-invalid' : '' }}" name="serialnumber" value="{{ old('serialnumber') }}" required #if (Session::has('autofocus')) autofocus #endif>
#if ($errors->any())
<div class="alert alert-danger">The book is not in stock.
<ul>
</ul>
</div>
#endif
</div>
</div>
<div class="form-group row">
<label for="serialnumber" class="col-md-4 col-form-label text-md-right">{{ __('') }}</label>
<div class="col-md-6">
<input id="serialnumber" type="text" class="form-control{{ $errors->has('serialnumber') ? ' is-invalid' : '' }}" name="serialnumber" value="{{ old('serialnumber') }}" required #if (Session::has('autofocus')) autofocus #endif>
#if ($errors->any())
<div class="alert alert-danger">The book is not in stock..
<ul>
</ul>
</div>
#endif
</div>
</div>
<div class="form-group row">
<label for="comment" class="col-md-4 col-form-label text-md-right">{{ __('comment') }}</label>
<div class="col-md-6">
<!-- <input id="ma_id" type="text" class="form-control{{ $errors->has('ma_id') ? ' is-invalid' : '' }}" value="{{ old('ma_id') }}" required> -->
<input id="comment" type="text-field" class="form-control" name="comment" placeholder="Test">
#if ($errors->has('comment'))
<span class="invalid-feedback">
<strong>{{ $errors->first('comment') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="ma_id" class="col-md-4 col-form-label text-md-right">{{ __('scan membercard to identify yourself') }}</label>
<div class="col-md-6">
<!-- <input id="ma_id" type="text" class="form-control{{ $errors->has('ma_id') ? ' is-invalid' : '' }}" value="{{ old('ma_id') }}" required> -->
<input id="ma_id" type="password" class="form-control" name="ma_id" required>
#if ($errors->has('ma_id'))
<span class="invalid-feedback">
<strong>{{ $errors->first('ma_id') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('send') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
This is my Controller:
public function store(bookRequest $request)
{
//if( !book::find($request->get('serialnumber'))->exists() ) {
$this->middleware('guest');
request()->validate([
'serialnumber' => 'required',
'serialnumber' => 'required',
'ma_id' => 'required'
]);
book::create($request->all());
return redirect()->route('book.index');
}
There is no error but the problem is that he writes only one serial number into the database as described above and completely ignores the other one that comes into the second field. My question is what I have to adjust so that the second serial number with the same data can be written into the database.

You cannot have two items with the same name (for example serialnumber) in the same form, otherwise only one value is going to be processed. Please check the inspection tool in the browser. If you need multiple inputs with the same "name" append [] to the name (for example name=serialnumber[]).
More info at HTML 5 spec
In the other hand, you have multiple items with the same ID. That's another issue to fix in some point.
Once you have your data in the controller you will have to generate your insert data accordingly
$requestData = $request->validated();
$data = [
[
'serialnumber' => $requestData['serialnumber'][0],
'comment' => $requestData['comment'],
'ma_id' => $requestData['ma_id']
],
[
'serialnumber' => $requestData['serialnumber'][1],
'comment' => $requestData['comment'],
'ma_id' => $requestData['ma_id']
]
];
Book::insert($data);

Make input's name as array.
Replace name="serialnumber" with name="serialnumber[]". When form is submitted, you will receive an array with both serial numbers.

Related

How can I solve " Invalid datetime format: 1366 Incorrect integer value" Exception in Laravel 7?

I'm quite new to Laravel, I would like to update a column in mySQL database named "client_id" the name of the table is "projects" I would like to insert this column when a Client creates a new Project, because the project belongs to the Client who created it. "client_id" is the Primary Key (id) in the table called "clients". There is a relationship between "clients" table and "projects" table. I have written some code to solve this problem but I'm getting this Exception and the "client_id" column is not updated.
Please help.
Store Method in my Controller:
/**
* Store a newly created Project in storage.
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'description' => 'required|string|max:255',
'start_date' => 'required|date',
'start_time' => 'required|string|max:10',
]);
$project = Project::create([
'name' => $request->name,
'description' => $request->description,
'start_date' => $request->start_date,
'start_time' => $request->start_time,
]);
$loggedinId = Auth::id();
$project->update(['created_by' => $loggedinId]);
$userId = $loggedinId;
$clientId = Client::where('user_id', '=', $userId)->pluck('id')->all();
DB::table('projects')->where('created_by', '=', $loggedinId)->update([ 'client_id' => $clientId]);
return redirect()->back()->with('message', 'You have successfully submitted your Job Card');
}
Relationship in my Client Model:
/**
* #return HasMany
*/
public function projects()
{
return $this->hasMany(Project::class);
}
Relationship in my Project Model:
/**
* #return BelongsTo
*/
public function client()
{
return $this->belongsTo(Client::class, 'client_id');
}
Form In my Blade View:
<form method="POST" action="{{ route('client-jobcard.store') }}">
#csrf
<div class="form-group row">
<div class="col-sm-8">
<label for="name">{{ __('Job Card Name') }}</label><span class="required">*</span>
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>
#if ($errors->has('name'))
<span class="invalid-feedback">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<div class="col-sm-8">
<label for="description">{{ __('Job Card Description') }}</label><span class="required">*</span>
<textarea class="form-control{{ $errors->has('description') ? ' is-invalid' : '' }}" id="description" rows="4" style="height: 150px;" name="description" value="{{ old('description') }}" required autofocus></textarea>
#if ($errors->has('description'))
<span class="invalid-feedback">
<strong>{{ $errors->first('description') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<div class="col-sm-4">
<label for="start_date">Start Date</label><span class="required">*</span>
<input type="date" class="form-control" id="start_date" name="start_date" value="{{ old('start_date') }}" required>
</div>
<div class="col-sm-4">
<label for="submission_date">Start Time</label><span class="required">*</span>
<input type="text" class="form-control" id="start_time" name="start_time" value="{{ old('start_time') }}" required>
</div>
</div>
<div class="form-group row">
<div class="col-sm-4">
<button type="submit" class="btn btn-primary">
{{ __('Submit') }}
</button>
</div>
</div>
</form>
This line:
$clientId = Client::where('user_id', '=', $userId)->pluck('id')->all();
will return a column, not the single ID ("2") that you want.
Then, when you assign it to client_id which is an integer, the ORM will render it as the string [2] instead of the number 2, and that will give you an error.
Either retrieve just the first tuple from Client, or extract $clientId[0] manually (wasteful and not recommended).
You also have a second error I had overlooked:
'created_by', '=', $loggedinId
The above means that created_by must be an integer value, but the text of the error implies SQL expects it to be a datetime. You seem to have a wrong definition in the database.
your time field is in string , it has to be changed , it is not the same format
can you try this instead

Regex match multiple lines before and after word delimited by start and end words

I am wanting to search for {{ upc }} and start the capture not from the <div immediately ahead of the match but the 2nd <div ahead of the match i.e. <div class="form-group"> and capture not up to the first </div> after the match but the 2nd i.e closing </div> or up to the start of the next <div class="form-group"> (depending on how you look at it)
Here is the sample HTML/Twig template I am wanting to search and replace.
<div class="form-group">
<label class="col-sm-2 control-label" for="input-sku"><span data-toggle="tooltip" title="{{ help_sku }}">{{ entry_sku }}</span></label>
<div class="col-sm-10">
<input type="text" name="sku" value="{{ sku }}" placeholder="{{ entry_sku }}" id="input-sku" class="form-control"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-upc"><span data-toggle="tooltip" title="{{ help_upc }}">{{ entry_upc }}</span></label>
<div class="col-sm-10">
<input type="text" name="upc" value="{{ upc }}" placeholder="{{ entry_upc }}" id="input-upc" class="form-control"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-ean"><span data-toggle="tooltip" title="{{ help_ean }}">{{ entry_ean }}</span></label>
<div class="col-sm-10">
<input type="text" name="ean" value="{{ ean }}" placeholder="{{ entry_ean }}" id="input-ean" class="form-control"/>
</div>
</div>
The expected regex match is as follows:
<div class="form-group">
<label class="col-sm-2 control-label" for="input-upc"><span data-toggle="tooltip" title="{{ help_upc }}">{{ entry_upc }}</span></label>
<div class="col-sm-10">
<input type="text" name="upc" value="{{ upc }}" placeholder="{{ entry_upc }}" id="input-upc" class="form-control"/>
</div>
</div>
All help appreciated. Thank you.
One thing you can try is to use a negative lookahead to filter out the things you do not wish to be included in your match. For instance, matching a <div, followed by anything and then another <div, can match things like <div></div><div>.
Instead, what you can say is to match <div, followed by anything - as long as it is not </div> - and then another <div.
<div (?:(?!</div>).)* <div
Then, you can insert that same subpattern anywhere in your expression where you'd normally write .*. In this particular case, you can repeat that to make sure you're not hitting a closing div before the UPC and then continue with the {{ UPC }} portion.
<div(?:(?!</div>).)*<div (?:(?!</div>).)* {{ upc }} .*?</div>\s*</div>
Here is a demo
You need to parse the div's you want and then absorb everything inside them and exclude the rest.
[\w\W] means match words and non-words. It matches newline characters for instance, which * does not.
[\w\W]*(<div[\w\W]*?<div[\w\W]*?{{ sku }}[\w\W]*?<\/div>[\w\W]*?<\/div>)[\w\W]*

Dynamic row is not saved in database with Laravel Controller

I have a form in my view which asks for education qualification of students(I have provided the code below). I want the user (in my case a Student) while filling up the form should be able to dynamically add his/her subjects, total marks and marks obtained in the database table. All thing works fine when i add only one subject. But the problem comes when i try to add multiple subjects. Looking forward for a solution from you people..
This is the form in my view :
<form action="/edu_details" method="post">
#csrf
<div class="jumbotron">
<div class="form-row">
<div class="form-group col-md-12">
<label for="institution">Institution Last Attended</label>
<input type="text" class="form-control" name="institution" id="institution" value="{{old('institution')}}">
#if ($errors->has('institution'))
<div class="error" style="color:red;">{{ $errors->first('institution') }}</div>
#endif
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="yop">Year of Passing</label>
<input type="text" class="form-control" name="yop" id="yop" value="{{old('yop')}}">
#if ($errors->has('yop'))
<div class="error" style="color:red;">{{ $errors->first('yop') }}</div>
#endif
</div>
<div class="form-group col-md-8">
<label for="board">Board</label>
<div class="col-sm-12">
<select name="board" id="board" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>SEBA</option>
<option>CBSE</option>
<option>Other</option>
</select>
</div>
#if ($errors->has('board'))
<div class="error" style="color:red;">{{ $errors->first('board') }}</div>
#endif
</div>
</div>
</div>
<div class="jumbotron">
<table class="table table-bordered">
<thead>
<tr>
<th>Name of the Subject</th>
<th>Total Marks</th>
<th>Marks Obtained</th>
<th>+</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="subj[]" class="form-control">
#if ($errors->has('subj'))
<div class="error" style="color:red;">{{ $errors->first('subj') }}</div>
#endif
</td>
<td>
<input type="text" name="totl" class="form-control">
#if ($errors->has('totl'))
<div class="error" style="color:red;">{{ $errors->first('totl') }}</div>
#endif
</td>
<td>
<input type="text" name="obtn" class="form-control">
#if ($errors->has('obtn'))
<div class="error" style="color:red;">{{ $errors->first('obtn') }}</div>
#endif
</td>
<td>
-
</td>
</tr>
</tbody>
</table>
</div>
<div class="jumbotron">
<div class="form-row">
<label for="sub_group">Group of subjects the applicant wishes to opt :</label>
<div class="form-group col-md-12">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sub_group" id="sub_group" value="Major">
<label class="form-check-label" for="sub_group">Major</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sub_group" id="sub_group" value="General">
<label class="form-check-label" for="sub_group">General</label>
</div>
</div>
#if ($errors->has('sub_group'))
<div class="error" style="color:red;">{{ $errors->first('sub_group') }}</div>
#endif
</div>
<div class="form-row">
<div class="col-sm-4">
<label for="pref1">1st Preference</label>
<select name="pref1" id="pref1" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>
#if ($errors->has('pref1'))
<div class="error" style="color:red;">{{ $errors->first('pref1') }}</div>
#endif
</div>
<div class="form-group col-md-8">
<label for="prefgroup1"> </label>
<div class="col-sm-12">
<select name="prefgroup1" id="prefgroup1" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>
#if ($errors->has('prefgroup1'))
<div class="error" style="color:red;">{{ $errors->first('prefgroup1') }}</div>
#endif
</div>
</div>
</div>
<div class="form-row">
<div class="col-sm-4">
<label for="pref2">2nd Preference</label>
<select name="pref2" id="pref2" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>
#if ($errors->has('pref2'))
<div class="error" style="color:red;">{{ $errors->first('pref2') }}</div>
#endif
</div>
<div class="form-group col-md-8">
<label for="prefgroup2"> </label>
<div class="col-sm-12">
<select name="prefgroup2" id="prefgroup2" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>
</div>
#if ($errors->has('prefgroup2'))
<div class="error" style="color:red;">{{ $errors->first('prefgroup2') }}</div>
#endif
</div>
</div>
</div>
<button type="submit" class="btn btn-primary btn-block">Submit Data</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(".addRow").on('click', function(){
addRow();
return false;
});
function addRow(){
var tr = '<tr>'+
'<td><input type="text" name="subj[]" class="form-control">#if ($errors->has('subj'))
<div class="error" style="color:red;">{{ $errors->first('subj') }}</div>
#endif </td>'+
'<td><input type="text" name="totl" class="form-control"></td>'+
'<td><input type="text" name="obtn" class="form-control"></td>'+
'<td>-</td>'+
'</tr>';
$('tbody').append(tr);
$('tbody').on('click','.delRow',function(){
$(this).parent().parent().remove();
return false;
});
}
</script>
This is my controller's save method :
public function store(Request $request)
{
$validatedData = $request->validate([
'institution' => 'required|max:255',
'yop' => 'required',
'board' => 'required|max:255',
'subj' => 'required|max:255',
'totl' => 'required',
'obtn' => 'required',
'sub_group' => 'required|max:255',
'pref1' => 'required|max:255',
'prefgroup1' => 'required|max:255',
'pref2' => 'required|max:255',
'prefgroup2' => 'required|max:255',
]);
//Insert student data if validated
$edudetail = new EduDetail();
$edudetail->user_id = Auth::user()->id;
//$edudetail->user_id = DB::table('pictures')->where('user_id', $user_id)->value('id');
$edudetail->institution = $request->input('institution');
$edudetail->yop = $request->input('yop');
$edudetail->board = $request->input('board');
foreach($request->get('subj') as $subj) {
$edudetail->subj[] = $subj;
$edudetail->save();
}
$edudetail->totl = $request->input('totl');
$edudetail->obtn = $request->input('obtn');
$edudetail->sub_group = $request->input('sub_group');
$edudetail->pref1 = $request->input('pref1');
$edudetail->prefgroup1 = $request->input('prefgroup1');
$edudetail->pref2 = $request->input('pref2');
$edudetail->prefgroup2 = $request->input('prefgroup2');
$edudetail->save();
return redirect('/student_dox_upload')->with('success','Education Details saved.');
}
I have tried to see if the values are passed as arrays in case of the subject field using print_r method.. And the values are seen in arrays... that's working fine.. but when i go to save only the last record of the dynamic row gets added.. For example if i add 3 subjects English, Maths and Science.. I want all 3 subjects to be added in a column but only Science is getting added.. Any help is appreciated..
Like the bottom, make a fillable for the input values ​​inside the model
$fillable=['institution',...];
Rules in the file in the request on the path
app \ Http \ Request
Define in a file
And then save the data in the form below
public function store(NewServiceRequest $request,Variable $variable,Variable $variable){
$variable->fill($request->only($variable->getFillable()));
$variable->save();
$variable->variable()->save($variable);
.
.
.
}
I did exactly what you said and it worked for the subjects field... But when I tried to implement your solution with the totl field and obtn field i got an error..
I tried this :
$edudetail = new EduDetail();
$edudetail->user_id = Auth::user()->id;
//$edudetail->user_id = DB::table('pictures')->where('user_id', $user_id)->value('id');
$edudetail->institution = $request->input('institution');
$edudetail->yop = $request->input('yop');
$edudetail->board = $request->input('board');
$edudetail->subj = json_encode($request->get('subj'));
$edudetail->totl = json_encode($request->get('totl'));
$edudetail->obtn = json_encode($request->get('obtn'));
$edudetail->sub_group = $request->input('sub_group');
$edudetail->pref1 = $request->input('pref1');
$edudetail->prefgroup1 = $request->input('prefgroup1');
$edudetail->pref2 = $request->input('pref2');
$edudetail->prefgroup2 = $request->input('prefgroup2');
$edudetail->save();
I got this error :
Illuminate \ Database \ QueryException (22007)
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["100","100","100","100"]' for column cas_db.edu_details.totl at row 1 (SQL: insert into edu_details (user_id, institution, yop, board, subj, totl, obtn, sub_group, pref1, prefgroup1, pref2, prefgroup2, updated_at, created_at) values (1, Little Flower School, 2004, CBSE, ["Science","Maths","Computer","English"], ["100","100","100","100"], ["72","76","80","78"], General, Sub2, Sub1, Sub3, Sub2, 2019-07-28 11:21:24, 2019-07-28 11:21:24))

How to pass form fields into 'templated' HTML file

I would like to get form data from a pre-built HTML template using a Django form without changing the display.
I know I could use something like:
name = request.POST.get('first_name')
But would like to handle the data using a ModelForm
forms.py
class BioDataForm(forms.ModelForm):
first_name = forms.CharField(max_length=100, label='first_name')
last_name =forms.CharField(max_length=100, label='last_name')
mobile = forms.CharField(max_length=100, label='mobile_number')
email = forms.EmailField(max_length=100, label='personal_email')
gender = forms.CharField(max_length=100, widget=forms.Select(attrs={'class': 'form-control', 'id': 'gender'}))
marital_status = forms.CharField(max_length=100, widget=forms.Select(attrs={'class': 'form-control', 'id': 'marital_status'}))
date_of_birth = forms.DateField(input_formats=['%Y-%m-%d'], widget=forms.SelectDateWidget())
address = forms.CharField(max_length=100)
class Meta:
model = BioData
exclude = ('state',)
fields = ['first_name', 'last_name', 'mobile', 'email',
'gender', 'marital_status', 'date_of_birth', 'address']
Sample HTML
<div class="row form-group">
<div class="col-md-6 col-sm-12 margin-bottom-20-m">
<label for="first_name" class="form-control-label kt-black-text">First Name</label>
<input type="text" class="form-control" id="first_name" name="e_first_name" required>
</div>
<div class="col-md-6 col-sm-12">
<label for="last_name" class="form-control-label kt-black-text">Last Name</label>
<input type="text" class="form-control" id="last_name" name="e_last_name" required>
</div>
</div>
iterate form in template :
<div class="row form-group">
{% for field in form %}
<div class="col-md-6 col-sm-12 margin-bottom-20-m">
<label for="{{ field.id_for_label }}" class="form-control-label kt-black-text">{{ field.label }}</label>
{{ field }}
</div>
{% endfor %}
</div>

Add value to select tag - HTML

I'm working on a Laravel application. In my view i have a form, what I'm trying to do is when submitting the form and one field is not valid, it returns the values of valid fields, so the user will have to edit the wrong field only, not the full form.
This is my form and an example. In here, the user didn't enter the url, so it shows an error message and return the values of the correct fields (client, domain provider, etc...) The problem i'm facing is this is not working for the avatar field and service field!
I'm returning the value by adding value="{{ old('client') }}" to the input tag , as shown in code below:
<div class="form-group row">
<label class="col-lg-3 col-form-label">Client:</label>
<div class="col-lg-9">
<input type="text" class="form-control" name="client" value="{{ old('client') }}">
<small class="error">{{$errors->first('client')}}</small>
</div>
</div>
QUESTION: How to add it for avatar and select service fields?
Avatar: - currently its not returning the value
<div class="form-group row">
<label class="col-lg-3 col-form-label">Attach avatar:</label>
<div class="col-lg-9">
<input type="file" class="form-input-styled" data-fouc="" name="avatar" value="{{ old('avatar') }}">
<small class="error">{{$errors->first('avatar')}}</small>
</div>
</div>
Select service:
<div class="form-group row">
<label class="col-lg-3 col-form-label">Select Service:</label>
<div class="col-lg-9">
<select data-placeholder="Select service" class="form-control form-control-select2" data-fouc="" name="service" >
<option></option>
<option value="Website">Website</option>
<option value="Application">Application</option>
</select>
<small class="error">{{$errors->first('service')}}</small>
</div>
</div>
For the select tag ,
you must put a condition for every option tag ,
if the last submitted value is equal to this option value put selected
attribute to this option
something like that ....
<div class="form-group row">
<label class="col-lg-3 col-form-label">Select Service:</label>
<div class="col-lg-9">
<select data-placeholder="Select service" class="form-control form-control-select2" data-fouc="" name="service" >
<option></option>
<option value="Website" {{ old('service') == "Website" ? "selected" : ""}}>Website</option>
<option value="Application" {{ old('service') == "Application" ? "selected" : "">Application</option>
</select>
<small class="error">{{$errors->first('service')}}</small>
</div>
</div>
hi for select tag this work for me
<div class="form-group row">
<label class="col-lg-3 col-form-label">Select Service:</label>
<div class="col-lg-9">
<select data-placeholder="Select service" class="form-control form-control-select2" data-fouc="" name="service" >
<option></option>
#foreach($Listservice as $key)
<option value ="{{$key->id}}"> {{$key->Website}}
{{$key>Application}}</option>
#endforeach
</select>
</div>
</div>
and in your model add these
$listservice = Service::get();
i hope it s help