Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions app/Http/Controllers/AuthorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Validation\ValidationException;

class AuthorController extends Controller
{
Expand Down Expand Up @@ -37,11 +40,29 @@ public function create()
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
User::create($request->post());
return redirect()->route('authors.index')->with('message', 'Author created successfully');
public function store(Request $request)
{
try {
// Validate the incoming request data
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|unique:users|max:255',
'password' => 'required|string|min:8',
]);

// Hash the password for security before storing
$validatedData['password'] = bcrypt($validatedData['password']);

// Create the user with the validated and sanitized data
User::create($validatedData);

return redirect()->route('authors.index')->with('success', 'Author created successfully.');

} catch (ValidationException $e) {
// Redirect back with validation errors if validation fails
return redirect()->back()->withErrors($e->errors())->withInput();
}
}

/**
* Show the form for editing the specified resource.
Expand Down