Skip to content

create CRUD, search employee#1

Open
thiennguyen84 wants to merge 7 commits intomasterfrom
feature/buildEmployee
Open

create CRUD, search employee#1
thiennguyen84 wants to merge 7 commits intomasterfrom
feature/buildEmployee

Conversation

@thiennguyen84
Copy link
Owner

No description provided.

//
public function show(){
$employees = DB::table('employees')->orderBy('name')->paginate(10);
return view('index',['employees'=>$employees]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nên dùng Eloquent String thay vì Query Builder https://laravel.com/docs/5.5/eloquent tuy QB có tốc độ truy vấn nhanh hơn nhưng khi câu query dài, phức tạp thì ES sẽ dễ hiểu hơn

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TruongPhan16, @thiennguyen84 ngoài ra thì dùng Eloquent Engine thì dữ liệu trả về mới là các Model Object đã extend từ Eloquent, lúc đó mới sử dụng được các method định nghĩa trong model và các method hỗ trợ bởi Eloquent

],
[
'email.unique'=>'Email đã có người sử dụng',
]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TruongPhan16 e làm cái controller mới r nhé.@@

$employee->name = $request->name;
$employee->address = $request->address;
$employee->birth_day = $request->birthday;
$employee->email = $request->email;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

birthday là 1 từ

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

birth day e để kiểu date nên k nhập được kí tự

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tức là birthday là 1 từ thôi chứ k phải birth_day

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TruongPhan16 @@.

use Validator;
use File;

class EmployeeController extends Controller

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nên dùng controller resource cho CRUD https://laravel.com/docs/5.5/controllers#resource-controllers

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TruongPhan16 đang training nên cần giải thích thêm tại sao ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chungth e làm cái controller mới r nhé.@@

}

public function delete($id){
$employee = Employee::find($id);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sử dụng findOrFail vì khi không tìm thấy sẽ trả về trang 404

$searchs = Employee::where('name','like','%'.$name.'%')->orderBy('name')->paginate(10);
return view('search',['searchs'=>$searchs,'name'=>$name]);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function này có thể gộp chung vào index

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TruongPhan16 tách như thế này có thể refactoring để search bằng ajax ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chungth thế là e tách ra như này có phải gộp lại k a. :v

</div>
@endif
<div class="col-lg-6 col-lg-offset-3">
<form action = "{{route('add')}}" method="POST" enctype="multipart/form-data">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thiếu space giữa {{}}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TruongPhan16 thiếu space là sao a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thiennguyen84 tức là thiếu dấu cách ấy

</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name= "email" value="@gmail.com" required="">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value="@gmail.com" là như thế nào nhỉ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TruongPhan16 e cho sẵn value là @gmail.com để chỉ cần nhập phần tên đầu thôi

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thế hotmail hay các mail khác thì sao?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e đặt value vậy thôi chứ người ta có thể xóa value nhập hotmail khác mà a.:v

</ul>
</div>
@endif
<div class="row">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nên hiện error vào input tương ứng thay vì hiện tất cả

{{ csrf_field() }}
<div class="col-lg-6">
<div class="avata_name">
Avata

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class sai convention avata-name

@endif
<div class="row">
<form action = "{{route('edit',$employee->id)}}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sử dụng method PUT hoặc PATCH khi update cho đúng chuẩn RESTful

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TruongPhan16 mà nếu đặt PUT PATH thì để form up ảnh riêng à anh

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{ method_field('PUT') }}
thêm cái này vào anh ơi. Mà không phải tạo form up ảnh riêng đâu anh.

@foreach ($employees as $value)
<?php $dem++; ?>
<tr>
<td>{{$dem}}</td>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Nên dùng biến tiếng anh thay vì tiếng việt
  • Không cần đoạn <?php $dem++; ?> cho $dem++td đầu tiên là được
  • Blade template có hỗ trợ @php @endphp thay cho thẻ đóng mở php

<td>{{$dem}}</td>
<td>{{$value->name}}</td>
<td><img src="http://localhost:8000/img/{{$value->image}}" width="40" height="35px" alt=""></td>
<td>{{$value->email}}</td>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

