2017-11-17 11 views
0

사진을 업로드 할 수는 있지만 업데이트하려고하면 오류가 없습니다. 파일 이름은 데이터베이스에서 새 그림의 이름으로 바뀝니다. 그리고 공용 폴더에는 그림이 오래되어 새 것으로 표시되지 않습니다.왜 그림을 업데이트하지 않습니까? laravel 5.4

내가 사용 개입 이미지 나는 이해하지 못했다 무엇,

도와주세요.

컨트롤러 : UploadController

use Illuminate\Http\Request; 

use App\Post; 
use Image; 
use Storage; 
use Faker\Provider\File; 

public function update (Request $request, $id) 
{ 
    //validate 
    $this->validate($request, [ 
     'title' => 'required|max:255', 
     'author' => 'required', 
     'text' => 'required', 
     'desc' => 'required', 
     'image' => 'required', 
    ]); 
    $posts = Post::find($id); 
    $posts->title = $request->input('title'); 
    $posts->author = $request->input('author'); 
    $posts->text = $request->input('text'); 
    $posts->desc = $request->input('desc'); 
    $posts->image = $request->input('image'); 

    //update image 
    if ($request->hasFile('image')) 
    { 
     $image = $request->file('image'); 
     $filename = time() . '.' . $image->getClientOriginalExtension(); 
     $location = public_path('images/'. $filename); 
     Image::make($image)->resize(800, 400)->save($location); 
     $oldFilename = $posts->image; 
     //update db 
     $posts->image = $filename; 
     //delete old image 
     Storage::delete($oldFilename); 
    } 
    $posts->save(); 

    return redirect('/'); 
} 

보기 : edit.blade.php

<div class="container"> 
<form method="POST" action="{{ route('goUpdate', [$posts->id]) }}"> 
    {{ csrf_field() }} 
    {!! method_field('patch') !!} 
    @if($posts) 
     <div class="form-group"> 
      <br> 
      <label>title</label> 
      <input name="title" type="text" class="form-control" value="{{ $posts->title }}"> 
     </div> 
     <div class="form-group"> 
      <label>author</label> 
      <input name="author" type="text" class="form-control" value="{{ $posts->author }}"> 
     </div> 
     <div class="form-group"> 
      <label>text</label> 
      <textarea name="text" class="form-control" rows="7">{{ $posts->text }}</textarea> 
     </div> 
     <div class="form-group"> 
      <label>desc</label> 
      <textarea name="desc" class="form-control" rows="5">{{ $posts->desc }}</textarea> 
     </div> 
     <div class="form-group"> 
      <label>image</label> 
      <input type="file" name="image" class="form-control-file" value="" > 
     </div> 
     <div> 
      <input name="submit" type="submit" class="btn btn-primary" value="update"/> 
      <a href="{{ url('/') }}" class="btn btn-primary">back</a> 
     </div> 
    @endif 
</form> 
<br> 

filesystems.php

'disks' => [ 

    'local' => [ 
     'driver' => 'local', 
     'root' => public_path('images/'), 
    ], 

    'public' => [ 
     'driver' => 'local', 
     'root' => storage_path('app/public'), 
     'url' => env('APP_URL').'/storage', 
     'visibility' => 'public', 
    ], 

    's3' => [ 
     'driver' => 's3', 
     'key' => env('AWS_ACCESS_KEY_ID'), 
     'secret' => env('AWS_SECRET_ACCESS_KEY'), 
     'region' => env('AWS_DEFAULT_REGION'), 
     'bucket' => env('AWS_BUCKET'), 
    ], 

답변

0

이 도움이 될 수 있습니다이

$image = $request->file('image'); 
$filename = time() . '.' . $image->getClientOriginalName(); 
$path = public_path('events/' . $filename); 
Image::make($image->getRealPath())->resize(300, 300)->save($path); 
$event->image = 'events/' . $filename; 

희망을 사용해보십시오 당신

+0

모든 같은, 단지 당신이 변수 $ 이벤트와 새로운 디렉토리 '이벤트를 /'필요한 이유를 나는 이해하지 못했다 –

+0

그래 왜냐하면 내가 public/events 디렉토리에 저장하기 때문이다.'if ($ request-> hasFile ('image'))'를 제거하려고 했는가? 아니면 어쩌면 image의 값을 반환 할 수 있는가? null' @ Иван –

+0

문제가 무엇인지 알아 냈습니다. 보기 태그 : enctype = "multipart/form-data 그러나 지금 또 다른 문제는 오래된 그림이 삭제되지 않는다는 것입니다 : –