Why it shows an error in the store() conferece? - laravel

I have this code to store a conference. There is a form field that is for the user to select the available payment methods. The form field is like this:
<div class="form-group">
<label for="inputName">
Select the payment methods</label>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="payment_mb" id="payment_mb">
<label class="form-check-label" for="exampleRadios1">
MB
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="payment_c" id="payment_c">
<label class="form-check-label" for="exampleRadios1">
Credit Card
</label>
</div>
</div>
The code to store is like below.
But it shows an error:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value:
'on' for column 'payment_mb' at row 1 (SQL:
insert into `conferences` (`name`, `organizer_id`,
`organizer_name`, `organizer_email`, `invoice_entity`,
`payment_mb`, `updated_at`, `created_at`)
values (a, 2, John W, emailtest#test.com, 1,
on, 2018-07-25 19:17:26, 2018-07-25 19:17:26))
Do you know why?
public function store(Request $request)
{
$conference = Conference::create([
'name' => $request->name,
'city' => $request->city,
'organizer_id' => Auth::id(),
'organizer_name' => $request->organizer_name,
'organizer_email' => $request->organizer_email,
'payment_mb' =>$request->payment_mb ? : 0,
'payment_c' => $request->payment_c ? : 0
]);
$conference->categories()->attach($request->categories);
$conference->save();
Session::flash('success', 'conference created.');
}
The $request->all() shows like:
array:25 [▼
"_token" => ""
"name" => "test"
"organizer_name" => "John W"
"organizer_email" => "..."
"payment_mb" => "on"
"payment_c" => "on"
"invoice_issuer" => "1"
]

Well on wouldn't be an int value. Your request has the value 'on' for that field.
'payment_mb' =>$request->payment_mb ? : 0,
You could maybe try:
'payment_mb' => (int) boolval($request->payment_mb),
Or even just:
'payment_mb' => (bool) $request->payment_mb,
PHP Docs - boolval
You could also actually set a value for the checkbox to be a '1' or something like that.
Some validation might be good.
Update
I think i was looking for filter_var:
filter_var($thatInput, FILTER_VALIDATE_BOOLEAN);
// 'on' => true
// null => false
// 'off' => false

Are you sure this is the correct store code?
payment_c does not appear in the sql statement even though it was set in your code.

Related

Save arrangement to Livewire

I need to save an array generated when I select several input type checkboxes, and I don't know how to do it Laravel Livewire. So, in this section, I select the checkboxes and send them by wire "documentos."
<input wire:model.defer="documentos" name="documentos" type="checkbox" value="Acta"
class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded">
<label for="acta" class="form-check-label">Acta</label>
<br></br>
<input wire:model.defer="documentos" name="documentos" type="checkbox" value="Documento Ideas"
class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded">
<label for="ideas" class="form-check-label">Documento Ideas</label>
<input wire:model.defer="documentos" name="documentos" type="checkbox" value="Plan Inversion"
class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded">
<label for="plan" class="form-check-label">Plan Inversion</label>
</div>
I have the save function in this section, but it only saves one value.
public function guardar()
{
$data = [
['id' => $this->use_id],
[
'tarea_paso' => $this->tarea_paso,
'descripcion_paso' => $this->descripcion_paso,
'fecha_inicio' => $this->fecha_inicio,
'fecha_fin' => $this->fecha_fin,
'id_responsable' => $this->id_responsable,
'documentos' => $this->documentos,
'acta' => $this->acta,
'ideas' => $this->ideas,
'plan' => $this->plan,
'acciones' => $this->acciones,
'poais_id' => $this->metodologia_id,
]
];
}
try this
$data = [
'tarea_paso' => $this->tarea_paso,
'descripcion_paso' => $this->descripcion_paso,
'fecha_inicio' => $this->fecha_inicio,
'fecha_fin' => $this->fecha_fin,
'id_responsable' => $this->id_responsable,
'documentos' => $this->documentos,
'acta' => $this->acta,
'ideas' => $this->ideas,
'plan' => $this->plan,
'acciones' => $this->acciones,
'poais_id' => $this->metodologia_id,
];
Model::where('id', $this->use_id)->update($data);

User Model return null for the user name

I'm trying to display the users name using blade, but Its says null.
This is the controller
public function edit(Project $project)
{
//
$project = Project::findOrFail($project->id);
$assignees = User::with('roles')->get();
return view('projects.edit',['project'=>$project,'assignees'=>$assignees]);
}
When I dd($assignees) all the columns appears including the name column.
#attributes: array:10 [▼
"id" => 4
"name" => "Kitten Moose"
"email" => "kitten#karata.com"
"email_verified_at" => null
"password" => "$2y$10$iG4EZBq13ExGOVIrGNhEQeVvordWc3ibWfYgdaq6x0wqefetqXixe"
"remember_token" => null
"created_at" => "2020-07-03 05:11:56"
"updated_at" => "2020-07-11 09:37:49"
"team_id" => 3
"deleted_at" => null
]
On blade:
<div class="form-group">
<label for="exampleFormControlSelect1">Assignee</label>
<select name="assignee_id" class="js-example-basic-single">
<option value=" ">Select</option>
#foreach($assignees as $assignee)
<option value="{{$assignee->id}}">{{$assignee->name}}</option>
#endforeach
</select>
</div>
Using the blade above, I get {"id":1,"name":null} everything else displays correctly
I'm not sure if this could be caused by the model, but I have this in the user model:
public function assignee()
{
return $this->belongsTo(User::class);
}

