Skip to content

Instantly share code, notes, and snippets.

@rudwolf
Created July 6, 2017 20:02
Show Gist options
  • Save rudwolf/90907fec2c7195661122c205f3d2cad8 to your computer and use it in GitHub Desktop.
Save rudwolf/90907fec2c7195661122c205f3d2cad8 to your computer and use it in GitHub Desktop.
Request and Controllers
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Contracts\Validation\Validator;
class AbstractRequest extends Request
{
protected $actionsToValidate = ['store', 'update'];
public function authorize()
{
return true;
}
public function rules()
{
if ( ( $this->isMethod('post') or $this->isMethod('put') ) and $this->checkAction())
return $this->rules;
///dd($this->rules);
//dd($this->rules);
return[];
}
public function messages()
{
$defaultMessages = [
'required' => 'O campo :attribute não pode ficar vazio',
'numeric' => 'O campo :attribute deve conter apenas números',
'min' => 'O campo :attribute deve conter no mínimo :min caractéres',
'max' => 'O campo :attribute deve conter no máximo :max caractéres'
];
$messages = $defaultMessages;
if (!empty($this->messages))
$messages = array_merge($defaultMessages, $this->messages);
if ( ( $this->isMethod('post') or $this->isMethod('put') ) and $this->checkAction())
return $messages;
return[];
}
protected function checkAction()
{
if (empty($this->route()->getAction()['as']))
return false;
$base = explode('.',$this->route()->getAction()['as']);
return in_array(end($base), $this->actionsToValidate);
}
protected function formatErrors(Validator $validator)
{
$result = $validator->errors()->all();
return $result;
}
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest
{
//
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use App\User;
use Auth;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use App\Company;
use Session;
class UserController extends Controller
{
public function __construct(UserRequest $request)
{
//$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::with('companies')->get();
return view('users.index')->with('users', $users);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$roles = Role::get();
$companies = Company::get();
return view('users.create', ['roles'=>$roles,'companies'=>$companies]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd($validator);
/*if ($validator->fails()) {
return redirect()
->route('admin.user.create')
->withErrors($validator)
->withInput();
}*/
/*$this->validate($request, [
'name'=>'required|max:120',
'email'=>'required|email|unique:users',
'password'=>'required|min:6|confirmed'
]);*/
$user = User::create($request->only('email', 'name', 'password'));
$id = $user->id;
$roles = $request['roles'];
if (isset($roles)) {
foreach ($roles as $role) {
$role_r = Role::where('id', '=', $role)->firstOrFail();
$user->assignRole($role_r);
}
}
$companies = $request['companies'];
foreach ($companies as $k => $CompanyId) {
$UserCompanyItem = new UserCompany;
$UserCompanyItem->user_id = $id;
$UserCompanyItem->company_id = $CompanyId;
$UserCompanyItem->save();
}
return redirect()->route('admin.user.index')
->with('flash_message',
'Usuário adicionado');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//return redirect('users');
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$user = User::findOrFail($id);
$roles = Role::get();
return view('users.edit', compact('user', 'roles'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$user = User::findOrFail($id);
$this->validate($request, [
'name'=>'required|max:120',
'email'=>'required|email|unique:users,email,'.$id,
'password'=>'required|min:6|confirmed'
]);
$input = $request->only(['name', 'email', 'password']);
$roles = $request['roles'];
$user->fill($input)->save();
if (isset($roles)) {
$user->roles()->sync($roles);
}
else {
$user->roles()->detach();
}
return redirect()->route('users.index')
->with('flash_message',
'User successfully edited.');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$user = User::findOrFail($id);
$user->delete();
return redirect()->route('users.index')
->with('flash_message',
'User successfully deleted.');
}
}
<?php
namespace App\Http\Requests;
use App\Http\Requests\AbstractRequest;
class UserRequest extends AbstractRequest
{
/* protected $rules = [
'usuarioId' => 'required',
'usuarioNome' => 'required',
'usuarioLogin' => 'required',
'dominioId' => 'required',
'usuarioSexo' => 'required',
];*/
protected $rules = [
'name'=>'required|max:120',
'email'=>'required|email|unique:users',
'password'=>'required|min:6|confirmed'
];
/*protected $rules = [
'name'=>'required',
'email'=>'required',
'password'=>'required'
];*/
protected $messages = [
'email.required' => 'Digite um e-mail',
'password.confirmed' => 'Digite a mesma senha na confirmação'
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment