I have create a validation request file on request folder. It working fine in new insert. But when i update, it's not working also i pass the unique $id but not working.
a. Resource controller update method
public function update(KlassNameRequest $request, $id)
{
$validated = $request->validated();
KlassName::where('id', $id)->update($validated);
}
b. Validation code
public function rules()
{
return [
'name' => 'required|unique:klass_names|max:128,' . $this->id,
'ref' => 'required|unique:klass_names|numeric|between:1,999,' . $this->id,
'seat_quota' => 'required|numeric|between:1,9999',
'division' => 'required',
'semester' => 'required',
];
}
This message shows me when i update
return [
'name' => 'required|unique:klass_names,' . $this->id.'|max:128',
'ref' => 'required|unique:klass_names,' . $this->id.'|numeric|between:1,999',
'seat_quota' => 'required|numeric|between:1,9999',
'division' => 'required',
'semester' => 'required',
];
Just try this one
'name' => ['required','max:128',Rule::unique('klass_names')->where(function ($query) {
return $query->where('name', $this->name);
})],
'ref' => ['required','max:128',Rule::unique('klass_names')->where(function ($query) {
return $query->where('ref', $this-> ref);
})],
Hope it will solve the problem 😊
I have solve my problem in this way -
a. Add extra input hidden field passing id for $request method. Because my route is resource group route -
<form action="{{ route('adm.kls.update', $kls->id) }}" method="post">
#csrf
#method('PUT')
<input type="hidden" name="id" value="{{ $kls->id }}">
</form>
b. Some editing in validation code.
public function rules() {
return [
'name' => 'required|max:128|unique:klass_names,name,' . $this->id,
'ref' => 'required|numeric|between:1,999|unique:klass_names,ref,' . $this->id,
'seat_quota' => 'required|numeric|between:1,9999',
];
}
done.
Related
I'm having problem that I would like to send Email with multiple images in Laravel9.
I wrote below code.
My goal is send Email with multiple images and
sametime each image file name stores into mysql's 'fileattach' column as photo1, photo2, photo3(one line)
Could someone correct my code please?
blade file
<div class="col-sm-6">
<input type="file" name="files[]" accept="file_extension|image/*|media_type" multiple>
</div>
Controller
public function saveContact(Request $request) {
$this->validate($request, [
'name' => 'required',
]);
if(count($files) > 0) {
foreach($files as $file) {
$message->attach($file->getRealPath(), array(
'as' => $file->getClientOriginalName(),custom name
'mime' => $file->getMimeType())
);
}
}
$contact = new Contact($request->all());
$contact->save();
\Mail::send('admin_email_tpl', //admin tpl
array(
'sno' => $request->get('sno'),
'name' => $request->get('name'),
'email' => $request->get('email'),
'files' => $request->post('files'),
), function($message) use ($request)
{
$message->from('123#123foobar.com');
$message->to('123#123foobar.com');
$message->subject('Thank You');
});
You are almost there, need to put the attachment code into callback method to mail.
public function saveContact(Request $request) {
$this->validate($request, [
'name' => 'required',
]);
//Get the $files array here
$files = [];
$contact = new Contact($request->all());
$contact->save();
\Mail::send('admin_email_tpl', //admin tpl
array(
'sno' => $request->get('sno'),
'name' => $request->get('name'),
'email' => $request->get('email'),
'files' => $request->post('files'),
), function($message) use ($request, $files)
{
$message->from('123#123foobar.com');
$message->to('123#123foobar.com');
$message->subject('Thank You');
if(count($files) > 0) {
foreach($files as $file) {
$message->attach($file->getRealPath(), array(
'as' => $file->getClientOriginalName(),//custom name
'mime' => $file->getMimeType())
);
}
}
});
}
how to save multiple selcet in laravel when I try the script below, not to save
this store in controller
public function store(Request $request)
{
$id = $request->id;
$post = Rek_medik::updateOrCreate(['id' => $id],
[
'kode_rekmed' => $request->kode_rekmed,
'kode_register' => $request->kode_register,
'kode_pasien' => $request->kode_pasien,
'nama_pemeriksa' => $request->nama_pemeriksa,
'tgl_rekmed' => Carbon::now(),
'anamnesis' => $request->anamnesis,
'pemeriksaan' => $request->pemeriksaan,
'resep' => $request->resep,
]);
return response()->json($post);
}
this is code in view
<div class="form-group">
<label for="resep">Resep</label>
<select class="select2bs4" id="resep" multiple="multiple" name="resep[]" data-placeholder="Select a State"
style="width: 100%;">
#foreach($obat as $row)
<option value="{{$row->kd_obat }}" >{{$row->kd_obat }}</option>
#endforeach
</select>
</div>
Why do not you try looping through your multiple select value and save the array's element: Like this : foreach($request->resep as $item){..then save your data here..}.
i think you should use 'save' function
public function store(Request $request)
{
$id = $request->id;
$post = Rek_medik::updateOrCreate(['id' => $id],
[
'kode_rekmed' => $request->kode_rekmed,
'kode_register' => $request->kode_register,
'kode_pasien' => $request->kode_pasien,
'nama_pemeriksa' => $request->nama_pemeriksa,
'tgl_rekmed' => Carbon::now(),
'anamnesis' => $request->anamnesis,
'pemeriksaan' => $request->pemeriksaan,
'resep' => $request->resep,
]);
$result = $post->save();
return response()->json($post);
}
Well, u can modify your array first before save it to database. I assume your resep column type is varchar. Here's my solution
$id = $request->id;
foreach($request->resep[] as $r){
$resep[] = $r;
}
$post = Rek_medik::updateOrCreate(['id' => $id],
[
'kode_rekmed' => $request->kode_rekmed,
'kode_register' => $request->kode_register,
'kode_pasien' => $request->kode_pasien,
'nama_pemeriksa' => $request->nama_pemeriksa,
'tgl_rekmed' => Carbon::now(),
'anamnesis' => $request->anamnesis,
'pemeriksaan' => $request->pemeriksaan,
'resep' => json_encode($resep),
]);
return response()->json($post);
json_encode function is to convert $resep array to string. You can change it back to array with json_decode function
json_decode($resep);
I'm using the Spatie MediaLibrary library in a Laravel application. I want to upload 0 or more photos to my app via a REST API.
I can get it to work when the photo attribute contains 1 file
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'slug' => 'required',
'description' => 'required',
'price' => 'required|integer',
'photo' => 'nullable|file'
]);
$listing = Listing::Create([
'user_id' => auth('api')->user()->id,
'name' => $request->name,
'slug' => $request->slug,
'description' => $request->description,
'price' => $request->price,
]);
// stores the photo
if ($request->hasFile('photo')) {
$listing->addMediaFromRequest('photo')->toMediaCollection('photos');
}
return new ListingResource($listing);
}
The postman request looks as follows:
I know want to change the code so it can handle multiple photos in the request. I'm using the following code in the controller above to do so:
if ($request->hasFile('photo')) {
foreach ($request->input('photo', []) as $photo) {
$listing->addMediaFromRequest('photo')->toMediaCollection('photos');
}
}
and I have changed the attribute to photos[] instead of photo.
The code never goes into the foreach loop even.
Anyone has a hint on how to solve this?
Apparently the Spatie Medialibrary has a function called addMultipleMediaFromRequest. The full code is now
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'slug' => 'required',
'description' => 'required',
'price' => 'required|integer',
'photo' => 'nullable'
]);
$listing = Listing::Create([
'user_id' => auth('api')->user()->id,
'name' => $request->name,
'slug' => $request->slug,
'description' => $request->description,
'price' => $request->price,
]);
if ($request->hasFile('photo')) {
$fileAdders = $listing->addMultipleMediaFromRequest(['photo'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection('photos');
});
}
return new ListingResource($listing);
}
In Postman, I'm calling it as follows:
documentation reference
I managed to upload multiple files like this:
if($request->file('photos')) {
foreach ($request->file('photos') as $photo) {
$post->addMedia($photo)->toMediaCollection('post');
}
}
Check this out:
https://github.com/spatie/laravel-medialibrary/issues/227#issuecomment-220794240
This code is working for me.
View
<input type="file" name="photo[]" multiple />
ListingController
public function store(Request $request)
{
if ($request->hasFile('photo')) {
$fileAdders = $listing->addMultipleMediaFromRequest(['photo'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection('photos');
});
}
}
I have this following problem while trying to Edit data already existing in my database.
my controller name is UserController.php
Error: Creating default object from empty value.
I can Pull data from my database and post it in my form. when trying editing that error occurs.
Here is the block of code that causes the error.
public function update(Request $request, $id)
{
$this->validate($request, [
'fname' => 'required',
'lname' => 'required',
'email' => 'required',
'phone' => 'required',
'address' => 'required',
'country' => 'required',
'city' => 'required',
'bday' => 'required',
'username' => 'required',
'password' => 'required',
'access' => 'required'
]);
$userList = users::find($id);
$userList->fname = $request->get('fname');
$userList->lname = $request->get('lname');
$userList->email = $request->get('email');
$userList->phone = $request->get('phone');
$userList->address = $request->get('address');
$userList->country = $request->get('country');
$userList->city = $request->get('city');
$userList->bday = $request->get('bday');
$userList->username = $request->get('username');
$userList->password = $request->get('password');
$userList->access = $request->get('access');
$userList->save();
return redirect()->route('users.index')->with('success', 'Data Updated');
}
I can see from the debugger that I send the new data
GET Data empty
POST Data
_token "mnC6GliLHdSazZkEpaxZQ97aAChr2LObcc9clMlk"
_method "PATCH"
fname "test"
lname "user"
email "test#user.lara"
phone "12345678990"
address "Streat"
country "countryplace"
city "somecity"
bday "2018-01-01"
username "tester"
password "test"
access "Client"
But It highlights $userList->fname = $request->get('fname');
and says : "Creating default object from empty value"
I am new to laravel and cant understand why this is happening.
is it because of my form?
<form method="post" action="{{action('UserController#update','$id')}}">
{{csrf_field()}}
<input type="hidden" name="_method" value="PATCH" />
I think the problem is here:
<form method="post" action="{{action('UserController#update','$id')}}">
You should not use quotes for $id, it should be:
<form method="post" action="{{action('UserController#update',$id)}}">
Now because you have quotes, in line:
$userList = users::find($id);
no user is found, because in fact it's doing:
$userList = users::find('$id');
I'm trying to send two arguments and one request to controller with post request. This is the code where I send the arguments:
<form class="form-horizontal" name="form1" method="post" action="{{ route('shipment_view', $uniqueid, $category_name) }}" role="form" enctype="multipart/form-data">
And this is the controller where I'm sending the arguments:
public function storeShipment(Request $request, $number, $category_name){
$category = Category::where('category_name', $category_name)->first();
$user = Auth::user();
$item = new Item([
'id' => $user->id,
'category_id' => $category->id,
'unq' => $number,
'fullname' => $request->input('name'),
]);
$item->save();
}
But when I open the view it gives me error
ErrorException in NewShipmentController.php line 53:
Missing argument 3 for App\Http\Controllers\NewShipmentController::storeShipment()
Update:
My route function:
Route::post('/ship/preview/{number}',[
'uses' => 'Controllerr#storeShipment',
'as' => 'shipment_view'
]);
Any ideas?
replace your route function with the below one
Route::post('/ship/preview/{number}/{category_name}',[
'uses' => 'Controllerr#storeShipment',
'as' => 'shipment_view'
]);
Hope this solves your problem.
You can get the all the parameters through request it self But before this how you are defining the route
public function storeShipment(Request $request){
$number = $request->number;
$category_name = $request->number
}