Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
User Resource Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use App\User;
class UserController extends Controller
{
/**
* Display a listing of the user.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request): View
{
$users = User::paginate();
return view('user.index', \compact('users'));
}
/**
* Show the form for creating a new user.
*
* @return \Illuminate\Http\Response
*/
public function create(): View
{
return view('user.create');
}
/**
* Store a newly created user in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request): RedirectResponse
{
$this->validate($request, [
'name' => 'required|string',
'email' => 'required|email|unique:users,email',
'password' => 'required|confirmed|min:6',
]);
$user = User::create($request->all());
return redirect()
->route('user.index')
->with('user', $user);
}
/**
* Display the specified user.
*
* @param User $user
* @return \Illuminate\Http\Response
*/
public function show(User $user): Show
{
return view('user.show', $user);
}
/**
* Show the form for editing the specified user.
*
* @param User $user
* @return \Illuminate\Http\Response
*/
public function edit(User $user): View
{
return view('user.edit', $user);
}
/**
* Update the specified user in storage.
*
* @param \Illuminate\Http\Request $request
* @param User $user
* @return \Illuminate\Http\Response
*/
public function update(Request $request, User $user): RedirectResponse
{
$this->request([
'name' => 'required|string'
]);
$user->fill($request->only(['name']));
$user->save();
return redirect()
->route('user.index')
->with('user', $user);
}
/**
* Remove the specified user from storage.
*
* @param User $user
* @return \Illuminate\Http\Response
*/
public function destroy(User $user): RedirectResponse
{
$user->delete();
return redirect()
->route('user.index')
->with('user', $user);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.