Last active
May 31, 2016 16:12
-
-
Save TimDorand/541f19b85489735eebfb5aec07139a02 to your computer and use it in GitHub Desktop.
Controller that upload pictures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\HomePost; | |
use App\User; | |
use Illuminate\Http\Request; | |
use App\Http\Requests; | |
use Illuminate\Support\Facades\Auth; | |
class HomePostController extends Controller | |
{ | |
public function __construct() | |
{ | |
// Admin à tous les droits, il peut tout voir | |
$this->middleware('admin')->except('index'); | |
} | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index() | |
{ | |
$posts = HomePost::all(); | |
return view('posts.index')->with(compact('posts')); | |
} | |
/** | |
* Show the form for creating a new resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function create() | |
{ | |
$users = User::all()->lists('name', 'id'); | |
return view('posts.create')->with(compact('users')); | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request) | |
{ | |
$post = new HomePost; | |
$imageName = $post->id . '.' . $request->file('image')->getClientOriginalExtension(); | |
$post->user_id = Auth::user()->id; | |
$post->title = $request->title; | |
$post->style = $request->style; | |
$post->text = $request->text; | |
$post->image = $imageName; | |
$post->save(); | |
$imageName = $post->id . '.' . $request->file('image')->getClientOriginalExtension(); | |
$request->file('image')->move( | |
base_path() . '/public/images/catalog/', $imageName | |
); | |
return redirect() | |
->route('posts.show', $post->id) | |
->with(compact('post')) | |
->with('message','Libellé ajouté !'); | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function show($id) | |
{ | |
$posts = HomePost::all(); | |
return view('posts.index')->with(compact('posts')); | |
} | |
/** | |
* Show the form for editing the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function edit($id) | |
{ | |
$post = HomePost::find($id); | |
$users = User::all()->lists('name', 'id') ; | |
$imageName = '/images/catalog/'.$post->id.$post->image; | |
return view('posts.edit')->with(compact('post', 'users', 'imageName')); | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function update(Request $request, $id) | |
{ | |
$post = HomePost::find($id); | |
$imageName = '.' . $request->file('image')->getClientOriginalExtension(); | |
$post->title = $request->title; | |
$post->style = $request->style; | |
$post->text = $request->text; | |
$post->image = $imageName; | |
$post->save(); | |
$imageName = $post->id . '.' . $request->file('image')->getClientOriginalExtension(); | |
$request->file('image')->move( | |
base_path() . '/public/images/catalog/', $imageName | |
); | |
return redirect()->route('posts.index', $post->id); | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function destroy($id) | |
{ | |
$post = HomePost::find($id); | |
$post->delete(); | |
$imageName = $post->id.$post->image; | |
$id->file('image')->delete( | |
base_path() . '/public/images/catalog/', $imageName | |
); | |
return redirect()->route('posts.index'); | |
} | |
} | |
------------ VIEW ---------- | |
<div class="container"> | |
@if (Auth::check() && Auth::user()->admin == 2) | |
@if($action == 'edit') | |
<h1>Editer une section à la page d'accueil</h1> | |
{!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT', 'files' => 'true']) !!} | |
@else | |
<h1>Ajouter une section à la page d'accueil</h1> | |
{!! Form::open(['route' => 'posts.store', 'method' => 'POST', 'files' => 'true']) !!} | |
@endif | |
<div class="form-group"> | |
{!! Form::label('Titre') !!} | |
{!! Form::text('title', null, [ | |
'class' => 'form-control', | |
'placeholder' => 'Nom de l\'article' | |
]) !!} | |
</div> | |
<div class="form-group"> | |
<label>Style du libellé</label> | |
{{--Select des différents type de projets--}} | |
{!! Form::select('style', array( | |
'image_droite' => 'Image à Droite', | |
'image_gauche' =>'Image à Gauche', | |
'image_fullwidth' =>'Image pleine largeur' | |
),[ | |
'class' => 'form-control', 'style'=>'display:inline;']) | |
!!} | |
<br/> | |
</div> | |
<div class="form-group"> | |
@if($action == 'edit') | |
<img src="{{url('/').$imageName}}" height="100"> Votre image actuelle<br/> | |
{!! Form::label('Votre image') !!} | |
{!! Form::file('image' , null) !!} | |
@else | |
{!! Form::label('Votre image') !!} | |
{!! Form::file('image', null) !!} | |
@endif | |
</div> | |
<div class="form-group"> | |
{!! Form::label('Contenu') !!} | |
{!! Form::textarea('text', null, ['class' => 'form-control', 'placeholder'=>'Contenu']) !!} | |
</div> | |
{!! Form::submit('Envoyer', ['class' => 'btn btn-block']) !!} | |
{!! Form::close() !!} | |
</div> | |
@else | |
@include('partials.posts.401') | |
@endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment