-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#11 - users crud operations #20
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace Interns2024c\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\RedirectResponse; | ||
use Inertia\Inertia; | ||
use Inertia\Response; | ||
use Interns2024c\Models\User; | ||
|
||
class UserController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the users. | ||
* | ||
* @return Response | ||
*/ | ||
public function index(): Response | ||
{ | ||
$users = User::all(); | ||
return Inertia::render('User/Index', ['users' => $users]); | ||
} | ||
|
||
/** | ||
* Show the form for creating a new user. | ||
* | ||
* @return Response | ||
*/ | ||
public function create(): Response | ||
{ | ||
return Inertia::render('User/Create'); | ||
} | ||
|
||
/** | ||
* Store a newly created user in storage. | ||
* | ||
* @param Request $request | ||
* @return RedirectResponse | ||
*/ | ||
public function store(Request $request): RedirectResponse | ||
{ | ||
$request->validate([ | ||
'name' => 'required|string|max:255', | ||
'email' => 'required|email|unique:users,email', | ||
'password' => 'required|string|min:8', | ||
]); | ||
|
||
User::create([ | ||
'name' => $request->input('name'), | ||
'email' => $request->input('email'), | ||
'password' => bcrypt($request->input('password')), | ||
]); | ||
|
||
return redirect()->route('users.index')->with('success', 'User created successfully.'); | ||
} | ||
|
||
/** | ||
* Show the form for editing the specified user. | ||
* | ||
* @param User $user | ||
* @return Response | ||
*/ | ||
public function edit(User $user) | ||
{ | ||
return Inertia::render('User/Edit', ['user' => $user]); | ||
} | ||
|
||
/** | ||
* Update the specified user in storage. | ||
* | ||
* @param Request $request | ||
* @param User $user | ||
* @return RedirectResponse | ||
*/ | ||
public function update(Request $request, User $user): RedirectResponse | ||
{ | ||
$validated = $request->validate([ | ||
"name" => "required|string|max:255", | ||
"email' => 'required|email|unique:users,email", | ||
]); | ||
|
||
$user->update($validated); | ||
|
||
return redirect()->route('users.index')->with('success', 'User updated successfully.'); | ||
} | ||
|
||
|
||
/** | ||
* Remove the specified user from storage. | ||
* | ||
* @param User $user | ||
* @return RedirectResponse | ||
*/ | ||
public function destroy(User $user): RedirectResponse | ||
{ | ||
$user->delete(); | ||
|
||
return redirect()->back()->with('success', 'User deleted successfully.'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,25 +20,38 @@ | |
*/ | ||
class User extends Authenticatable | ||
{ | ||
use HasApiTokens; | ||
use HasFactory; | ||
use Notifiable; | ||
use HasApiTokens, HasFactory, Notifiable; | ||
|
||
public mixed $id; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this? |
||
protected $table = 'users'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this? |
||
|
||
protected $fillable = [ | ||
"name", | ||
"email", | ||
"password", | ||
'name', | ||
'email', | ||
'password', | ||
]; | ||
|
||
protected $hidden = [ | ||
"password", | ||
"remember_token", | ||
'password', | ||
'remember_token', | ||
]; | ||
|
||
protected function casts(): array | ||
protected $casts = [ | ||
'email_verified_at' => 'datetime', | ||
'password' => 'hashed', | ||
]; | ||
|
||
/** | ||
* Custom create method to handle user creation | ||
* | ||
* @param array $attributes | ||
* @return static | ||
*/ | ||
public static function create(array $attributes): static | ||
{ | ||
return [ | ||
"email_verified_at" => "datetime", | ||
"password" => "hashed", | ||
]; | ||
$attributes['password'] = bcrypt($attributes['password']); | ||
return static::query()->create($attributes); | ||
} | ||
|
||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
import blumilkDefault from '@blumilksoftware/eslint-config' | ||
import globals from "globals"; | ||
import tseslint from "typescript-eslint"; | ||
import pluginVue from "eslint-plugin-vue"; | ||
|
||
|
||
/** @type {import('eslint').Linter.Config[]} */ | ||
export default [ | ||
...blumilkDefault, | ||
] | ||
{files: ["**/*.{js,mjs,cjs,ts,vue}"]}, | ||
{languageOptions: { globals: globals.browser }}, | ||
...tseslint.configs.recommended, | ||
...pluginVue.configs["flat/essential"], | ||
{files: ["**/*.vue"], languageOptions: {parserOptions: {parser: tseslint.parser}}}, | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These comments don't add value to the code and could be removed to improve readability.