– htmlべだ書と同様、ヘッダにdropzoneのcss、フッタにjsを読み込ませればいい。
– dropzoneはfileのnameがglobalで’file’なので、controllerで受け取るときは、$request->file(‘file’);と書く。
/resources/views/media/index.blade.php
@if($photos) <table class="table table-striped"> <thead> <tr> <th>Id</th> <th>Name</th> <th>Created date</th> </tr> </thead> <tbody> @foreach($photos as $photo) <tr> <td>{{$photo->id}}</td> <td><img height="50" src="/{{$photo->file}}"></td> <td>{{$photo->created_at ? $photo->created_at : 'no date'}}</td> </tr> @endforeach </tbody> </table> @endif
$ php artisan make:controller AdminMediasController
route.php
Route::group(['middleware'=>'admin'], function(){ Route::resource('admin/users', 'AdminUsersController'); Route::resource('admin/posts', 'AdminPostsController'); Route::resource('admin/categories', 'AdminCategoriesController'); Route::resource('admin/media', 'AdminMediasController'); });
layouts/admin.blade.php
<li> <a href="{{route('admin.medias.index')}}">All Media</a> </li> s <li> <a href="{{route('admin.medias.create')}}">Upload Media</a> </li>
AdminMediasController.php
public function index(){ $photos = Photo::all(); return view('admin.media.index', compact('photos')); } public function create(){ return view('admin.media.create'); }
https://www.dropzonejs.com/
create.blade.php
@extends('layouts.admin') @section('styles') <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/min/dropzone.min.css"> @stop @section('content') <h1>Upload Media</h1> {!! Form::open(['method'=>'POST', 'action'=>'AdminMediasController@store','class'=>'dropzone', 'files'=>true]) !!} {{ csrf_field()}} <div class="form-group"> {!! Form::submit('Create Post', ['class'=>'btn btn-primary']) !!} </div> {!! Form::close() !!} @stop @section('scripts') <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/min/dropzone.min.js"></script> @stop
AdminMediasController.php
public function store(Request $request){ $file = $request->file('file'); $name = time(). $file->getClientOriginalName(); $file->move('images', $name); Photo::create(['file'=>$name]); } public function destroy($id){ $photo = Photo::findOrFail($id); unlink(public_path(). '/'. $photo->file); $photo->delete(); return redirect('/admin/media'); }
index.blade.php
<tbody> @foreach($photos as $photo) <tr> <td>{{$photo->id}}</td> <td><img height="50" src="/{{$photo->file}}"></td> <td>{{$photo->created_at ? $photo->created_at : 'no date'}}</td> <td> {!! Form::open(['method'=>'DELETE', 'action'=>['AdminMediasController@destroy', $photo->id]]) !!} {{ csrf_field()}} <div class="form-group"> {!! Form::submit('Delete', ['class'=>'btn btn-danger']) !!} </div> {!! Form::close() !!} </td> </tr> @endforeach </tbody>
対象ページのみcssやjsをインクルードさせたい時は、ヘッダとフッタのsectionに追加で書き込めば良い😺