图片上传在 laravel 5.1 中不起作用
Image upload not working in laravel 5.1
我目前正在做一个叫医生管理系统的项目。在这个项目中,我被困住了,因为我无法上传我的图像,也无法定义我的图像所在的位置。我想每页显示 2 个医院信息,但它为我提供了所有数据。
我的路线文件是
Route::get('/profile/{id}', array('as' =>'profile' ,'uses' => 'ProfileController@index'));
//Route::get('/update', array('as' =>'update' ,'uses' => 'ProfileController@postUpdate'));
Route::get('/profile/{profile_id}/edit', [
'uses' => 'ProfileController@getUpdate',
'as' => 'edit'
]);
Route::post('profile/update', [
'uses' => 'ProfileController@postUpdate',
'as' => 'update'
]);
我的 Blade 模板是
<?php $active="profile"; ?>
@extends('admin.dashboard')
@section('content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
User Profile
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li class="active">User profile</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-10">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Quick Example</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form action="{{ route('update') }}" role="form" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name" {{ $errors->has('name') ? 'class=has-error' : '' }} value="{{ Request::old('name') ? Request::old('name') : isset($doctor) ? $doctor->name : '' }}">
</div>
<div class="form-group">
<label for="institute">Institute Name</label>
<input type="text" class="form-control" name="institute" id="institute" {{ $errors->has('institute') ? 'class=has-error' : '' }} value="{{ Request::old('institute') ? Request::old('institute') : isset($doctor) ? $doctor->institute : '' }}">
</div>
<div class="form-group">
<label for="education">Degree</label>
<input type="text" class="form-control" name="education" id="education" {{ $errors->has('education') ? 'class=has-error' : '' }} value="{{ Request::old('education') ? Request::old('education') : isset($doctor) ? $doctor->education : '' }}">
</div>
<div class="form-group">
<label for="specialty">Specialty</label>
<input type="text" class="form-control" name="specialty" id="specialty" {{ $errors->has('specialty') ? 'class=has-error' : '' }} value="{{ Request::old('specialty') ? Request::old('specialty') : isset($doctor) ? $doctor->specialty : '' }}">
</div>
<div class="form-group">
<label for="phone">Mobile No.</label>
<input type="number" class="form-control" name="phone" id="phone" {{ $errors->has('phone') ? 'class=has-error' : '' }} value="{{ Request::old('phone') ? Request::old('phone') : isset($doctor) ? $doctor->phone : '' }}">
</div>
<div class="form-group">
<label for="time">Visiting Time</label>
<input type="text" class="form-control" name="time" id="time" {{ $errors->has('time') ? 'class=has-error' : '' }} value="{{ Request::old('time') ? Request::old('time') : isset($doctor) ? $doctor->time : '' }}">
</div>
<div class="form-group">
<label for="fee">Fee</label>
<input type="number" class="form-control" name="fee" id="fee" {{ $errors->has('fee') ? 'class=has-error' : '' }} value="{{ Request::old('fee') ? Request::old('fee') : isset($doctor) ? $doctor->fee : '' }}">
</div>
<div class="form-group">
<label for="division">Division</label>
<select name="division_id">
@foreach($divisions as $division)
<option value="{{ $division->id }}">{{ $division->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="district">District</label>
<select name="district_id">
@foreach($districts as $district)
<option value="{{ $district->id }}">{{ $district->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="dcategory">Category</label>
<select name="dcategory_id">
@foreach($dcategories as $dcategory)
<option value="{{ $dcategory->id }}">{{ $dcategory->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="image">Chose the picture</label>
<input type="file" class="form-control" name="image" id="image" {{ $errors->has('image') ? 'class=has-error' : '' }} value="{{ Request::old('image') ? Request::old('image') : isset($photo) ? $photo->image : '' }}">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
<input type="hidden" name="id" value="{{ $doctor->id }}">
</div>
</form>
</div>
<!-- /.box -->
</div>
<!--/.col (left) -->
</div>
<!-- /.row -->
</section>
<!-- /.content -->
@endsection
我的控制器是
public function getUpdate($id) {
$divisions = Division::all();
$districts = District::all();
$dcategories = Dcategory::all();
$profile = Auth::user();
$doctor=Doctor::find($id);
// $post = Post::find($post_id);
// if(!$post) {
// return redirect()->route('blog.index')->with(['fail' => 'Post Not Found']);
// }
return view('admin.article.edit')
->with('divisions',$divisions)
->with('districts',$districts)
->with('dcategories',$dcategories)
->with('profile',$profile)
->with('doctor',$doctor);
}
public function postUpdate(Request $request ) {
$this->validate($request, [
'name' => 'required|max:120',
'fee' => 'required|max:5',
'division_id' => '',
'district_id' => '',
'dcategory_id' => '',
'education' => '',
'institute' => '',
'specialty' => '',
'hospital' => '',
'time' => '',
'phone' => '',
'image' =>''
]);
$doctor = Doctor::find($request['id']);
$doctor->name = $request['name'];
$doctor->division_id = $request['division_id'];
$doctor->district_id = $request['district_id'];
$doctor->dcategory_id = $request['dcategory_id'];
$doctor->institute = $request['institute'];
$doctor->education = $request['education'];
$doctor->specialty = $request['specialty'];
$doctor->hospital = $request['hospital'];
$doctor->phone = $request['phone'];
$doctor->time = $request['time'];
$doctor->fee = $request['fee'];
$photo = Photo::find($request['id']);
$upload='uploads/logo';
$file=Input::file('file');
$file->move('uploads', $file->getClientOriginalName());
$photo->image = $request['image'];
// $file = Input::file('pic');
// $img = Image::make($request->file('pic')->getRealPath());
// //$img = Image::make($file);
// Response::make($img->encode('jpeg'));
// $doctor->pic = $img;
$doctor->update();
$photo->update();
return redirect()->route('profile')->with(['success' => 'Profile Successfully Updated']);
}
你应该写这个。这将解决您的问题
public function postUpdate(Request $request ) {
$this->validate($request, [
'name' => 'required|max:120',
'fee' => 'required|max:5',
'division_id' => '',
'district_id' => '',
'dcategory_id' => '',
'education' => '',
'institute' => '',
'specialty' => '',
'hospital' => '',
'time' => '',
'phone' => '',
'image' =>''
]);
$doctor = Doctor::find($request['id']);
$doctor->name = $request['name'];
$doctor->division_id = $request['division_id'];
$doctor->district_id = $request['district_id'];
$doctor->dcategory_id = $request['dcategory_id'];
$doctor->institute = $request['institute'];
$doctor->education = $request['education'];
$doctor->specialty = $request['specialty'];
$doctor->hospital = $request['hospital'];
$doctor->phone = $request['phone'];
$doctor->time = $request['time'];
$doctor->fee = $request['fee'];
// $photo = Photo::find($request['id']);
$logo=$request->file('image');
$upload='uploads/logo';
$filename=$logo->getClientOriginalName();
$success=$logo->move($upload,$filename);
if($success) {
$doctor->image = $filename;
}
// $file = Input::file('pic');
// $img = Image::make($request->file('pic')->getRealPath());
// //$img = Image::make($file);
// Response::make($img->encode('jpeg'));
// $doctor->pic = $img;
$doctor->update();
return redirect()->route('profile')->with(['success' => 'Profile Successfully Updated']);
}
我目前正在做一个叫医生管理系统的项目。在这个项目中,我被困住了,因为我无法上传我的图像,也无法定义我的图像所在的位置。我想每页显示 2 个医院信息,但它为我提供了所有数据。
我的路线文件是
Route::get('/profile/{id}', array('as' =>'profile' ,'uses' => 'ProfileController@index'));
//Route::get('/update', array('as' =>'update' ,'uses' => 'ProfileController@postUpdate'));
Route::get('/profile/{profile_id}/edit', [
'uses' => 'ProfileController@getUpdate',
'as' => 'edit'
]);
Route::post('profile/update', [
'uses' => 'ProfileController@postUpdate',
'as' => 'update'
]);
我的 Blade 模板是
<?php $active="profile"; ?>
@extends('admin.dashboard')
@section('content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
User Profile
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li class="active">User profile</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-10">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Quick Example</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form action="{{ route('update') }}" role="form" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name" {{ $errors->has('name') ? 'class=has-error' : '' }} value="{{ Request::old('name') ? Request::old('name') : isset($doctor) ? $doctor->name : '' }}">
</div>
<div class="form-group">
<label for="institute">Institute Name</label>
<input type="text" class="form-control" name="institute" id="institute" {{ $errors->has('institute') ? 'class=has-error' : '' }} value="{{ Request::old('institute') ? Request::old('institute') : isset($doctor) ? $doctor->institute : '' }}">
</div>
<div class="form-group">
<label for="education">Degree</label>
<input type="text" class="form-control" name="education" id="education" {{ $errors->has('education') ? 'class=has-error' : '' }} value="{{ Request::old('education') ? Request::old('education') : isset($doctor) ? $doctor->education : '' }}">
</div>
<div class="form-group">
<label for="specialty">Specialty</label>
<input type="text" class="form-control" name="specialty" id="specialty" {{ $errors->has('specialty') ? 'class=has-error' : '' }} value="{{ Request::old('specialty') ? Request::old('specialty') : isset($doctor) ? $doctor->specialty : '' }}">
</div>
<div class="form-group">
<label for="phone">Mobile No.</label>
<input type="number" class="form-control" name="phone" id="phone" {{ $errors->has('phone') ? 'class=has-error' : '' }} value="{{ Request::old('phone') ? Request::old('phone') : isset($doctor) ? $doctor->phone : '' }}">
</div>
<div class="form-group">
<label for="time">Visiting Time</label>
<input type="text" class="form-control" name="time" id="time" {{ $errors->has('time') ? 'class=has-error' : '' }} value="{{ Request::old('time') ? Request::old('time') : isset($doctor) ? $doctor->time : '' }}">
</div>
<div class="form-group">
<label for="fee">Fee</label>
<input type="number" class="form-control" name="fee" id="fee" {{ $errors->has('fee') ? 'class=has-error' : '' }} value="{{ Request::old('fee') ? Request::old('fee') : isset($doctor) ? $doctor->fee : '' }}">
</div>
<div class="form-group">
<label for="division">Division</label>
<select name="division_id">
@foreach($divisions as $division)
<option value="{{ $division->id }}">{{ $division->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="district">District</label>
<select name="district_id">
@foreach($districts as $district)
<option value="{{ $district->id }}">{{ $district->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="dcategory">Category</label>
<select name="dcategory_id">
@foreach($dcategories as $dcategory)
<option value="{{ $dcategory->id }}">{{ $dcategory->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="image">Chose the picture</label>
<input type="file" class="form-control" name="image" id="image" {{ $errors->has('image') ? 'class=has-error' : '' }} value="{{ Request::old('image') ? Request::old('image') : isset($photo) ? $photo->image : '' }}">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
<input type="hidden" name="id" value="{{ $doctor->id }}">
</div>
</form>
</div>
<!-- /.box -->
</div>
<!--/.col (left) -->
</div>
<!-- /.row -->
</section>
<!-- /.content -->
@endsection
我的控制器是
public function getUpdate($id) {
$divisions = Division::all();
$districts = District::all();
$dcategories = Dcategory::all();
$profile = Auth::user();
$doctor=Doctor::find($id);
// $post = Post::find($post_id);
// if(!$post) {
// return redirect()->route('blog.index')->with(['fail' => 'Post Not Found']);
// }
return view('admin.article.edit')
->with('divisions',$divisions)
->with('districts',$districts)
->with('dcategories',$dcategories)
->with('profile',$profile)
->with('doctor',$doctor);
}
public function postUpdate(Request $request ) {
$this->validate($request, [
'name' => 'required|max:120',
'fee' => 'required|max:5',
'division_id' => '',
'district_id' => '',
'dcategory_id' => '',
'education' => '',
'institute' => '',
'specialty' => '',
'hospital' => '',
'time' => '',
'phone' => '',
'image' =>''
]);
$doctor = Doctor::find($request['id']);
$doctor->name = $request['name'];
$doctor->division_id = $request['division_id'];
$doctor->district_id = $request['district_id'];
$doctor->dcategory_id = $request['dcategory_id'];
$doctor->institute = $request['institute'];
$doctor->education = $request['education'];
$doctor->specialty = $request['specialty'];
$doctor->hospital = $request['hospital'];
$doctor->phone = $request['phone'];
$doctor->time = $request['time'];
$doctor->fee = $request['fee'];
$photo = Photo::find($request['id']);
$upload='uploads/logo';
$file=Input::file('file');
$file->move('uploads', $file->getClientOriginalName());
$photo->image = $request['image'];
// $file = Input::file('pic');
// $img = Image::make($request->file('pic')->getRealPath());
// //$img = Image::make($file);
// Response::make($img->encode('jpeg'));
// $doctor->pic = $img;
$doctor->update();
$photo->update();
return redirect()->route('profile')->with(['success' => 'Profile Successfully Updated']);
}
你应该写这个。这将解决您的问题
public function postUpdate(Request $request ) {
$this->validate($request, [
'name' => 'required|max:120',
'fee' => 'required|max:5',
'division_id' => '',
'district_id' => '',
'dcategory_id' => '',
'education' => '',
'institute' => '',
'specialty' => '',
'hospital' => '',
'time' => '',
'phone' => '',
'image' =>''
]);
$doctor = Doctor::find($request['id']);
$doctor->name = $request['name'];
$doctor->division_id = $request['division_id'];
$doctor->district_id = $request['district_id'];
$doctor->dcategory_id = $request['dcategory_id'];
$doctor->institute = $request['institute'];
$doctor->education = $request['education'];
$doctor->specialty = $request['specialty'];
$doctor->hospital = $request['hospital'];
$doctor->phone = $request['phone'];
$doctor->time = $request['time'];
$doctor->fee = $request['fee'];
// $photo = Photo::find($request['id']);
$logo=$request->file('image');
$upload='uploads/logo';
$filename=$logo->getClientOriginalName();
$success=$logo->move($upload,$filename);
if($success) {
$doctor->image = $filename;
}
// $file = Input::file('pic');
// $img = Image::make($request->file('pic')->getRealPath());
// //$img = Image::make($file);
// Response::make($img->encode('jpeg'));
// $doctor->pic = $img;
$doctor->update();
return redirect()->route('profile')->with(['success' => 'Profile Successfully Updated']);
}