How to retrieve uploaded file edit view blade using Laravel - laravel

I have a project in Laravel-5.8 for upload of files like excel and images.
I have successfully inserted it. I saved the file name in the table and the file itself in a directly. Where I have a problem is how to retrieve the file in the edit view blade.
Controller
public function update_employee_mid_year_comment(UpdateSelfReviewRequest $request, $id) {
$goal = Goal::find($id);
$goal->employee_mid_year_comment = $request->employee_mid_year_comment;
if ($request->employee_mid_year_attachment != "") {
$employee_mid_year_attachment = $request->file('employee_mid_year_attachment');
$new_name = rand() . '.' . $employee_mid_year_attachment->getClientOriginalExtension();
$employee_mid_year_attachment->move(public_path('storage/documents/mid_year'), $new_name);
$goal->employee_mid_year_attachment = $new_name;
}
$goal->save();
DB::commit();
Session::flash('success', 'Comment is Successfully Updated');
return redirect()->back();
}
I am using a modal form:
edit.blade
<div class="modal fade" id="edit{{ $goal->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{route('mid_year_setups.update_employee_mid_year_comment',['id'=>$goal->id])}}" method="post" enctype="multipart/form-data" id="edit_comment-form">
{{ csrf_field() }}
<div class="modal-header">
Update Self-Review Comment
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">Comment:<span style="color:red;">*</span></label>
<textarea rows="2" name="employee_mid_year_comment" class="form-control" placeholder="Enter Comment here" value="{{old('employee_mid_year_comment',$goal->employee_mid_year_comment)}})}}" required data-validation-required-message="This field is required">{{old('employee_mid_year_comment',$goal->employee_mid_year_comment)}}</textarea>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label"> Attachment:</label>
<div class="custom-file">
<div class="custom-file">
<input value="{{old('employee_mid_year_attachment',$goal->employee_mid_year_attachment)}}" type="file" name="employee_mid_year_attachment" class="custom-file-input" id="customFile">
<label class="custom-file-label" for="exampleInputFile">Choose file</label>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" id="edit_comment_btn-submit" class="btn btn-success btn-ok">Save</button>
</div>
</form>
</div>
</div>
</div>
The file name is employee_mid_year_attachment while the file path is: storage/documents/mid_year
When I rendered the edit view blade, it didn't retrieve the attached document. How do I achieve this?
Thanks

use in value input path to file
<input
value="{{asset('storage/documents/mid_year/'.$goal>employee_mid_year_attachment')}}"
type="file" name="employee_mid_year_attachment" class="custom-file-input"
id="customFile">

Related

Laravel - Error: 403 while trying to upload picture

In my Laravel-5.8, I want to upload picture
Http\Controllers\HomeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Hr\HrEmployee;
use App\Http\Requests\Hr\Employee\UploadPictureRequest;
class HomeController extends Controller
{
public function update_picture(UploadPictureRequest $request, $id)
{
DB::beginTransaction();
try{
$employee = HrEmployee::find($id);
if ($request->emp_image != "") {
$emp_image = $request->file('emp_image');
$new_name = rand() . '.' . $emp_image->getClientOriginalExtension();
$emp_image->move(public_path('storage/employees/image'), $new_name);
$employee->emp_image = $new_name;
}
$employee->save();
DB::commit();
Session::flash('success', 'Picture Successfully Uploaded');
return redirect()->route('dashboard');
}
catch (Exception $exception)
{
DB::rollback();
Session::flash('error', 'Action failed!');
return redirect()->back();
}
}
}
view
<span data-toggle="tooltip" data-original-title="Click To Upload Picture">
<a class="btn btn-info btn-block text-white" data-toggle="modal" data-target="#upload-picture{{ $employee->id }}" data-original-title="Picture">
<b>Upload My Picture</b>
</a>
</span>
<div class="modal fade" id="upload-picture{{ $employee->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{route('update_picture',['id'=>$employee->id])}}" method="post" id="update-picture-form">
{{ csrf_field() }}
<div class="modal-header">
Self-Review Comment
</div>
<div class="col-md-12">
<div class="text-center">
#if($employee->emp_image != '')
<input type="image" src="{{ URL::to('/') }}/public/storage/employees/image/{{ $employee->emp_image }}" class="profile-user-img img-fluid img-circle" id="wizardPicturePreview" title="" width="150" height="165" disabled/>
<!--<input type="file" name="emp_image" id="wizard-picture" class="" hidden>-->
<div class="row">
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
<input type="file" name="emp_image" id="wizard-picture" class="form-control">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
</div>
#else
<input type="image" src="{{asset('theme/adminlte3/dist/img/default.png')}}" class="profile-user-img img-fluid img-circle" id="wizardPicturePreview" title="" width="150" height="150" disabled/>
<!--<input type="file" name="emp_image" id="wizard-picture" class="" hidden>-->
<div class="row">
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
<input type="file" name="emp_image" id="wizard-picture" class="form-control">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
</div>
#endif
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" id="upload_pic_btn-submit" class="btn btn-success btn-ok">Save</button>
</div>
</form>
</div>
</div>
</div>
view\dashboard
The form is a modal form inside a view called dashboard
route/web
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
Route::post('update_picture/{id}', [
'uses' => 'HomeController#update_picture',
'as' => 'update_picture'
]);
When I submited the form, I got this error:
Error: 403 while trying to upload picture
Then when I did php artisan route:list, I got:
| POST | update_picture/{id} | update_picture | App\Http\Controllers\HomeController#update_picture | web,auth |
How do I resolve it?
Thank you.
Or simply you can try this
Route::put('update_picture/{id}''HomeController#update_picture')->name('update_picture');
please add this enctype="multipart/form-data" in form tag
<form action="{{route('update_picture',['id'=>$employee->id])}}" method="post" id="update-picture-form" enctype="multipart/form-data">
and replace this line $new_name = rand() . '.' . $emp_image->getClientOriginalExtension();
with this line $new_name = rand() . '.' . $request->emp_image->getClientOriginalExtension();