How to validate an ID which should be in given array - Laravel

I have an array which is
array (
0 => 1,
1 => 7,
2 => 11,
3 => 8,
4 => 5,
)
Now I want to validate a field status which should be in the given list.
$validation = Validator::make($req->all(),[
'status.*.id' => 'required'
]);
How can I validate the status? Can anyone suggest any solution?
Thank You.
Laravel has an option to validate a value compare to a given array of data:
//At the top of your class add the correct Rule namespace.
use Illuminate\Validation\Rule;
$yourarray = [
0 => 1,
1 => 7,
2 => 11,
3 => 8,
4 => 5,
];
$validation = Validator::make($req->all(),[
'status.*.id' => ['required',Rule::in($yourarray)]
]);
For further details follow the official documentation here
<p>
<input type="text" name="person[1][id]">
<input type="text" name="person[1][name]">
</p>
<p>
<input type="text" name="person[2][id]">
<input type="text" name="person[2][name]">
</p>
Now you can validate the requested data as below:
$v = Validator::make($request->all(), [
'person.*.id' => 'exists:users.id',
'person.*.name' => 'required:string',
]);

Textarea with Laravel Collective

Hello friends I need make a Textarea with Laravel Collective, regulary I use:
<textarea id="txt" name="txt" maxlength="1900" class="form-control" rows=1" onkeypress="return nameFunction(event);">My text</textarea>
How to create a textarea wiht Laravel Collective?
Try like this:
Form::textarea('My text', null, [
'class' => 'form-control',
'rows' => 1,
'name' => 'txt',
'id' => 'txt',
'onkeypress' => "return nameFunction(event);"
])

Laravel checkbox validation, always empty?

I have a checkboxes like this:
<div class="form-group">
<div style="display:none ;" class="weekday_message form-control alert-warning"></div>
<label id="weekday2" for="weekday" class="col-md-4 control-label">Weekday</label>
<div class="required form-field" name="weekday" id="weekday">
<input class="weekday" type="checkbox" name="weekdays[]" value="MO">Monday
<input class="weekday" type="checkbox" name="weekdays[]" value="TU">Tuesday
<input class="weekday" type="checkbox" name="weekdays[]" value="WE">Wednesday
<input class="weekday" type="checkbox" name="weekdays[]" value="TH">Thursday
<input class="weekday" type="checkbox" name="weekdays[]" value="FR">Friday
<input class="weekday" type="checkbox" name="weekdays[]" value="SA">Saturday
<input class="weekday" type="checkbox" name="weekdays[]" value="SU">Sunday
</div>
<span class="help-block">
<strong></strong>
</span>
</div>
My validation:
public function rules()
{
return [
'startdate' => 'required|date',
'endate' => 'nullable|date',
'startime' => ['required', new Time],
'endtime' => ['required', new Time],
'title' => 'required',
'entity_id' => 'required',
'type' => 'required|exists:entities,type',
'description' => 'required',
'frequency' => 'required',
'interval' => 'nullable|numeric',
'monthday' => 'nullable|numeric|min:1|max:3',
'weekdays' => 'array|max:3',
'month' => 'nullable|numeric',
'until' => 'nullable|date',
'tags' => 'nullable',
];
}
and controller:
public function storeEvent(EventRequest $request)
{
$test = ($request->input('weekdays'));
dd($test);
$weekday_string = implode(",", $request->input('weekdays'));
$request->merge(array('weekday', $weekday_string));
dd($request->all());
$event = DirtyEvent::create($request->all());
$geoloc_id = Entity::find($event->entity_id)
->first();
$user_id = Auth::id();
// Save Geoloc + user id into newly created event
$event->_geoloc()->associate($geoloc_id);
$event->users()->associate($user_id);
$event->save();
Now, validation seems to pass because it does data dump, however both dd($test) as well as $request->all() are giving me back empty weekdays, like it would not be defined. What could be the possible cause of this?
If you want to make sure you have always at least one weekday selected you should change:
'weekdays' => 'array|max:3',
into:
'weekdays' => 'array|required|max:3',
Also I suppose you don't send data using standard HTML form because you set for example name for divs so maybe you forget to attach weekdays or have bug in code elsewhere?
Your HTML says weekday (singular) but your rules set says weekdays (plural).
There needs to be at least one checkbox selected to make the input weekdays to be included in the request. You can use a default value in case none was selected by adding a hidden input before the checkboxes.
<input type="hidden" name="weekdays" value="defaultValue">

Resources