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

65160087 #70

Open
wants to merge 2 commits 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
76 changes: 76 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Title;
use Illuminate\Support\Facades\Storage;

class UserController extends Controller
{//65160087 Kasama Soisuwan
public function index()
{
$users = User::with('title')->get();
return view('homepage',['users' => $users]);
}

public function viewAddPage(){//page add user
$titles = Title::all();
return view('addpage', compact('titles'));
}
public function addUser(Request $request){//add user
$validatedInput = $request->validate([
'title_id' => 'required',
'name'=> 'required|max:50',//ไม่เกิน50ตัวอักษร
'email' => 'required',
'password' => 'required|min:8',//อย่างน้อย 8 9ตัว
'avatar' => 'nullable',
]);
if ($request->hasFile('avatar')) {//check pic
$Avatar = $request->file('avatar');
$avatarFilePath = $Avatar->store('public/avatars');
$validatedInput['avatar'] = basename($avatarFilePath);
}

$validatedInput['password'] = bcrypt($validatedInput['password']);

$user = new User($validatedInput);
$user->save();

return redirect()->route('homepage');
}
public function deleteUser($id){//delete user
$user = User::findOrFail($id);
$user->delete();
return redirect()->route('homepage');
}
public function editUser($id) {//edit user
$user = User::findOrFail($id);
$titles = Title::all();

return view('editpage', compact('user', 'titles'));
}
public function updateUser(Request $request, $id){//uddate user
$validatedInput = $request->validate([
'title_id' => 'required',
'name'=> 'required|max:50',
'email' => 'required|unique:users,email,' . $id,
'avatar' => 'nullable',
]);

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

if ($request->hasFile('avatar')) {
$Avatar = $request->file('avatar');
$avatarFilePath = $Avatar->store('public/avatars');
$validatedInput['avatar'] = basename($avatarFilePath);
}

if ($request->isMethod('PUT')) {
$user->update($validatedInput);
return redirect()->route('homepage');
}

}
}
67 changes: 63 additions & 4 deletions resources/views/addpage.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,83 @@
</div>
<!-- /.card-header -->
<!-- form start -->
<form class="form-horizontal" action="{{ url('/') }}" method="post">
<!--add user-->
<form class="form-horizontal" action="{{ route('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="title_id" class="col-sm-2 col-form-label">Title</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="input01">
<select class="form-control" id="title_id" name="title_id">
<!--Select title-->
<option selected hidden>Titles</option>
@foreach ($titles as $title)
<option value="{{ $title->id }}">{{ $title->tit_name }}</option>
@endforeach
</select>
</div>
</div>
<div class="form-group row">
<!--Name-->
<label for="name" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name">
</div>
</div>
<div class="form-group row">
<!--Email-->
<label for="email" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email">
</div>
</div>
<div class="form-group row">
<!--Password-->
<label for="password" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password">
</div>
</div>
<div class="form-group row">
<!--Pic-->
<label for="avatar" class="col-sm-2 col-form-label">Avatar</label>
<div class="col-sm-10">
<div class="custom-file">
<!--show url-->
<input type="file" class="custom-file-input" id="avatar" name="avatar" onchange="showFileName()">
<label class="custom-file-label" for="avatar" id="avatar-label">Choose file</label>
</div>
@if ($errors->has('avatar'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('avatar') }}</strong>
</span>
@endif
<p id="avatar-url" style="display:none;"></p>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<!--submit-->
<button type="submit" class="btn btn-info">Submit</button>
<a href="{{ url('/') }}" class="btn btn-default float-right">Cancel</a>
<!--cancle-->
<a href="{{ route('homepage') }}" class="btn btn-default float-right">Cancel</a>
<!--reset-->
<button type="reset" class="btn btn-default float-right mr-2">Reset</button>
</div>
<!-- /.card-footer -->
</form>
</div>
<!-- /.card -->
<script>
//show url
function showFileName() {
var input = document.getElementById('avatar');
var label = document.getElementById('avatar-label');
var url = document.getElementById('avatar-url');
var file = input.files[0];
label.innerHTML = file.name;
url.style.display = 'block';
}
</script>
@endsection
56 changes: 53 additions & 3 deletions resources/views/editpage.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,52 @@
</div>
<!-- /.card-header -->
<!-- form start -->
<form class="form-horizontal" action="{{ url('/') }}" method="post">
<!--edit user-->
<form class="form-horizontal" action="{{ route('update.user', $user->id) }}" method="post" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="card-body">
<div class="form-group row">
<label for="input01" class="col-sm-2 col-form-label">Example Input</label>
<label for="title_id" class="col-sm-2 col-form-label">Title</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="input01">
<!--Select title-->
<select class="form-control" id="title_id" name="title_id">
@foreach ($titles as $title)
<option value="{{ $title->id }}">{{ $title->tit_name }}</option>
@endforeach
</select>
</div>
</div>
<div class="form-group row">
<!--Name-->
<label for="name" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name"
value="{{ $user->name }}">
</div>
</div>
<div class="form-group row">
<!--Email-->
<label for="email" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email"
value="{{ $user->email }}">
</div>
</div>
<div class="form-group row">
<!--Pic-->
<label for="avatar" class="col-sm-2 col-form-label">Avatar</label>
<div class="col-sm-10">
<div class="custom-file">
<input type="file" class="custom-file-input" id="avatar" name="avatar" onchange="showFileName()">
<label class="custom-file-label" for="avatar" id="avatar-label">Choose file</label>
</div>
@if ($errors->has('avatar'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('avatar') }}</strong>
</span>
@endif
<p id="avatar-url" style="display:none;"></p>
</div>
</div>
</div>
Expand All @@ -27,4 +66,15 @@
</form>
</div>
<!-- /.card -->
<script>
//show url
function showFileName() {
var input = document.getElementById('avatar');
var label = document.getElementById('avatar-label');
var url = document.getElementById('avatar-url');
var file = input.files[0];
label.innerHTML = file.name;
url.style.display = 'block';
}
</script>
@endsection
84 changes: 69 additions & 15 deletions resources/views/homepage.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,87 @@

