Laravel date before_equal is allowing all dates through - laravel

$DOB = $request->input('DOB');
$allowed = Carbon::now()->subYears(26)->format('Y-m-d');
$validatedData = $request->validate(
['DOB' => 'required|date|before_or_equal:'.$allowed],
);
<input type="date" name="DOB" class="form-control" required>
I am having a hard time validating dates in Laravel.
If I enter 01/01/2018 in the form, the validation does not catch it. Below it what is returned if I dd() the variables.
dd([$DOB, $allowed]);
array:2 [▼
0 => "2018-01-01"
1 => "1992-04-26"
]
No idea where I am going wrong.

Related

Array to string conversion error when multiple file upload in Laravel 5.8.38

I have an Array to string conversion error, when trying to realize multiple upload files function in Laravel 5.8.38
Cant find any decision about it
In blade form I have simple thing:
<form class="form-horizontal" action="{{route('admin.estates.store')}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<label for="estate_image" class="mt-4">Images</label>
<input type="file" name="estate_image[]" multiple>
<input class="btn btn-primary" type="submit" value="Сохранить">
<input type="hidden" name="created_by" value="{{Auth::id()}}">
</form>
In store function I have:
The function creates an estate(one property). If user adds some images for it, we adds this images in local path and adds them in database
if I comment $estate = Estate::create($request->all()); it works fine
but in this case estate doesnt adds in database
public function store(Request $request)
{
$estate = Estate::create($request->all());
if($request->hasFile('estate_image')) {
foreach ($request->file('estate_image') as $image) {
// do some image resize and store it on local path
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images\\' . $filename);
Image::make($image)->resize(800, 400)->save($location);
// add image info in database
$estateimage = new EstateImages();
$estateimage->image_path = $location;
$estateimage->image_alt = 'testalt';
$estateimage->save();
}
}
}
Am array I have from input
array:5 [▼
"name" => array:2 [▼
0 => "image1.jpg"
1 => "image2.jpg"
]
"type" => array:2 [▼
0 => "image/jpeg"
1 => "image/jpeg"
]
"tmp_name" => array:2 [▼
0 => "C:\OSPanel\userdata\php_upload\phpCB51.tmp"
1 => "C:\OSPanel\userdata\php_upload\phpCB52.tmp"
]
"error" => array:2 [▼
0 => 0
1 => 0
]
"size" => array:2 [▼
0 => 164808
1 => 58217
]
]
As understand, foreach doesnt start, but dont understand why (tried to delete all code in foreach, and leave there just simple echo 'Hello!'; , have the same error.
Saw the same problems in StackOverflow, but any of it helped me...
The problem was in first line
$estate = Estate::create($request->all());
So, from blade I receive a name="estate_image[] data which is an array, and Laravel tried to adds an array in database cell. In database cell cold be added just string value and through that, I had that error.
Solve it, by deleting this column from database and deleting this from $fillable variable in main model.
Quite stupid and easy thing from me, but hope, this answer helps someone. :-)

Laravel: How to save checkbox value

I get this error, when I submit the form
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'onloan' cannot be null
Please help. Sorry for the noob question. I've been searching but the examples are different.
edit.blade.php
<div class="form-group">
<input type="checkbox" class="" id="onloan" value="1" {{ $item->onloan == 1 ? 'checked' : '' }}>
<label for="onloan">On Loan</label>
</div>
controller
public function update(Request $request, Item $item)
{
$item->update([
'name' => $request->name,
'description' => $request->description,
'barcode' => $request->barcode,
'onloan' => $request->onloan //Not Working
]);
}
add name="onloan" on your input field, otherwise it won't pass data with form submit method.
<input type="checkbox" class="" name="onloan" id="onloan" value="1" {{ $item->onloan == 1 ? 'checked' : '' }}>
If you uncheck the checkbox, then it will send nothing with submit, so from your controller you can give a value (0) if value is not passed :
'onloan' => ($request->onloan) ? '1' : '0';
Or,
if(isset($request->onloan)) {
$onloan = "1";
} else {
$onloan = "0";
}
'onloan' => $onloan,

How to store saved checkbox value in Laravel?

