Laravel 5.4图片库

时间:2017-04-17 20:57:58

标签: php laravel laravel-5

我正在构建一个Laravel Web应用程序,我需要一个动态图库,我构建了一个后端管理面板,我可以在其中添加图像,我成功添加并将图像保存到数据库但我无法编辑或删除它们。

错误是:

  

UrlGenerationException.php第17行中的ErrorException:缺少[Route:galleries.update]所需的参数[URI:   后端/画廊/ {画廊}]。 (查看:/var/www/html/tryout101/resources/views/backend/gallery/edit.blade.php)

这是我的路线代码:

<?php
   /*backend access*/
     Route::group(['prefix' => '/backend'], function() {
     /*The route Dashboard main page */
     Route::get('/' , 'AdminController@index')->name('dashboard');
     Route::resource('galleries' , 'GalleriesController');

     });

这是控制器代码:

      <?php

        namespace App\Http\Controllers;
        use App\Gallery;
        use Illuminate\Http\Request;
        use Image; 
        use Illuminate\Support\Facades\Input;

      class GalleriesController extends Controller
    {
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
  $gallery = Gallery::all();
return view('backend.gallery.library', compact('gallery'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('backend.gallery.uploadform');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
  $gallery = new Gallery();
  $this->validate($request, [
    'title' => 'required',
    'image' => 'required'
  ]);

  $gallery->title = $request->title;
  $gallery->description = $request->description;
  if($request->hasFile('image')) {
    $file = Input::file('image');
    $filename = time(). '-' .$file->getClientOriginalName();
    $gallery->image = $filename;
    $file->move(public_path().'/images/', $filename);
  }
  $gallery->save();
  return $this->create()->with('success', 'Image Uploaded 
  Successfully');
}

/**
 * Display the specified resource.
 *
 * @param  \App\Gallery  $gallery
 * @return \Illuminate\Http\Response
 */
public function show(Gallery $gallery)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  \App\Gallery  $gallery
 * @return \Illuminate\Http\Response
 */
public function edit(Gallery $gallery)
{
    if(!$gallery){
      return redirect('dashboard')->with(['fail'=>'post not found']);
    }
    return view('backend.gallery.edit',compact('gallery'));
}

public function update(Request $request, Gallery $gallery)
{
        $this->validate($request, [
        'title'=>'required|max:120',
        'image'=>'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
       ]);

       $gallery->title = $request->title;
       $gallery->description = $request->description;
         if($request->hasFile('image')) {
         $file = Input::file('image');
         $filename = $file->getClientOriginalName();
         $gallery->image = $filename;
         $file->move(public_path().'images/', $filename);
       }
       $gallery->update();
      return Redirect()->route('dashboard')->with(['success'=> 'post 
      successfully updated']);
   }

    public function destroy(Gallery $gallery)
   {
    //
    }
 }

/ 这是我的编辑页面 /

  @extends('layouts.backend-master')
  @section('styles')
  <link rel="stylesheet" href="">
   @endsection
   @section('content')
    @if (count($errors) > 0)
   <div class="alert alert-danger">
    <strong>Whoops!</strong> There were some problems with your input.
   <br><br>
    <ul>
      @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
      @endforeach
    </ul>
  </div>
@endif

<h1>File Upload</h1>
<form action="{{route('galleries.update')}}" method="post" 
 enctype="multipart/form-data">

<div class="input-group">
  <label for="title">Title</label>
  <input type="text" name="title" id="title"/>
</div>

<div class="input-group">
  <label for="description">Description</label>
  <textarea type="text" name="description" id="description" rows="8">
</textarea>
</div>

<div class="input-group">
  <label for="image">Select image to upload:</label>
  <input type="file" name="image" id="file">
</div>

 <button type="submit" class="btn">Update</button>
 <input type="hidden" name="_token" value="{{Session::token()}}">
 <input type="hidden" name="gallery" value="{{$gallery->id}}">
</form>
@endsection
@section('scripts')
@endsection

1 个答案:

答案 0 :(得分:1)

事实是路线&#39; galleries.update&#39;需要一个画廊

因此,在使用该路线调用路线功能时,您应该告诉他您要去哪个画廊

因此,我认为改变

route('galleries.update')

route('galleries.update', $gallery)

将使一切正常