diff --git a/app/Http/Controllers/StockLogController.php b/app/Http/Controllers/StockLogController.php index 74e9289..c1d41c7 100644 --- a/app/Http/Controllers/StockLogController.php +++ b/app/Http/Controllers/StockLogController.php @@ -9,7 +9,18 @@ use Illuminate\Http\Request; class StockLogController extends Controller { - public function Input(Request $request, $id) + public function index(Request $request) + { + $perPage = (int) $request->input('per_page', 20); // 페이지에 표시 할 목록의 수 + $title = '입출고 이력'; + // Eager Loading 으로 N + 1 문제 해결 + $logs = StockLog::with(['product:id,name,sku']) + ->latestFirst() + ->paginate($perPage); + return view('stock_log.list',compact('title','logs')); + } + + public function input(Request $request, $id) { // queryString action 의 default value 는 in 으로 한다. $action = $request->query('action','in'); diff --git a/app/Models/StockLog.php b/app/Models/StockLog.php index 23125db..cf38307 100644 --- a/app/Models/StockLog.php +++ b/app/Models/StockLog.php @@ -8,7 +8,20 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; class StockLog extends Model { protected $table = 'stock_logs'; + protected $guarded = []; + + protected $casts = [ + 'change_amount' => 'integer', + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + ]; + + public function scopeLatestFirst($qurey) + { + return $qurey->orderByDesc('created_at')->orderByDesc('id'); + } + public function product(): BelongsTo { return $this->belongsTo(Product::class); diff --git a/resources/views/inc/topbar.blade.php b/resources/views/inc/topbar.blade.php index d43ff82..7e6a7ca 100644 --- a/resources/views/inc/topbar.blade.php +++ b/resources/views/inc/topbar.blade.php @@ -14,7 +14,7 @@