I'm studying Laravel and made contact form.
my Laravel Framework is 7.15.0
I wrote this code to saving form values useing old function.
<input class="form-check-input" type="radio" name="q1[]"
value="A" #if(is_array(old('q1')) && in_array("A", old('q1'))) checked #endif>
<label class="form-check-label" >A</label>
<input class="form-check-input" type="radio" name="q1[]"
value="B" #if(is_array(old('q1')) && in_array("B", old('q1'))) checked #endif>
<label class="form-check-label" >B</label>
However after I wrote this I couldn't store data to my mysql.
Here is error message.
ErrorException
Array to string conversion
vendor/laravel/framework/src/Illuminate/Support/Str.php:449
Seems like saving array data to my mysql is problem I guess.
Here is my controller code.
Could someone teach me how to write store data as not array please?
public function ContactUsForm(Request $request) {
// Form validation
$this->validate($request, [
'name' => 'required',
]);
Contact::create($request->all());
\Mail::send('mail', array(
'name' => $request->get('name'),
'q1_s' => $request->get('q1')
), function($message) use ($request){
$message->from($request->email);
$message->to('mail#myemailadress.com', 'you got mail ')->subject($request->get('you got mail'));
});

Input array inside json by ajax not working properly in laravel

I'm using Semantic UI and Laravel 6
I got this html code:
<div class="field">
<label for="realTime">Tiempo real</label>
<input type="text" id="realTime" name="schedules[{{ $service->id_service }}][realTime]" required/>
</div>
<div class="field">
<label for="delayTime">Tiempo de demora</label>
<input type="text" id="delayTime" name="schedules[{{ $service->id_service }}][delayTime]" required/>
</div>
<div class="field">
<label for="deathTime">Tiempo muerto</label>
<input type="text" id="deathTime" name="schedules[{{ $service->id_service }}][deathTime]" required/>
</div>
And I'm sending this info by ajax using this code:
$.ajax({
url: "...",
data: {
"formData": $(formClass).form('get values')
},
success: function (response) {
// skipped code
}
});
But when I dump the request data in controller using:
dd($request->input('formData'));
I got this result:
array:1 [
"formData" => array:1 [
"schedules[1" => array:3 [
"realTime" => "12:00:00"
"delayTime" => "13:00:00"
"deathTime" => "14:00:00"
]
]
]
Instead of:
array:1 [
"formData" => array:1 [
"schedules" => array:1 [
1 => array:3 [
"realTime" => "12:00:00"
"delayTime" => "13:00:00"
"deathTime" => "14:00:00"
]
]
]
]
I checked out the content sent shown in Headers tab (using Chrome) and I found this:
formData[schedules[1][realTime]]: 12:00:00
formData[schedules[1][delayTime]]: 13:00:00
formData[schedules[1][deathTime]]: 14:00:00
I also realized that if I put the name as "schedules][{{ $service->id_service }}][realTime]" (putting an extra closing bracket after "schedules"), the dump shows the array correctly.
Any solution?

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'score' cannot be null

Controller
public function getScore(Request $request, $id)
{
// $scores = Criteria::find($id);
$contestants = Contestant::find($id);
foreach ($request->criteria as $id => $criteria){
$criteriaModel = Score::find($id);
$scores = new Score();
$scores->judge_name = $request->input('judge_name');
$scores->contestant = $contestants->name;
$scores->criteria = $criteriaModel->criteria;
$scores->score = $scores->score;
$scores->save();
}
return redirect('/tabulation')->with('status', 'Score saved!');
}
Blade
#foreach ($criterias as $criteria)
<div class="form-group col-md-6">
<label for="{{$criteria->name}}">{{$criteria->name}} </br> (0 - {{$criteria->points}})</label>
<input type="text" name="criteria[{{$criteria->id}}][criteria]" value="{{$criteria->name}}" hidden>
<input type="text" name="score[{{$criteria->id}}][score]" class="form-control" placeholder="Input score" required>
</div>
#endforeach
Form field names can contain brackets to store multiple properties for a single name:
#foreach ($criterias as $criteria)
<div class="form-group col-md-6">
<label for="{{$criteria->name}}">{{$criteria->name}} </br> (0 - {{$criteria->points}})</label>
<input type="text" name="criterias[{{$criteria->id}}][name]" value="{{$criteria->name}}" hidden>
<input type="text" name="criterias[{{$criteria->id}}][points]" class="form-control" placeholder="Input score" max="{{$criteria->points}}" name="score" required>
</div>
#endforeach
The above form would result the $request->criterias variable containing the following value:
array:2 [▼
1 => array:2 [▼
"name" => "test"
"points" => "dd"
]
2 => array:2 [▼
"name" => "tes22t"
"points" => "sdsd"
]
]
This value can be used in the controller for creating multiple scores:
foreach ($request->criterias as $id => $criteria){
$criteriaModel = Criteria::find($id);
$scores = new Score();
$scores->judge_name = $request->input('judge_name');
$scores->contestant = $contestants->name;
$scores->criteria = $criteriaModel->name;
$scores->score = $criteria->points;
$scores->save();
}
First of all you have to change the name of your input to be an array like so:
<input type="text" name="criteria[]" value="{{$criterias->name}}" hidden>
and in your controller you have to loop through the inputs:
foreach ($request->input('criteria') as $criteria){
$scores = new Score();
$scores->judge_name = $request->input('judge_name');
$scores->contestant = $contestants->name;
$scores->criteria = $request->input('criteria');
$scores->score = $request->input('score');
$scores->save();
}

Resources