</tbody>
</table>
<div class="row">{{$employees->links()}}</div>
</div>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Như này thì phần phân trang sẽ không ra chính giữa đúng k nhỉ

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TruongPhan16 Quy định là ra giữa a???

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thiennguyen84 ra giữa cho dễ nhìn thôi

<td>{{$value->address}}</td>
<td><div class="edit"><a href="{{route('edit',$value->id)}}" class="btn btn-info"><span class="ti-pencil-alt"></span></a></div></td>
<td><form action="/task/{{ $value->id }}" method="POST">
{{ csrf_field() }}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

truyền route name vào action thay vì hard code

routes/web.php Outdated
Route::get('search',[
'as'=>'search',
'uses'=>'EmployeeController@search'
]); No newline at end of file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tất cả route trên có thể thay bằng Route::resource cho ngắn gọn

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TruongPhan16 thế là phải làm lại route à a.@@

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thiennguyen84 cách của em ko sai nhưng @TruongPhan16 có giới thiệu cái Route::resource để sau em định nghĩa nó sẽ ngắn gọn và dễ hơn, em đọc trong laravel doc nhé

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chungth ok a

Copy link

@chungth chungth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Các file trong folder

public/img là file được người dùng upload lên ?

Những file đó thuộc về dữ liệu của app nên cần ignore khỏi git repo

@endif
<div class="row">
<form action = "{{route('edit',$employee->id)}}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{ method_field('PUT') }}
thêm cái này vào anh ơi. Mà không phải tạo form up ảnh riêng đâu anh.

<div class="container">
<div class="navbar-header">
<img src="asset/images/hapo.jpg" class="hapo_img" alt="">
<img src="http://localhost:8000/asset/images/hapo.jpg" class="hapo_img" alt="">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tại sao chỗ này dùng link http: vậy anh :@@

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anhht-hapo a chạy serve nếu k sử dụng http://localhost:8000 thì n không nhận link css.@@

<div class="row">
@if(Session::has('thanhcong'))
<div class="alert alert-success thanh-cong">{{Session::get('thanhcong')}}</div>
<div class="alert alert-success thanh-cong">{{Session::get('thanhcong')}}</div>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sao tên class tiếng việt không dấu vậy anh

{
//
$employee = new Employee();
if($request->hasFile('avata')){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avatar chứ ko phải avata đau a

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ căng

return redirect('employs')->with('thanhcong','Xóa nhân viên thành công');
}

public function search(Request $request){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convention dòng này em ơi! {} 2 dấu đóng mở này phải nằm trên 1 dong trắng

@@ -13,12 +13,12 @@ class EmployeeController extends Controller
{
//
public function show(){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

như trên

return view('admin.employee.index',['employees'=>$employees]);
}

public function add(){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

như trên

$employee->birthday = $request->birthday;
$employee->email = $request->email;
$employee->save();
return redirect('/show')->with('thanhcong','Thêm nhân viên thành công');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thiennguyen84 dùng route('name') đi em.ko nên dùng đường dẫn url nữa

$employee->email = $request->email;
$employee->save();
return redirect('/employs')->with('thanhcong','Thêm nhân viên thành công');
return redirect()->route('employs.index')->with('thanhcong','Thêm nhân viên thành công');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thiennguyen84 cho message này vào file lang em ơi. Đọc doc ở đây em nhé https://laravel.com/docs/5.5/localization
các chỗ tương tự em tự tìm rồi tự cho vào nhé, anh ko comment nữa nhé
(nguyên xi dòng anh chung review của anh. em xem của anh tham khảo nha)

@thiennguyen84 thiennguyen84 force-pushed the feature/buildEmployee branch from ded0b26 to 4fbfc41 Compare July 3, 2018 06:44
@thiennguyen84 thiennguyen84 force-pushed the feature/buildEmployee branch from 4fbfc41 to 5fb7bf1 Compare July 3, 2018 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Comments