如何在 laravel 6 上上传图片?

我试图在 laravel 6 上编辑图像,但它没有前进到下一个视图,停留在表单视图上。


我看过很多 laravel 5.8 和 6 的教程。我无法让它以任何方式工作


这是 de 控制器:


 public function update(Request $request, $id)

{



    $validator = $request->validate([

       'titulo' => 'required | max:50', //campo obligatorio y máximo 50 caracteres

       'contenido' => 'required | max:150', 

       'imagen' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:4096',

    ]);


     $image_name = time().'.'.$request->imagen->getClientOriginalExtension();

     $request->image->move(public_path('images'), $image_name);



     $datos = array(

        'titulo' => $request->titulo,

        'contenido' => $request->contenido,

        'imagen' => $image_name,

    );


    Noticia::whereId($id)->update($datos);



    return redirect('/mostrar');

}

这是 Web.php 文件:


Route::get('/actualizar/{id}', 'crearNoticiaController@update')->name('actualizar');

Route::get('/editar/{id}', 'crearNoticiaController@edit')->name('editar');

这是表格文件:


<div class="subir-imagen">

    <form method="get" action="{{ route('actualizar', $noticia->id) }}"  enctype="multipart/form-data">

        @csrf   

        <div class="crear-titulo">

            <input class="titulo" type="text" name="titulo" placeholder="Escriba el titulo" value="{{$noticia->titulo}}">

        </div>


        <div class="crear-contenido">

            <textarea  class="ckeditor" name="contenido" placeholder="Escriba el contenido" >

                {{$noticia->contenido}}

            </textarea>

        </div>


        <table border="2">

            <tr>

                <td><img src="{{URL::to('/')}}/images/{{$noticia->imagen}}" alt="imagen" width="250" align="left"/></td>

            </tr>

        </table>


冉冉说
浏览 162回答 3
3回答

一只萌萌小番薯