Getting error when click in my second post for the single page

When I am trying to open first post in single page, it's opening and when trying to open my second post in single page it;s showing "Trying to get property 'title' of non-object"
Here is code
FrontendController
public function singlePost($slug)
{
$post= Post::where('slug', $slug)->first();
return view('single')->with('post', $post)
->with('title', $post->title)
->with('settings', Setting::first())
->with('categories', Category::take(4)->get());
}
single.blade.php
in that I am using same frontend controller for same page
#extends('layouts.frontend')
#section('content')
<div id="product-post">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="heading-section">
<img src="{{$post->featured}}" alt="" />
</div>
</div>
</div>
<div id="single-blog" class="page-section first-section">
<div class="container">
<div class="row">
<div class="product-item col-md-12">
<div class="row">
<div class="col-md-8">
<div class="product-content">
<div class="product-title">
<h3>{{$post->title}}</h3>
<span class="subtitle">4 comments</span>
</div>
<p>
{!! $post->content!!}
</p>
</div>
<div class="leave-form">
<form action="#" method="post" class="leave-comment">
<div class="row">
<div class="name col-md-4">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="email col-md-4">
<input type="text" name="email" id="email" placeholder="Email" />
</div>
<div class="subject col-md-4">
<input type="text" name="subject" id="subject" placeholder="Subject" />
</div>
</div>
<div class="row">
<div class="text col-md-12">
<textarea name="text" placeholder="Comment"></textarea>
</div>
</div>
<div class="send">
<button type="submit">Send</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
You have to check whether the value is coming from table or not before getting exact column value. In that case if your table return empty result you can redirect it to 404 page.
Please refer the code below :
public function singlePost($slug)
{
$post= Post::where('slug', $slug)->first();
if($post) {
return view('single')->with('post', $post)
->with('title', $post->title)
->with('settings', Setting::first())
->with('categories', Category::take(4)->get());
} else {
// You can redirect to 404 page
}
}

updating image hasfile condition

Now i'm trying to update an image but in method update it keeps skip the hasfile condition
public function update(Request $request, $id)
{
$slider = Slider::find($id);
$slider->header = $request->header;
$slider->paragraph=$request->paragraph;
if($request->hasFile('image')){
return 'a';
// $image=$request->file('image');
// $filename=time(). '.' .$image->getClientOriginalExtension();
// $location=public_path('images/' . $filename);
// Image::make($image)->save($location);
// $oldFilename=$slider->image;
// $slider->image=$filename;
// File::delete(public_path('images/'. $oldFilename));
}else{
return 'whatever';
}
}
and here's my view
<form class="form-horizontal" action="{{ route('slider.update',$slider->id) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{method_field('PATCH')}}
<div class="row">
<div class="col-lg-12">
<div class="ibox float-e-margins">
<div class="ibox-title back-change">
<h5>الغلاف </h5>
</div>
<div class="ibox-content">
<div class="row">
<div class="col-md-6">
<div class="image-crop">
<img src="{{asset('images/'.$slider->image)}}">
</div>
</div>
<div class="col-md-6">
<div class="btn-group">
<label title="Upload image file" for="inputImage" class="btn btn-primary">
<input type="file" name="image" id="inputImage" class="hide">
Upload new image
</label>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-primary pull-right" type="submit"> حفظ التغيرات</button>
</div>
</div>
</div>
</div>
</div>
Why can I not get to the condition?
The form is okay and the name of the input is okay, but it still returns to else.