@section('page_name', 'Users Data')
@section('content')
<!-- CODE HERE -->
<!--home-->
<div class="float-right pb-4">
<a href="{{ url('/add-user') }}" class="btn btn-success"> Add User </a>
</div>
<!--head table-->
<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 width="35px">#</th>
<th width="100px">Title</th>
<th>Name</th>
<th>Email</th>
<th width="150px">Avatar</th>
<th width="200px">Tools</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>name</td>
<td>email</td>
<td>avatar</td>
<td>Title</td>
<!--show user-->
@foreach ($users as $user)
<tr id="row_{{ $user->id }}">
<td>{{ $loop->iteration }}</td>
<td>{{ $user->title ? $user->title->tit_name : 'N/A' }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
<!--ถ้ามีแสดงรูป-->
@if ($user->avatar)
<img src="{{ asset('storage/avatars/' . $user->avatar) }}" width="100">
<!--ถ้าไม่มีรูป-->
@else
<span>ไม่มีรูป</span>
@endif
</td>
<td>
<a href="{{ url('/edit-user') }}" class="btn btn-warning">Edit</a>
<button class="btn btn-danger">Delete</button>
<!--edit-->
<a href="{{ route('edit.user', $user->id) }}" class="btn btn-warning">Edit</a>
<!--delete-->
<form action="{{ route('delete.user', $user->id) }}" method="post" class="d-inline">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection

<!--Sweet Alert -->
{{-- @push('scripts')
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
<script>
function confirmDelete(userId) {
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
url: '/delete-user/' + userId,
type: 'DELETE',
data: {
_token: '{{ csrf_token() }}'
},
success: function(response) {
$('#row_' + userId).remove();
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
)
},
});
}
});
}
</script>
@endpush --}}
34 changes: 25 additions & 9 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
<?php

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

Route::get('/', function () {
return view('homepage');
});
Route::get('/', [UserController::class, 'index'])->name('homepage');
Route::get('/add-user',[App\Http\Controllers\UserController::class, 'viewAddPage']);
Route::post('/add-user', [App\Http\Controllers\UserController::class, 'addUser'])->name('add.user');
Route::delete('/delete-user/{id}', [UserController::class, 'deleteUser'])->name('delete.user');
Route::get('/edit-user/{id}', [UserController::class, 'editUser'])->name('edit.user');
Route::put('/update-user/{id}', [Usercontroller::class, 'updateUser'])->name('update.user');


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

// Route::get('/add-user', function(){
// $titles = App\Models\Title::all();
// return view('/addpage', compact('titles'));
// });

// Route::get('/', function () {
// return view('homepage');
// });

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

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

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