pingcrm/app/Http/Controllers/UsersController.php

92 lines
2.6 KiB
PHP
Raw Normal View History

2019-03-18 08:53:00 -03:00
<?php
namespace App\Http\Controllers;
use App\User;
use Inertia\Inertia;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
class UsersController extends Controller
{
public function index()
{
return Inertia::render('Users/Index', [
'filters' => Request::all('search', 'role', 'trashed'),
'users' => Auth::user()->account->users()
->orderByName()
->filter(Request::only('search', 'role', 'trashed'))
->get()
->transform(function ($user) {
return [
'id' => $user->id,
'name' => $user->name,
'email' => $user->email,
'owner' => $user->owner,
'deleted_at' => $user->deleted_at,
];
}),
]);
}
public function create()
{
return Inertia::render('Users/Create');
}
public function store()
{
return Auth::user()->account->users()->create(
Request::validate([
'first_name' => ['required', 'max:50'],
'last_name' => ['required', 'max:50'],
'email' => ['required', 'max:50', 'email', Rule::unique('users')],
'password' => ['nullable'],
'owner' => ['required', 'boolean'],
])
)->only('id');
}
public function edit(User $user)
{
return Inertia::render('Users/Edit', [
'user' => [
'id' => $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
'owner' => $user->owner,
'deleted_at' => $user->deleted_at,
],
]);
}
public function update(User $user)
{
Request::validate([
'first_name' => ['required', 'max:50'],
'last_name' => ['required', 'max:50'],
'email' => ['required', 'max:50', 'email', Rule::unique('users')->ignore($user->id)],
'password' => ['nullable'],
'owner' => ['required', 'boolean'],
]);
$user->update(Request::only('first_name', 'last_name', 'email', 'owner'));
if (Request::get('password')) {
$user->update(['password' => Request::get('password')]);
}
}
public function destroy(User $user)
{
$user->delete();
}
public function restore(User $user)
{
$user->restore();
}
}