Skip to content

Instantly share code, notes, and snippets.

@betinho37
Created June 11, 2018 17:35
Show Gist options
  • Save betinho37/7083ae0f5266b809246f3d9c0f67a380 to your computer and use it in GitHub Desktop.
Save betinho37/7083ae0f5266b809246f3d9c0f67a380 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Publicacao;
use App\User;
use Illuminate\Support\Facades\DB;
use Storage;
use File;
class PublicacaoController extends Controller
{
private $publicacao, $user;
public function __construct(Publicacao $publicacao, User $user)
{
$this->publicacao = $publicacao;
$this->user = $user;
}
public function index()
{
$publicacao = DB::table('publicacoes')->paginate(4);
//Ordernar Datas
return view('publicacoes.index', compact('publicacao'));
}
public function show()
{
$publicacao = Publicacao::find($id);
return view('publicacoes.carrousel', compact('publicacao'));
}
public function create()
{
$publicacao = Publicacao::get();
$list_user = $this->user->listUser();
return view('publicacoes.create', compact('publicacao','list_user'));
}
public function store(Request $request)
{
if ($file = \Input::file('arquivo')){
$destinationPath = public_path().DIRECTORY_SEPARATOR.'images';
$extension = $file->getClientOriginalExtension();
$arquivo['arquivo'] = str_random (30).'.'.$extension;
$name['arquivo'] = $file->getClientOriginalName();
$inputs = $request->all();
$file->move( $destinationPath, $name['arquivo'], $arquivo['arquivo'] );
$inputs['arquivo'] = $name['arquivo'];
$this->publicacao->create($inputs);
return redirect()->action('PublicacaoController@index');
}
$inputs = $request->all();
$this->publicacao->create($inputs);
return redirect()->action('PublicacaoController@index');
}
public function edit($id)
{
$list_user = $this->user->listUser();
$publicacao = Publicacao::find($id);
return view('publicacoes.edit', compact('publicacao','list_user'));
}
public function update(Request $request, $id)
{
$publicacao = Publicacao::find($id);
//var_dump(\Input::file('arquivo')); die();
if ($file = \Input::file('arquivo'))
{
$destinationPath = public_path().DIRECTORY_SEPARATOR.'images';
$extension = $file->getClientOriginalExtension();
$arquivo['arquivo'] = str_random (30).'.'.$extension;
$name['arquivo'] = $file->getClientOriginalName();
$file->move( $destinationPath, $name['arquivo'] );
$publicacao->arquivo = $name['arquivo'];
$publicacao->save();
}
$publicacao->situacao = $request->situacao;
$publicacao->save();
return redirect()->action('PublicacaoController@controle');
}
public function controle()
{
$publicacao = auth()->user->publicacao ;
return view('publicacoes.controle', compact('publicacao'));
}
public function deletfile($name)
{
$publicacao = Publicacao::where('arquivo', $name)->first();
$publicacao->arquivo = null;
unlink(public_path('images/'. $name));
$publicacao->save();
return redirect()->back();
}
public function download($id)
{
$publicacao = Publicacao::find($id);
$filename = $publicacao->arquivo;
return response()->download(public_path("images/{$filename}"));
}
public function destroy($id)
{
$publicacao = Publicacao::find($id);
if ($publicacao->arquivo) {
unlink(public_path('images/' . $publicacao->arquivo));
$publicacao->delete();
}
$publicacao->delete();
return redirect()->back();
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\User;
use App\Estado;
use App\Relacao;
class UsuarioController extends Controller
{
private $usuario, $estado, $relacao;
public function __construct(User $usuario, Estado $estado, Relacao $relacao)
{
$this->usuario = $usuario;
$this->estado = $estado;
$this->relacao = $relacao;
}
public function index()
{
$usuario = $this->usuario->all();
return view('admin.index', compact('usuario'));
}
public function create()
{
$list_estado = $this->estado->listEstado();
$list_relacao = $this->relacao->listRelacao();
return view('admin.create', compact('list_estado', 'list_relacao'));
}
public function edit($id)
{
$list_estado = $this->estado->listEstado();
$list_relacao = $this->relacao->listRelacao();
$usuario = User::find($id);
return view('admin.edit', compact('usuario', 'list_estado', 'list_relacao'));
}
public function store(Request $request)
{
$inputs = $request->all();
$validator = $this->validator($inputs);
if ($validator->fails()) {
return redirect('usuario/create')
->withErrors($validator)
->withInput();
}
$inputs['password'] = bcrypt($inputs['password']);
$this->usuario->create($inputs);
return redirect()->action('PaginaInicialController@index');
}
public function update(Request $request, $id)
{
$usuario = User::find($id);
$usuario->fill($request->all());
if ($usuario['password'] != null)
$usuario['password'] = bcrypt($usuario['password']);
else
unset($usuario['password']);
$usuario->save();
return redirect()->action('UsuarioController@index');
}
public function destroy($id)
{
$usuario = User::find($id);
$usuario->delete();
return redirect()->back();
}
protected function validator(array $data)
{
return Validator::make($data, [
'password' => 'required|string|min:6|confirmed',
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment