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);
Related
I am trying to upload multiple images using guzzlehttp client. I load images as a file and loads only one image with the code below.
$clientPAimages = new Client();
$imagesPA = Req::file('images');
foreach($imagesPA as $image){
$bodyImages[] = [
'name' => 'image',
'contents' => fopen($image->getRealPath(), 'r'),
'filename' => $image->getClientOriginalName(),
'headers' => [
'Content-Type' => '<Content-type header>'
]
];
}
$responsePA3 = $clientPAimages->request('POST', 'link/images', [
'multipart' => $bodyImages
]);
$responsePA3->getBody();
Does anyone have any idea how to solve this i.e. how to save multiple images?
I was in this kind of situation, when i needed to send multiple images from the laravel app to the node.js app. Your code seems ok, but may you've something wrong in your blade, for example usage of enctype="multipart/form-data" in the form, or name="images[]" as an input property, or something like that.
Anyway i'll share here the snippet, which worked well for me before, so i guess it can be useful.
Specifications for this snippet was:
"laravel/framework": "^8.12",
"guzzlehttp/guzzle": "^7.0.1",
create.blade.php
<form action="http://internal.site/upload" method="post" enctype="multipart/form-data">
#csrf
<input id="ss_images"
name="images[]"
type="file"
multiple="multiple"
class="input"
accept=".jpeg,.bmp,.png,.jpg,.gif"
/>
<div class="mt-3">
<label class="{{ $errors->has('name') ? 'has-error' : '' }}">
{{ $errors->has('name') ? $errors->first('name') : "Name" }}
</label>
<input value="{{ old('name') }}" type="text" class="input" placeholder="Name" name="name">
</div>
<div class="mt-3">
<label class="{{ $errors->has('description') ? 'has-error' : '' }}">
{{ $errors->has('description') ? $errors->first('description') : "Description" }}
</label>
<textarea class="input" placeholder="Description" name="description">{{ old('description') }}</textarea>
</div>
</form>
ItemController.php
use GuzzleHttp\Client;
// ...
public function upload(Request $request)
{
$data = $request->only([
'name',
'description',
]);
$multipart = [];
if($request->hasFile('images')) {
foreach ($request->file('images') as $k => $image) {
$multipart[] = [
'name' => 'file',
'contents' => fopen($image->getRealPath(), 'r'),
// ... some additional fields
];
}
}
// adding some text-oriented data if need
$multipart[] = [
'name' => 'data',
'contents' => json_encode($data, true),
];
$client = new Client();
$url = "http://external.site/link/images";
$response = $client->request('POST', $url, [
'multipart' => $multipart
]);
$res_json = $response->getBody()->getContents();
$res = json_decode($res_json, true);
return redirect()->back()->with([
'success' => $res['success'],
'flash_message' => $res['message'],
]);
}
Enjoy!!
I've a problem with livewire 2.0 on Laravel 8.
I have created a registration page using a livewire component, and in it, I have implemented validation.
Once I click on the Register button, the action is not saved in the table.
I included a debug log, and in the debug itself if I see the correct action, and if I do it in tinker it is also correct.
I am getting desperate because I am not able to locate it, and the tests pass well.
This is the point in my gitlab where I have left the topic desperate.
protected $rules = [
'email' => 'required|email|unique:users',
'name' => 'required|min:6',
'password' => 'required|min:6|same:passwordConfirmation'
];
public function register()
{
$this->validate();
$user = User::create([
'name' => $this->name,
'email' => $this->email,
'password' => Hash::make($this->password)
]);
Log::debug($user);
auth()->login($user);
return redirect('/');
}
Laravel.log
Debug write value of returned User::create
[2021-03-16 10:08:29] testing.DEBUG: {"name":"Foo","email":"foo#foo.com","updated_at":"2021-03-16T10:08:29.000000Z","created_at":"2021-03-16T10:08:29.000000Z","id":1}
On tinker
Work fine after verify database
$user = User::create([
'name' => 'Foo',
'email' => 'foo#email.com',
... 'password' => '123456']);
[!] Aliasing 'User' to 'App\Models\User' for this Tinker session.
=> App\Models\User {#4365
name: "Foo",
email: "foo#email.com",
#password: "123456",
updated_at: "2021-03-16 10:10:19",
created_at: "2021-03-16 10:10:19",
id: 4,
}
Oh. my god. Despite in blade.
I forget create instance of name for verify.
Component of livewire, validates what is there, but does not validate if something is missing, and continues. It is something strange because the field that was missing name is no required really on database (nullable), but the create method does not return an error if value is empty, but also not save data in table.
Missing
<div>
<label for="name" class="block text-sm font-medium text-gray-700">
Name
</label>
<div class="mt-1 rounded-md shadow-md">
<input wire:model="name" id="name" name="name" type="text" autocomplete="text"
class="#error('name') border-red-500 #enderror appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
</div>
#error('name') <div class="mt-1 text-red-500 text-sm">{{ $message }}</div> #enderror
</div>
Now work fine.
I can't understand what the problem may be:
I state that the code without the part of the attachment works
function submit(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'file' => 'mimes:pdf,doc,docx'
]);
$data = array(
'name_data' => $request->name,
'cognome_data' => $request->cognome,
'luogo_data' => $request->luogo,
'date_data' => $request->date,
'telefono_data' => $request->telefono,
'email_data' => $request->email,
'citta_data' => $request->citta,
'provincia_data' => $request->provincia,
'studio_data' => $request->studio,
'lingua_data' => $request->lingua,
'livello_data' => $request->livello,
'lingua2_data' => $request->lingua2,
'livello2_data' => $request->livello2,
'file_data' => $request->file,
'agree_data' => $request->agree
);
Mail::send('mail', $data, function($message) use ($request,$data){
$message->to('pipo#gmail.com', 'piooi')->subject('Send mail ' . $request->name);
$message->from($request->email, $request->name);
if ( isset($data['file_data']))
{
$message->attach($data['file_data']->getRealPath(), array(
'as' => $data['file_data']->getClientOriginalName(),
'mime' => $data['file_data']->getMimeType()));
}
});
Session::flash('success', 'Mail spedita con sucesso');
}
}
I put the piece of the form in question:
<form class="text-left form-email"action="#" enctype="multipart/form-data" method="POST">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Curriculum Vitae:</span>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="file" name="file"
aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" for="inputGroupFile01">Seleziona il file</label>
</div>
</div>
the error that gives the summit is the following:
local.ERROR: Call to a member function getRealPath() on null {"exception":"[object] (Error(code: 0):
Change your mail part to this and see if it works
Mail::send('mail', $data, function($message) use ($request,$data){
$message->to('pipo#gmail.com', 'piooi')->subject('Send mail ' . $request->name);
$message->from($request->email, $request->name);
if($request->hasFile('file')){
$message->attach($request->file->getRealPath(), array(
'as' => $request->file->getClientOriginalName(),
'mime' => $request->file->getMimeType())
);
}
});
How will I validate the current entries from here , this is the json response inside the Chrome Devtools after submitting the form thru axios ?
And this is in my UpdateProfileRequest.php
public function rules()
{
return [
'username' => 'required|unique:users|min:3',
'password' => 'required|min:8|confirmed',
'confirm_password' => 'required|',
'current_password' => 'required|u',
'country_id' => 'required|integer',
'display_name' => 'required|min:5',
'email' => 'required|unique:users,email_address',
'phone_number' => 'required|alpha_num',
'image' => 'mimes:jpg,png,gif'
];
This is the whole response from the request
This is inside my UpdateProfile.vue
<div>
<div v-if="user != null">
<form class="bg-white m-auto h-full p-4 w-full" id="setup-billing-form" #submit.prevent="submitProfile()" method="POST">
<div class="flex inline-block">
<div id="input-group" class="w-3/5">
<label for="name" class="block uppercase tracking-wide text-black-v2 text-xs font-bold mb-2">Username
</label>
<input v-model="form.username" type="text" class="hover:bg-grey-lightest bg-grey-lighter w-full mb-2 p-2 leading-normal" id="pin" name="pin" autocomplete="name" placeholder="Your Username" required>
</div>
<div id="input-group" class="ml-2 w-3/5">
<label for="name" class="block uppercase tracking-wide text-black-v2 text-xs font-bold mb-2">Email
</label>
this is is inside my axios request
submitProfile(){
let data = new FormData();
axios.put(this.endpoint, {
form : this.form ,
image : this.image
}).then(response => {
console.log(response.data);
}).catch(error => {
console.log(error);
});
},
Now, I want to ask if how do I validate those requests inside my UpdateProfileRequest, should I add the form. to each of those requests ?
Two Methods:
1. just use form.username to validate
$rule = [
'form.username' => 'required|unique:users|min:3',
'form.password' => 'required|min:8|confirmed',
'form.confirm_password' => 'required|',
'form.current_password' => 'required|u',
'form.country_id' => 'required|integer',
'form.display_name' => 'required|min:5',
'form.email' => 'required|unique:users,email_address',
'form.phone_number' => 'required|alpha_num',
'image' => 'mimes:jpg,png,gif'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
}
2. Flatten the object to request:
It seems that you new a formdata without using it.
And pass the data with keys form and image.
You need to flatten your object before put it:
submitProfile(){
// if you use formdata
// let form = new FormData(this.form);
// let form = form.append('image', this.image);
let form = Object.assign({}, this.form); // clone this.form
form['image'] = this.image;
axios.put(this.endpoint, form).then(response => {
console.log(response.data);
}).catch(error => {
console.log(error);
});
},
This request that you can directly validate by
'username', 'password', ... without prefix form.
and if you want to insert the datas, you can just use $request->except('image').
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">