我用这种方式解决了:在 web.php 我把补丁而不是得到Route::patch('/actualizar/{id}', 'crearNoticiaController@update')->name('actualizar');在我输入的编辑刀片中:@method('PATCH')这是控制器中的更新:&nbsp;public function update(Request $request, $id){&nbsp; &nbsp; $noticia = Noticia::findOrFail($id);&nbsp; &nbsp; $noticia->titulo = $request->get('titulo');&nbsp;&nbsp; &nbsp; $noticia->contenido = $request->get('contenido');&nbsp; &nbsp; $noticia->imagen = $request->file('imagen');&nbsp; &nbsp; $validator = $request->validate([&nbsp; &nbsp; &nbsp; &nbsp;'imagen' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:4096',&nbsp; &nbsp; ]);&nbsp; &nbsp; $imageName = time().'.'.request()->imagen->getClientOriginalExtension();&nbsp; &nbsp; request()->imagen->move(public_path('images'), $imageName);&nbsp; &nbsp; $noticia->imagen = $imageName;&nbsp; &nbsp; $noticia->update();&nbsp; &nbsp; return redirect('/mostrar'); //Redirigimos a la la vista para mostrar las noticias&nbsp;}

慕田峪9158850

我遇到了同样的问题,但幸运的是我解决了这个问题。我在下面添加了我的解决方案,我认为这会帮助你解决这个问题&nbsp; &nbsp;public function updatePost(Request $request, $id)&nbsp; &nbsp;{&nbsp; &nbsp; $validatedData = $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'required|unique:posts|max:25|min:4',&nbsp; &nbsp; &nbsp; &nbsp; 'image' => 'mimes:jpeg,jpg,png,JPEG,JPG,PNG | max:100000',&nbsp; &nbsp; ]);&nbsp; &nbsp; $data = array();&nbsp; &nbsp; $data['category_id'] = $request->category_id;&nbsp; &nbsp; $data['title'] = $request->title;&nbsp; &nbsp; $data['details'] = $request->details;&nbsp; &nbsp; $image = $request->file('image');&nbsp; &nbsp; if($image)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $image_name = hexdec(uniqid());&nbsp; &nbsp; &nbsp; &nbsp; $ext = strtolower($image->getClientOriginalExtension());&nbsp; &nbsp; &nbsp; &nbsp; $image_full_name = $image_name.'.'.$ext;&nbsp; &nbsp; &nbsp; &nbsp; $upload_path = 'public/assets/img/';&nbsp; &nbsp; &nbsp; &nbsp; $image_url = $upload_path.$image_full_name;&nbsp; &nbsp; &nbsp; &nbsp; $success = $image->move($upload_path,$image_full_name);&nbsp; &nbsp; &nbsp; &nbsp; $data['image'] = $image_url;&nbsp; &nbsp; &nbsp; &nbsp; unlink($request->old_photo);&nbsp; &nbsp; &nbsp; &nbsp; $posts = DB::table('posts')->where('posts.id', $id)->update($data);&nbsp; &nbsp; &nbsp; &nbsp; if($posts)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Redirect()->route('all.posts')->with('success','Posts are inserted successfully');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return back()->with('error', 'Posts are not inserted successfully');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data['image'] = $request->old_photo;&nbsp; &nbsp; &nbsp; &nbsp; $posts = DB::table('posts')->where('posts.id', $id)->update($data);&nbsp; &nbsp; &nbsp; &nbsp; if($posts)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Redirect()->route('all.posts')->with('success','Posts are inserted successfully');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return back()->with('error', 'Posts are not inserted successfully');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}edit_post.blade.php@extends('welcome')@section('content')<div class="container"><div class="row">&nbsp; <div class="col-lg-8 col-md-10 mx-auto">&nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; <a href="{{ route('all.posts') }}" class="btn btn-danger">List Posts</a>&nbsp; &nbsp; </p>&nbsp; &nbsp; @if ($errors->any())&nbsp; &nbsp; <div class="alert alert-danger">&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach ($errors->all() as $error)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>{{ $error }}</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; </div>&nbsp; &nbsp; @endif<form action="{{ url('posts.update_posts/'.$posts->id) }}" method="post" enctype="multipart/form-data">&nbsp; &nbsp; @csrf&nbsp; &nbsp; &nbsp; &nbsp; <div class="control-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group floating-label-form-group controls">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>Category Name</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Category ID</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<select class="form-control" name="category_id">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@foreach($category as $categories)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<option value="{{ $categories->id }}" <?php if ($categories->id == $posts->category_id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "selected"; ?> > {{ $categories->name }} </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="help-block text-danger"></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="control-group">&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group floating-label-form-group controls">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Product Title</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="title" class="form-control" value="{{ $posts->title }}" id="title" required data-validation-required-message="Please product name.">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="help-block text-danger"></p>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="control-group">&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group floating-label-form-group controls">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Details</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <textarea name="details" rows="5" class="form-control" value="{{ $posts->details }}" id="details"></textarea>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="help-block text-danger"></p>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="control-group">&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group floating-label-form-group controls">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Product Image</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="file" name="image" class="form-control" id="image"><br/>&nbsp; &nbsp; &nbsp; &nbsp; Old Image : <img src="{{ URL::to($posts->image) }}" style="hight: 40px; width: 100px">&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="old_photo" value="{{ $posts->image }}">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; <div id="success"></div>&nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-success" id="sendMessageButton">Update</button>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </form>&nbsp; </div></div></div>@endsection

蓝山帝景

首先在您的项目控制台命令上运行:php artisan storage:link然后尝试此代码,如果返回任何错误消息,请告诉我 khow:$imagen = $request->file("imagen");$extension = $imagen->extension();$filename = time().".".$extension;$request->file('imagen')->storeAs("public/images", $filename);最后检查您的public/images文件夹中是否存在图像文件。
打开App,查看更多内容
随时随地看视频慕课网APP