i am new for laravel, i am not able to save the files in database on update function can any one help me for this,I have two related tables where one is a ticekt table and the other a one a documents table. In the documents table are the columns id, doc_name,doc_path,user_id and service_id. I'm trying to edit multiple images when editing a service. documents table not updating remaining things update successful
Cread service code
public function store(Request $request)
{
$rules = [
'email' => 'required|string|max:255',
'typeofservice' => 'required',
'companyname' => 'required',
'representative'=> 'required',
'phone' => 'required',
'services' => 'required',
'applicant' => 'required',
//'document' => 'required',
//'document.*' => 'required',
'remark' => 'required',
];
$validator = Validator::make($request->all(),$rules);
if($validator->fails()){
return back()->with('warning','please Fill manadatory fields');
} else {
//$dates = ;
//dd($dates);
$ticket = new Ticket([
'user_id' => Auth::user()->id,
'ticket_id' => strtoupper(str_random(10)),
'email'=>$request->input('email'),
'typeofservice' => $request->input('typeofservice'),
'companyname' => $request->input('companyname'),
'representative' => $request->input('representative'),
'phone' => $request->input('phone'),
'services' => $request->input('services'),
'applicant' => $request->input('applicant'),
'remark' => $request->input('remark'),
'ticket_submit_date' => date('d-M-Y'),
'status' => "1",
]);
//dd($ticket);
$ticket->save();
$userId = Auth::id();
$last_id = DB::getPdo()->lastInsertId();
if($ticket) {
if($request->hasfile('documents')) {
foreach($request->file('documents') as $doc)
{
$name = $doc->getClientOriginalName();
$destinationPath = 'public/documets/';
$documentPath = date('YmdHis') . "." . $doc->getClientOriginalExtension();
$doc->move($destinationPath, $documentPath);
Document::create([
'doc_name' => $name,
'doc_path' => $documentPath,
'user_id' => $userId,
'ser_id' => $last_id,
]);
}
}
//return $last_id;
$ticket_details = DB::table('ticket')->where('id','=',$last_id)->first();
$users = User::where('id','=',$ticket_details->user_id)->first();
$ticketid = $ticket_details->ticket_id;
$username = $users->first_name.' '.$users->last_name;
$mdata = ['ticketid'=>$ticketid,'name'=>$username];
$user['to']= $users->email;
Mail::send('emails.user_create_application',$mdata,function($message) use ($user){
$message->to($user['to']);
$message->subject('User Create Application');
});
return back()->with("success","Service Requiest Created Successfully! your tracking id:#$ticket->ticket_id" );
}
}
}
For uddate function given below
public function udateuserticket(Request $request, $id){
$rules = [
'email' => 'required|string|max:255',
'typeofservice' => 'required',
'companyname' => 'required',
'representative'=> 'required',
'phone' => 'required',
'services' => 'required',
'applicant' => 'required',
//'document' => 'required',
//'document.*' => 'required',
'remark' => 'required',
];
$email = $request->email;
$typeofservice = $request->typeofservice;
$companyname = $request->companyname;
$representative = $request->representative;
$phone = $request->phone;
$services = $request-> services;
$applicant = $request->applicant;
$remark = $request->remark;
$updateuserticket = Ticket::where('id','=',$id)->update([
'email' => $email,'typeofservice' =>$typeofservice, 'companyname' => $companyname, 'representative' => $representative,'phone' => $phone,'services' => $services, 'applicant' => $applicant, 'remark' => $remark ]);
$userId = Auth::id();
$last_id = DB::getPdo()->lastInsertId();
if($updateuserticket){
if($request->hasfile('documents')) {
foreach($request->file('documents') as $doc)
{
$name = $doc->getClientOriginalName();
$destinationPath = 'public/documets/';
if(File::exists($destinationPath)){
File::delete($destinationPath);
}
$documentPath = date('YmdHis') . "." . $doc->getClientOriginalExtension();
$doc->move($destinationPath, $documentPath);
Document::create([
'doc_name' => $name,
'doc_path' => $documentPath,
'user_id' => $userId,
'ser_id' => $last_id,
]);
}
}
$ticket_details = DB::table('ticket')->where('id','=',$last_id)->first();
//$users = User::where('id','=',$ticket_details->user_id)->first();
//$ticketid = $ticket_details->ticket_id;
//$username = $users->first_name.' '.$users->last_name;
return redirect('showtickets')->with('success','Ticket Updated Successfully!');
}
}
for view
#foreach( $documents as $doc )
<div class="col-md-6">
<input id="documents" type="file" class="form-control" name="documents[]" value="" required>
<img src="{{ url('/') }}/public/documets/{{ $doc->doc_path }}" alt="user-img" class="img-width" style="width:30px;height:30px;">
</div>
#endforeach
This one update only details not able to update documents can you please guid anyone where i am wrong
Related
I am using the url with an id variable to update a record:
Route::patch('ProjectsRevenueUpdateAjax/{id}'...
And I have my controller for this:
public function updateRevenue(ProjectRevenueUpdateRequest $request,$id)
{
// When using stdClass(), we need to prepend with \ so that Laravel won't get confused...
$result = new \stdClass();
$inputs = $request->all();
$projectRevenueToUpdate = ProjectRevenue::find($id);
$projectRevenueToUpdate->update($inputs);
$result->result = 'success';
$result->msg = 'Record updated successfully';
return json_encode($result);
}
So I'm calling the validation through another file which is ProjectRevenueUpdateRequest:
public function rules()
{
$id = $this->id;
$revenue = ProjectRevenue::find($id);
$project_id = $revenue->project_id;
$year = $this->year;
$product_code = $this->product_code;
return [
'project_id' => 'required|unique:project_revenues,project_id,'.$id.',id,year,'.$year.',product_code,'.$product_code,
'year' => 'required|unique:project_revenues,year,'.$id.',id,project_id,'.$project_id.',product_code,'.$product_code,
'product_code' => 'required|unique:project_revenues,product_code,'.$id.',id,year,'.$year.',project_id,'.$project_id,
'jan' => 'required',
'feb' => 'required',
'mar' => 'required',
'apr' => 'required',
'may' => 'required',
'jun' => 'required',
'jul' => 'required',
'aug' => 'required',
'sep' => 'required',
'oct' => 'required',
'nov' => 'required',
'dec' => 'required',
];
}
My problem is that it cannot get the $id = $this->id from the url in my request validation file. How can I do that?
public function update(Request $request, $emp_id)
{
$validator = Validator::make($request->all(), [
'emp_code' => 'required|max:255',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'emp_image' => 'required|image|mimes:jpeg,jpg,png,PNG,JPEG,JPG|max:2048',
'department' => 'required|max:255',
'designation' => 'required|max:255',
'joining_date' => 'required|max:255',
'offer_letter' => 'required|mimes:doc,docx,pdf,DOC,DOCX,PDF|max:2048',
'emp_email' => 'required|email|max:255|unique:employees',
'mobile_number' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
]);
$employeeimg = rand() . '.' . request()->emp_image->getClientOriginalExtension();
request()->emp_image->move(public_path('images'), $employeeimg);
$offerletter = rand() . '.' . request()->offer_letter->getClientOriginalExtension();
request()->offer_letter->move(public_path('docs/new'), $offerletter);
$form_data = array(
'emp_code' => $request->emp_code,
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'emp_image' => $employeeimg,
'department' => $request->department,
'designation' => $request->designation,
'joining_date' => $request->joining_date,
'offer_letter' => $offerletter,
'emp_email' => $request->emp_email,
'mobile_number' => $request->mobile_number,
);
$data = Employee::findOrFail($emp_id);
//dd($form_data);
$data->update($form_data);
return redirect('employee')->with('msg_success', 'Employee updated successfully!');
}
I had getting error while updating the form with file uploads. and the error is
Call to a member function getClientOriginalExtension() on null
please give a solution for this
First, you need to check-in request has to file.
if($request->hasFile('emp_image')){
if (Input::file('emp_image')->isValid()) {
#unlink(IMG_PATH_URL.( $records->emp_image) );
$file = Input::file('emp_image');
$ext = $file->getClientOriginalExtension();
$mainFilename1 = rand().'.'.$ext;
$file->move(public_path('images'), $mainFilename);
}
}
if($request->hasFile('offer_letter')){
if (Input::file('offer_letter')->isValid()) {
#unlink(IMG_PATH_URL.( $records->offer_letter) );
$file = Input::file('offer_letter');
$ext = $file->getClientOriginalExtension();
$mainFilename2 = rand().'.'.$ext;
$file->move(public_path('docs/new'), $mainFilename);
}
}
Make sure You have added the enctype in form
<form action="" method="post" enctype="multipart/form-data">
</form>
Your controller should be like:
public function update(Request $request, $emp_id)
{
$validator = Validator::make($request->all(), [
'emp_code' => 'required|max:255',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'emp_image' => 'required|image|mimes:jpeg,jpg,png,PNG,JPEG,JPG|max:2048',
'department' => 'required|max:255',
'designation' => 'required|max:255',
'joining_date' => 'required|max:255',
'offer_letter' => 'required|mimes:doc,docx,pdf,DOC,DOCX,PDF|max:2048',
'emp_email' => 'required|email|max:255|unique:employees',
'mobile_number' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
]);
$data = Employee::findOrFail($emp_id);
if($data){
$data->emp_code = $request->emp_code;
$data->first_name = $request->first_name;
$data->last_name = $request->last_name;
$data->department = $request->department;
$data->designation = $request->designation;
$data->joining_date = $request->joining_date;
$data->emp_email = $request->emp_email;
$data->mobile_number = $request->mobile_number;
if($request->hasFile('emp_image')){
if (Input::file('emp_image')->isValid()) {
#unlink(IMG_PATH_URL.( $records->emp_image) );
$file = Input::file('emp_image');
$ext = $file->getClientOriginalExtension();
$mainFilename1 = rand().'.'.$ext;
$file->move(public_path('images'), $mainFilename);
$data->emp_image => $mainFilename1;
}
}
if($request->hasFile('offer_letter')){
if (Input::file('offer_letter')->isValid()) {
#unlink(IMG_PATH_URL.( $records->offer_letter) );
$file = Input::file('offer_letter');
$ext = $file->getClientOriginalExtension();
$mainFilename2 = rand().'.'.$ext;
$file->move(public_path('docs/new'), $mainFilename2);
$data->offer_letter => $mainFilename2;
}
}
if( $data->save() ) {
return redirect('employee')->with('msg_success', 'Employee updated successfully!');
}
}
}
public function update(Request $request, $emp_id)
{
$validator = Validator::make($request->all(), [
'emp_code' => 'required|max:255',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'emp_image' => 'required|image|mimes:jpeg,jpg,png,PNG,JPEG,JPG|max:2048',
'department' => 'required|max:255',
'designation' => 'required|max:255',
'joining_date' => 'required|max:255',
'offer_letter' => 'required|mimes:doc,docx,pdf,DOC,DOCX,PDF|max:2048',
'emp_email' => 'required|email|max:255|unique:employees',
'mobile_number' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
]);
$emp_img = $request->file('emp_image');
$employee = rand() . '.' . $emp_img->getClientOriginalExtension();
$emp_img->move(public_path('images'),$employee);
$offer_letter = $request->file('emp_image');
$offer = rand() . '.' . $offer_letter->getClientOriginalExtension();
$offer_letter->move(public_path('docs/new'),$offer);
$form_data = array(
'emp_code' => $request->emp_code,
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'emp_image' => $employee,
'department' => $request->department,
'designation' => $request->designation,
'joining_date' => $request->joining_date,
'offer_letter' => $offer,
'emp_email' => $request->emp_email,
'mobile_number' => $request->mobile_number,
);
$data = Employee::findOrFail($emp_id);
//dd($form_data);
$data->save($form_data);
return redirect('employee')->with('msg_success', 'Employee updated successfully!');
}
and in view form
you have to add
<form action="" method="" enctype="multipart/form-data">
I Hope It Will Help You
hi m trying to save data into db data saves but image isn't saving how to save it in db:
controller:
public function store(Request $request)
{
// dd($request->all());
$request->validate([
'category_name' => 'required',
'category_description' => 'required',
'category_slug' => 'required',
'category_image' => 'required|image',
]);
DB::table('categories')->insert([
'category_name' => $request->category_name,
'category_description' => $request->category_description,
'category_slug' => $request->category_slug,
'category_image' => $request->category_image,
]);
$path = $request->file('category_image');
$path->getClientOriginalName();
$path->move(public_path('images/backend_images/category_images');
return back();
}
Well... In your portion of code, there is no var.save(); so how will eloquent store any data without the given instruction?
Per example do something like this:
$userImage = new UserImage;
$UserImage->save();
public function store(Request $request)
{
// dd($request->all());
$request->validate([
'category_name' => 'required',
'category_description' => 'required',
'category_slug' => 'required',
'category_image' => 'required|image',
]);
$path = $request->file('category_image');
$nameImage = $path->getClientOriginalName();
$path->move(base_path('public/images/backend_images/category_images'), $nameImage);
DB::table('categories')->insert([
'category_name' => $request->category_name,
'category_description' => $request->category_description,
'category_slug' => $request->category_slug,
'category_image' => $nameImage,
]);
return redirect()->back();
}
code in my project and its work!
if ($request->hasFile('image')) {
$file = $request->file('image');
$filename = date('mdYHis') .str_random(5).'.'.$file->extension();
$request->image->move(base_path('public/images/rooms'), $filename);
$request->merge(array('image' => $filename));;
}
I think this would help. But you can browse for more answers.
Save to public directory
$path = 'images/backend_images/category_images';
$file = $request->file('category_image');
$filename = $file->getClientOriginalName();
$file->move(public_path($path), $filename);
And the, save to database
DB::table('categories')->insert([
'category_name' => $request->category_name,
'category_description' => $request->category_description,
'category_slug' => $request->category_slug,
'category_image' => $filename
]);
What to here ?
how to assign the value of $filenameTosTor for my 'photo' which is a database column?
public function update(Request $request, User $user)
{
$filenameWithExt = $request->file('profile_pic')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
Get the Extention
$extention = $request->file('profile_pic')->getClientOriginalExtension();
$filenameToStore = $filename.'_'.time().'.'.$extention;
$path = $request->file('profile_pic')->storeAs('public/profile_image', $filenameToStore);
$userupdate = User::where('id', $user->id)->update
([
'name' => $request->input('name'),
$user->profile_pic =$filenameToStore;
'photo'=> $request->($filenameToStore);
'last_name' => $request->input('last_name'),
'phone_number' => $request->input('phone_number'),
'address' => $request->input('address'),
'facebook_link' => $request->input('facebook_link'),
'twittr_link' => $request->input('twittr_link'),
'youtube_link' => $request->input('youtube_link'),
'Biography' => $request->input('Biography'),
]);
$userupdate->save();
return redirect('user.index');
}
Just use this variable like this:
'photo' => $filenameToStore,
I'm trying to make two functions in controller that have post action and that are in the same page.
My Controller
public function store(Request $request)
{
$status = DB::table('analytics')->where('dienstleistung', '!=', '')->get();
//Save data
$rules = [
'site_id' => 'required',
'dienstleistung' => 'required',
'objekt' => 'required',
'zimmer' => 'required',
'vorname' => 'required',
'name' => 'required',
'strasse' => 'required',
'ort' => 'required',
'plz' => 'required',
'tel' => 'required',
'email' => 'required|email',
'reinigungstermin' => 'required',
'gekommen' => 'required',
'message' => 'required',
'status' => 'required',
'answer' => 'required',
'notiz' => 'required',
'userId' => 'required',
];
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()) {
return Redirect::to('anfrage')
->withErrors($validator)
->withInput();
}
else {
$anfrage = new Analytic();
$anfrage->site_id = Input::get('site_id');
$anfrage->dienstleistung = Input::get('dienstleistung');
$anfrage->objekt = Input::get('objekt');
$anfrage->zimmer = Input::get('zimmer');
$anfrage->vorname = Input::get('vorname');
$anfrage->name = Input::get('name');
$anfrage->strasse = Input::get('strasse');
$anfrage->ort = Input::get('ort');
$anfrage->plz = Input::get('plz');
$anfrage->tel = Input::get('tel');
$anfrage->email = Input::get('email');
$anfrage->reinigungstermin = Input::get('reinigungstermin');
$anfrage->gekommen = Input::get('gekommen');
$anfrage->message = Input::get('message');
$anfrage->status = Input::get('status');
$anfrage->answer = Input::get('answer');
$anfrage->notiz = Input::get('notiz');
$anfrage->userId = Input::get('userId');
try {
$anfrage->save();
flash()->success(trans('app.success'));
return Redirect::to('anfrage');
} catch (\Exception $e) {
Log::writeException($e);
return Redirect::to('anfrage')
->withErrors($e->getMessage())
->withInput();
}
}
}
public function editItem(Request $request) {
$anfrages = Analytic::find($request['id'] );
$anfrages->status = $request->status;
$anfrages->answer = $request->answer;
$anfrages->notiz = $request->notiz;
$anfrages->save();
return response ()->json( $anfrages );
}
My route:
Route::post('anfrage', 'AnfrageController#store');
Route::post ( 'anfrage', 'AnfrageController#editItem' );
EditItem function is OK, it makes changes when I want to edit data, but when I want to store data, message being displayed is:
creating default object from empty value
So, I need to leave active only one of these function, both are not working.