Skip to content
Snippets Groups Projects
Commit 46d36f76 authored by Wellington Gabriel Vicente de Souza's avatar Wellington Gabriel Vicente de Souza
Browse files

add image crud

parent dd9c01f0
No related branches found
No related tags found
No related merge requests found
Showing
with 482 additions and 4 deletions
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use App\Http\Requests\ImageRequest;
use App\Http\Requests\UpdateImageRequest;
use App\Image;
class GalleryController extends Controller
{
public function index()
{
$images = Image::all();
return view('dashboard.galeria.index')->with(['images' => $images]);
}
public function create()
{
return view('dashboard.galeria.create');
}
public function store(ImageRequest $request)
{
$image = Image::create([
'title' => $request['title'],
'description' => $request['description'],
/* Cadastra a imagem em storage/app/public/images/ e devolve o caminho como public/images/nomeImagem.extensao
depois remove o public/images/, deixando apenas o nome da imagem */
'path' => str_replace("public/images/galeria/", "", ($request->file('image')->store('public/images/galeria'))),
]);
$image->save();
return redirect('dashboard/galeria');
}
public function show($id)
{
$image = Image::findOrFail($id);
return view('dashboard.galeria.show')->with(['image' => $image]);
}
public function edit($id)
{
$image = Image::findOrFail($id);
return view('dashboard.galeria.edit')->with(['image' => $image]);
}
public function update(UpdateImageRequest $request, $id)
{
$image = Image::findOrFail($id);
$image->title = $request->title;
$image->description = $request->description;
$image->update();
return redirect('dashboard/galeria');
}
public function destroy($id)
{
$image = Image::findOrFail($id);
File::delete('storage/images/galeria/' . $image->path);
Image::destroy($id);
return redirect('dashboard/galeria');
}
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ImageRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required|string|max:100',
'description' => 'string|max:200',
'image' => 'required|file|image|max:4096'
];
}
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateImageRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required|string|max:100',
'description' => 'string|max:200'
];
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
protected $fillable = [
'title', 'description', 'path'
];
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateImagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('description');
$table->string('path');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('images');
}
}
#image-preview {
max-width: 260px;
}
.options-dropdown-btn {
background:none!important;
border:none;
font: inherit;
cursor: pointer;
display: block;
padding: 6px 20px!important;
clear: both;
font-weight: 400;
line-height: 1.42857143;
color: #f05050;
white-space: nowrap;
width: 100%;
text-align: left;
}
.options-dropdown-btn:hover {
text-decoration: none;
background-color: #f5f5f5!important;
}
.dropdown-btn:hover {
color: #262626;
text-decoration: none;
background-color: #f5f5f5!important;
}
.thumb-gallery {
width: 100%;
max-width: inherit;
height: 150px;
}
.img-show {
width: 100%;
}
\ No newline at end of file
@extends('dashboard.layout.base')
@section('content')
<div class="row">
<div class="col-sm-12">
<h4 class="page-title">Galeria</h4>
<ol class="breadcrumb">
<li><a href="{{ route('dashboard') }}">Dashboard</a></li>
<li><a href="{{ route('gallery') }}">Galeria</a></li>
<li class="active">Nova imagem</li>
</ol>
</div>
</div>
<div class="col-xs-12">
<div class="card-box">
<h4 class="m-t-0 m-b-30 header-title"><b>Nova Imagem</b></h4>
<form role="form" method="POST" action="{{ action('GalleryController@store') }}" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-xs-12 col-md-3">
<label for="image-input">
<div class="form-group{{ $errors->has('thumbnail') ? ' has-error' : '' }}">
<label for="image-input">Upload da imagem</label>
<div class="image-placeholder outer-container cursor-pointer">
<img id="image-preview" class="img-rounded hidden" src="">
<div id="image-preview-empty">
<b><i class="fa fa-arrow-up fa-3x"></i></b>
</div>
</div>
</div>
</label>
<div class="form-group{{ $errors->has('image') ? ' has-error' : '' }}">
<input id="image-input" type="file" name="image" value="{{ old('image') }}" class="hidden" autofocus required>
@if ($errors->has('image'))
<span class="help-block">
<strong>{{ $errors->first('image') }}</strong>
</span>
@endif
</div>
</div>
<div class="col-xs-12 col-md-9">
<div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
<label for="title">Título da imagem</label>
<input id="title" type="text" class="form-control" name="title" value="{{ old('title') }}" autofocus required>
<div>
@if ($errors->has('title'))
<span class="help-block">
<strong>{{ $errors->first('title') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('description') ? ' has-error' : '' }}">
<label for="description">Descrição curta da imagem</label>
<input id="description" type="text" class="form-control" name="description" value="{{ old('description') }}" autofocus required>
<div>
@if ($errors->has('description'))
<span class="help-block">
<strong>{{ $errors->first('description') }}</strong>
</span>
@endif
</div>
</div>
</div>
<div class="col-xs-12">
<div class="form-group text-right m-b-0">
<button type="submit" class="btn btn-primary waves-effect waves-light" autofocus>Cadastrar</button>
<a href="{{ route('gallery') }}" class="btn btn-white waves-effect waves-light m-l-5">Cancelar</a>
</div>
</div>
</div>
</form>
</div>
</div>
@endsection
@section('bottomJs')
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#image-preview').attr('src', e.target.result);
$('#image-preview').removeClass('hidden');
$('#image-preview').next().addClass('hidden');
$('#image-preview').parent().css('border', 'none');
}
reader.readAsDataURL(input.files[0]);
}
}
$("#image-input").change(function(){
readURL(this);
});
</script>
@endsection
\ No newline at end of file
@extends('dashboard.layout.base')
@section('content')
<div class="row">
<div class="col-sm-12">
<h4 class="page-title">Galeria</h4>
<ol class="breadcrumb">
<li><a href="{{ route('dashboard') }}">Dashboard</a></li>
<li><a href="{{ route('gallery') }}">Galeria</a></li>
<li class="active">Editar {{ $image->title }}</li>
</ol>
</div>
</div>
<div class="col-xs-12">
<div class="card-box">
<h4 class="m-t-0 m-b-30 header-title"><b>Editar informações de {{ $image->title }}</b></h4>
<form role="form" method="POST" action="{{ action('GalleryController@update', $image->id) }}" enctype="multipart/form-data">
@method('PUT')
@csrf
<div class="row">
<div class="col-xs-12 col-md-3">
<div class="form-group{{ $errors->has('thumbnail') ? ' has-error' : '' }}">
<div class="image-placeholder outer-container cursor-pointer">
<img id="image-preview" class="image-rounded" src="{{ asset('storage/images/galeria/' . $image->path) }}"/>
</div>
</div>
</div>
<div class="col-xs-12 col-md-9">
<div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
<label for="title">Título da imagem</label>
<input id="title" type="text" class="form-control" value="{{ $image->title }}" name="title" autofocus required>
<div>
@if ($errors->has('title'))
<span class="help-block">
<strong>{{ $errors->first('title') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('description') ? ' has-error' : '' }}">
<label for="description">Descrição curta da imagem</label>
<input id="description" type="text" class="form-control" value="{{ $image->description }}" name="description" autofocus required>
<div>
@if ($errors->has('description'))
<span class="help-block">
<strong>{{ $errors->first('description') }}</strong>
</span>
@endif
</div>
</div>
</div>
<div class="col-xs-12">
<div class="form-group text-right m-b-0">
<button type="submit" class="btn btn-primary waves-effect waves-light" autofocus>Atualizar</button>
<a href="{{ route('gallery') }}" class="btn btn-white waves-effect waves-light m-l-5">Cancelar</a>
</div>
</div>
</div>
</form>
</div>
</div>
@endsection
@section('bottomJs')
@endsection
\ No newline at end of file
@extends('dashboard.layout.base')
@section('content')
<div class="row">
<div class="col-sm-12">
<div class="btn-group pull-right m-t-15">
<button type="button" class="btn btn-default dropdown-toggle waves-effect waves-light" data-toggle="dropdown" aria-expanded="false">Opções <span class="m-l-5"><i class="fa fa-cog"></i></span></button>
<ul class="dropdown-menu drop-menu-right" role="menu">
<li><a href="{{ route('upload') }}">Nova imagem</a></li>
</ul>
</div>
<h4 class="page-title">Galeria</h4>
<ol class="breadcrumb">
<li><a href="{{ route('dashboard') }}">Dashboard</a></li>
<li class="active">Galeria</li>
</ol>
</div>
</div>
<div class="row port m-b-20">
<div class="portfolioContainer">
@foreach ($images as $image)
<div class="col-sm-6 col-lg-3 col-md-4">
<div class="gal-detail thumb">
<a href="{{ route('show-image', $image->id) }}" class="image-popup" title="{{ $image->description }}">
<img src="{{ asset('storage/images/galeria/' . $image->path) }}" class="thumb-img thumb-gallery" alt="work-thumbnail">
</a>
<h4>{{ $image->title }}</h4>
</div>
</div>
@endforeach
</div>
</div> <!-- End row -->
@endsection
@section('bottomJs')
@endsection
\ No newline at end of file
@extends('dashboard.layout.base')
@section('content')
<div class="row">
<div class="col-sm-12">
<div class="btn-group pull-right m-t-15">
<button type="button" class="btn btn-default dropdown-toggle waves-effect waves-light" data-toggle="dropdown" aria-expanded="false">Opções <span class="m-l-5"><i class="fa fa-cog"></i></span></button>
<ul class="dropdown-menu drop-menu-right" role="menu">
<li><a href="{{ route('edit', $image->id) }}">Editar</a></li>
<form role="form" method="POST" action="{{ action('GalleryController@destroy', $image->id) }}" enctype="multipart/form-data">
@method('DELETE')
@csrf
<li class="dropdown-btn"><a>
<button class="options-dropdown-btn" type="submit">Delete</button>
</a></li>
</form>
</ul>
</div>
<h4 class="page-title">Galeria</h4>
<ol class="breadcrumb">
<li><a href="{{ route('dashboard') }}">Dashboard</a></li>
<li><a href="{{ route('gallery') }}">Galeria</a></li>
<li class="active">{{$image->title}}</li>
</ol>
</div>
</div>
<div class="col-xs-12">
<div class="card-box">
<h4 class="m-t-0 m-b-10 header-title" align="center"><b>Título:</b> {{ $image->title }}</h4>
<p class="m-b-30"><b>Descrição: </b>{{ $image->description }}</p>
<img class="img-responsive img-show" src="{{ asset('storage/images/galeria/' . $image->path) }}"/>
</div>
</div>
@endsection
@section('bottomJs')
@endsection
\ No newline at end of file
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<!-- Logo container--> <!-- Logo container-->
<div class="logo"> <div class="logo">
<a href="index.html" class="logo"><span>RememberPET</span></a> <a href="{{ route('dashboard') }}" class="logo"><span>RememberPET</span></a>
</div> </div>
<!-- End Logo container--> <!-- End Logo container-->
...@@ -46,8 +46,10 @@ ...@@ -46,8 +46,10 @@
<a href="#"><i class="md md-dashboard"></i>Galeria</a> <a href="#"><i class="md md-dashboard"></i>Galeria</a>
<ul class="submenu"> <ul class="submenu">
<li> <li>
<a href="/dashboard">Lista de imagens</a> <a href="{{ route('gallery') }}">Lista de imagens</a>
<a href="/dashboard">Nova imagem</a> </li>
<li>
<a href="{{ route('upload') }}">Nova imagem</a>
</li> </li>
</ul> </ul>
</li> </li>
......
...@@ -21,7 +21,17 @@ Route::get('/', 'HomeController@index')->name('home'); ...@@ -21,7 +21,17 @@ Route::get('/', 'HomeController@index')->name('home');
Route::group(['prefix' => 'dashboard', 'middleware' => ['web', 'auth']], function () { Route::group(['prefix' => 'dashboard', 'middleware' => ['web', 'auth']], function () {
Route::get('/', function () { Route::get('/', function () {
return view('dashboard.home'); return view('dashboard.home');
})->name('dashboard-home'); })->name('dashboard');
Route::group(['prefix' => 'galeria'], function () {
Route::get('/', 'GalleryController@index')->name('gallery');
Route::get('/upload', 'GalleryController@create')->name('upload');
Route::post('/upload', 'GalleryController@store');
Route::get('/{id}', 'GalleryController@show')->name('show-image');
Route::get('/{id}/editar', 'GalleryController@edit')->name('edit');
Route::put('/{id}/editar', 'GalleryController@update');
Route::delete('/{id}/deletar', 'GalleryController@destroy')->name('delete-image');
});
}); });
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login'); Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment