Skip to content
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

65160234 #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
82 changes: 82 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
{
public function index()
{
$items = User::all();
return view('homepage', compact('items'));
}

public function create()
{
return view('addpage');
}

public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|min:6',
'avatar' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);

if ($request->hasFile('avatar')) {
$avatarName = time() . '.' . $request->avatar->extension();
$request->avatar->move(public_path('avatars'), $avatarName);
}

User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
'avatar' => isset($avatarName) ? $avatarName : null,
]);

return redirect('/')->with('success', 'เพิ่มข้อมูลผู้ใช้เรียบร้อยแล้ว');
}

public function edit($id)
{
$user = User::find($id);
return view('editpage', compact('user'));
}

public function destroy($id)
{
$user = User::find($id);
$user->delete();

return redirect('/')->with('success', 'ลบข้อมูลผู้ใช้เรียบร้อยแล้ว');
}

public function update(Request $request, $id)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email,' . $id,
'avatar' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);

$user = User::find($id);

if ($request->hasFile('avatar')) {
$avatarName = time() . '.' . $request->avatar->extension();
$request->avatar->move(public_path('avatars'), $avatarName);
$user->avatar = $avatarName;
}

$user->name = $request->name;
$user->email = $request->email;
$user->save();

return redirect('/')->with('success', 'อัปเดตข้อมูลผู้ใช้เรียบร้อยแล้ว');
}

}
Binary file added public/avatars/1711892879.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/avatars/1711898923.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/avatars/1711898979.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/avatars/1711900715.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 24 additions & 10 deletions resources/views/addpage.blade.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
@extends('layouts.default')

@section('page_name', 'Add Users Data')
@section('page_name', 'Add User Form')
@section('content')
<div class="card card-info">
<div class="card-header">
<h3 class="card-title">Add User Form</h3>
<h3 class="card-title">Add User Data</h3>
</div>
<!-- /.card-header -->
<!-- form start -->
<form class="form-horizontal" action="{{ url('/') }}" method="post">
<form class="form-horizontal" action="{{ url('/add-user') }}" method="post" enctype="multipart/form-data">
@csrf
<div class="card-body">
<div class="form-group row">
<label for="input01" class="col-sm-2 col-form-label">Example Input</label>
<label for="inputName" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="input01">
<input type="text" class="form-control" id="inputName" name="name" placeholder="Name">
</div>
</div>
<div class="form-group row">
<label for="inputEmail" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="email" autocomplete="off">
</div>
</div>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword" name="password" placeholder="password" autocomplete="off">
</div>
</div>
<div class="form-group row">
<label for="inputAvatar" class="col-sm-2 col-form-label">Avatar</label>
<div class="col-sm-10">
<input type="file" class="form-control-file" id="inputAvatar" name="avatar">
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-info">Submit</button>
<a href="{{ url('/') }}" class="btn btn-default float-right">Cancel</a>
<button type="reset" class="btn btn-default float-right mr-2">Reset</button>
</div>
<!-- /.card-footer -->

</form>
</div>
<!-- /.card -->
@endsection
73 changes: 63 additions & 10 deletions resources/views/editpage.blade.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,83 @@
@extends('layouts.default')

@section('page_name', 'Edit Users Data')
@section('page_name', 'Edit User Data')

@section('content')

<div class="card card-info">

<div class="card-header">
<h3 class="card-title">Edit User Form</h3>

<h3 class="card-title">Edit User Data</h3>

</div>
<!-- /.card-header -->
<!-- form start -->
<form class="form-horizontal" action="{{ url('/') }}" method="post">

<form class="form-horizontal" action="{{ route('users.update', $user->id) }}" method="post" enctype="multipart/form-data">
@csrf

<div class="card-body">

<div class="form-group row">

<label for="inputName" class="col-sm-2 col-form-label">Name</label>

<div class="col-sm-10">

<input type="text" class="form-control" id="inputName" name="name" value="{{ $user->name }}">

</div>

</div>

<div class="form-group row">
<label for="input01" class="col-sm-2 col-form-label">Example Input</label>

<label for="inputEmail" class="col-sm-2 col-form-label">Email</label>

<div class="col-sm-10">
<input type="text" class="form-control" id="input01">

<input type="email" class="form-control" id="inputEmail" name="email" value="{{ $user->email }}" autocomplete="off">

</div>

</div>

<div class="form-group row">

<label for="inputPassword" class="col-sm-2 col-form-label">Password</label>

<div class="col-sm-10">

<input type="password" class="form-control" id="inputPassword" name="password" placeholder="รหัสผ่าน">

</div>

</div>

<div class="form-group row">

<label for="inputAvatar" class="col-sm-2 col-form-label">Avatar</label>

<div class="col-sm-10">

<input type="file" class="form-control-file" id="inputAvatar" name="avatar">

</div>

</div>

</div>
<!-- /.card-body -->

<div class="card-footer">

<button type="submit" class="btn btn-info">Submit</button>

<a href="{{ url('/') }}" class="btn btn-default float-right">Cancel</a>
<button type="reset" class="btn btn-default float-right mr-2">Reset</button>

</div>
<!-- /.card-footer -->

</form>

</div>
<!-- /.card -->

@endsection
39 changes: 24 additions & 15 deletions resources/views/homepage.blade.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
@extends('layouts.default')

@section('page_name', 'Users Data')

@section('content')
<!-- CODE HERE -->

<div class="float-right pb-4">
<a href="{{ url('/add-user') }}" class="btn btn-success"> Add User </a>
<a href="{{ url('/add-user') }}" class="btn btn-success">Add User</a>
</div>

<table class="table table-bordered">
<thead>
<tr>
<td width="35px">#</td>
<td>name</td>
<td>email</td>
<td>avatar</td>
<td>Title</td>
<td width="150px">Tools</td>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Avatar</th>
<th scope="col" width="150px">Tools</th>
</tr>
</thead>
<tbody>
@foreach($items as $item)
<tr>
<td>1</td>
<td>name</td>
<td>email</td>
<td>avatar</td>
<td>Title</td>
<th scope="row">{{ $item->id }}</th>
<td>{{ $item->name }}</td>
<td>{{ $item->email }}</td>
<td>
<a href="{{ url('/edit-user') }}" class="btn btn-warning">Edit</a>
<button class="btn btn-danger">Delete</button>
@if($item->avatar)
<img src="{{ asset('storage/'.$item->avatar) }}" alt="Avatar" style="max-width: 100px; max-height: 100px;">
@else
<img src="{{ asset('images/default-avatar.jpg') }}" alt="Default Avatar" style="max-width: 100px; max-height: 100px;">
@endif
</td>
<td>
<a href="{{ url('/edit-user/'.$item->id) }}" class="btn btn-warning">Edit</a>
<a href="{{ url('/delete-user/'.$item->id) }}" class="btn btn-danger">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>

@endsection
22 changes: 13 additions & 9 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::get('/', function () {
return view('homepage');
});
Route::post('/update-user/{id}', [UserController::class, 'update'])->name('users.update');

Route::get('/add-user', function () {
return view('addpage');
});

Route::get('/edit-user', function () {
return view('editpage');
});
Route::get('/', [UserController::class, 'index']);

Route::get('/add-user', [UserController::class, 'create']);

Route::post('/add-user', [UserController::class, 'store']);

Route::get('/edit-user/{id}', [UserController::class, 'edit']);

Route::post('/update.user/{id}', [UserController::class, 'update']);

Route::get('/delete-user/{id}', [UserController::class, 'destroy']);