Laravel - data stored in a variable but becomes null in database

I have a problem with my function that saves data into database,
Basically user puts name into a form and when I do console.log I do get that value, I then call my function in controller and everything looks sucessfully - no errors but when I check in mysql name = NULL, why?
My form:
<div class="modal fade" id="myModalHorizontal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">
Website Name
</h4>
</div>
<div class="modal-body">
<form action="{{ action('BuilderController#postDB') }}"
class="form-horizontal" role="form" method = "POST">
<div class="form-group">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" id="code" name="newCode" value="">
<label class="col-sm-2 control-label"
for="website_name">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control"
id="website_name" placeholder="Website Name"/>
</div>
</div>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="form-control margin btn btn-danger"
data-dismiss="modal">
Close
</button>
<button onClick=" updateDatabase(this);" type="submit" class="form-control margin btn btn-success" id="getRequest changes">
Save Website
</button>
</div>
</div>
</div></form>
</div>
JS:
var web_name;
function updateDatabase()
{
code2 = document.getElementById("content-link2").innerHTML;
var newCode = document.getElementById('code').value = code2;
web_name = ($('#website_name').val());
console.log(web_name);
console.log(newCode);
Controller:
public function postDB(Request $request) {
$newName = $request->input('web_name');
$newLat = $request->input('newCode');
$websites = new Website();
$websites->name = $newName;
$websites->html = $newLat;
$websites->save();
// Now we go to our other function
return $this->website($newName);
}
public function website($newName)
{
// Return our "website" object
$html = Website::where('name', $newName)->first();
// Pass the contents of the "html" property to the view
return view('layouts/website', [
'html' => $html->html
]);
}
You need to add name attribute to input element:
<input name="website_name" ...

Cannot retrieve posted data from vue.js ajax call

I'm giving a try to Vue.js, It's much simpler than Angular and then I stuck on this problem. I'm using CodeIgniter for the backend process. Basically I just want to send data using ajax call via Vue.js then I want to retrieve the data in mmy controller. But I can't retrive the data using $this->input->post().
Here is my view. I'm using form on a modal.
<div class="modal fade modal-primary" id="modalObat" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Tambah Obat</h4>
</div>
<form #submit.prevent="onSubmit" class="form-horizontal">
<div class="modal-body">
<div class="form-group">
<label for="nama-obat" class="col-sm-2 control-label">Nama Obat</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="namaObat" placeholder="nama obat" v-model="newObat.nama">
</div>
</div>
<div class="form-group">
<label for="harga" class="col-sm-2 control-label">Harga</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="harga" placeholder="harga" v-model="newObat.harga">
</div>
</div>
<div class="form-group">
<label for="Stok" class="col-sm-2 control-label">Stok</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="stok" placeholder="jumlah" v-model="newObat.stok">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline pull-left" data-dismiss="modal">Close</button>
<button type="submit" id="simpen" class="btn btn-outline">Save Changes</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
This is how I send the data on vue
Then here is my controller
public function tambahObat()
{
$data = array(
'nama' => $this->input->post('nama'),
'harga' => $this->input->post('harga'),
'stok' => $this->input->post('stok')
);
$data1 = $this->input->post('newObat');
$query = $this->obat_m->save($data, null);
if( $query == true )
{
$message = $this->session->set_flashdata('message', 'berhasil menyimpan');
}
else
{
$message = $this->session->set_flashdata('message', 'berhasil mengupdate');
}
redirect('obat');
}
I succeed to send the data as I check on the console
I think there is a problem with the controller but I dont know why. Could you help me, please??
You are trying to access this from inside the post callback, where this refers to something other than your vue instance. Try this format:
this.$http.post('url/to/post',{},function(){
//because of "bind", 'this' is vue
}.bind(this));

Resources