From 3c1212d2e9442a3a564ba1ae997feed2cd1b4a58 Mon Sep 17 00:00:00 2001 From: myuqinurrohman Date: Tue, 25 Mar 2025 10:34:23 +0700 Subject: [PATCH] chore: display news data feat: filter news data --- .../Dashboard/DatatableController.php | 40 +++- .../Controllers/Dashboard/NewsController.php | 27 ++- app/Http/Requests/NewsRequest.php | 7 +- app/Models/Legacy/OldNews.php | 15 ++ app/Models/News.php | 2 + .../2025_02_27_030638_create_news_table.php | 4 +- database/seeders/DatabaseSeeder.php | 1 + database/seeders/NewsSeeder.php | 23 +- .../views/dashboard/news/create.blade.php | 11 +- resources/views/dashboard/news/edit.blade.php | 15 +- .../views/dashboard/news/index.blade.php | 67 ++++-- resources/views/dashboard/news/show.blade.php | 199 ++++++++++-------- .../views/partials/app/datatable.blade.php | 28 ++- .../views/partials/dashboard/footer.blade.php | 2 + routes/web.php | 1 + 15 files changed, 311 insertions(+), 131 deletions(-) create mode 100644 app/Models/Legacy/OldNews.php diff --git a/app/Http/Controllers/Dashboard/DatatableController.php b/app/Http/Controllers/Dashboard/DatatableController.php index a159831..0ed67d5 100644 --- a/app/Http/Controllers/Dashboard/DatatableController.php +++ b/app/Http/Controllers/Dashboard/DatatableController.php @@ -26,6 +26,7 @@ use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use SebastianBergmann\Type\TrueType; use Yajra\DataTables\DataTables; @@ -409,12 +410,43 @@ public function governmentAgency(Request $request){ public function news(Request $request){ if ($request->ajax()) { - $data = News::all(); + + + $category = $request->input('category', 'all'); + $year = $request->input('year', null); + $month = $request->input('month', null); + $date = $request->input('date', null); + + $query = News::query(); + + if($category !== 'all'){ + if($category === 'news'){ + $query->where('category', '0'); + }else if($category === 'announcement'){ + $query->where('category', '1'); + } + } + + if ($year) { + $query->whereYear('created_at', $year); + } + + if ($month) { + $monthYear = Carbon::parse($month); + $query->whereYear('created_at', $monthYear->year) + ->whereMonth('created_at', $monthYear->month); + } + + if ($date) { + $query->whereDate('created_at', $date); + } + + $data = $query->get(); return DataTables::of($data) ->addIndexColumn() ->addColumn('title', function ($data) { - return shorten_text($data->title, 50); + return $data->title; }) ->addColumn('link', function ($data) { if ($data->link != null) { @@ -424,10 +456,10 @@ public function news(Request $request){ } }) ->addColumn('category', function ($data) { - if ($data->category == '0') { + if ($data->category === '0') { return 'Berita'; }else{ - return 'Pengumuman'; + return 'Pengumuman'; } }) ->addColumn('action', function ($data) { diff --git a/app/Http/Controllers/Dashboard/NewsController.php b/app/Http/Controllers/Dashboard/NewsController.php index 75a5df2..31de6cb 100644 --- a/app/Http/Controllers/Dashboard/NewsController.php +++ b/app/Http/Controllers/Dashboard/NewsController.php @@ -11,10 +11,15 @@ class NewsController extends Controller { - public function index(){ + public function index(Request $request){ return view('dashboard.news.index', [ - 'title' => 'Berita' + 'title' => 'Berita', + 'category' => $request->input('category', null), + 'year' => $request->input('year', null), + 'month' => $request->input('month', null), + 'date' => $request->input('date', null), ]); + } public function create(){ @@ -25,15 +30,18 @@ public function create(){ public function store(NewsRequest $request){ $validatedData = $request->validated(); - $validatedData['image'] = store_file($validatedData['image'], '/uploads/news', 'public')['randomFileName']; $validatedData['slug'] = Str::slug($validatedData['title']); $validatedData['author_id'] = Auth::id(); + if($request->has('image')){ + $validatedData['image'] = store_file($validatedData['image'], '/uploads/news', 'public')['randomFileName']; + } + $createdNews = News::create($validatedData); if($createdNews){ - return redirect()->back()->with('success-status', 'Berhasil menambahkan berita.'); + return redirect()->route('dashboard.news.index')->with('success-status', 'Berhasil menambahkan berita.'); }else{ return redirect()->back()->with('failed-status', 'Gagal menambahkan berita.'); } @@ -94,4 +102,15 @@ public function destroy($id){ return response()->json(['message' => 'Gagal menghapus berita.'], 500); } } + + public function filter(Request $request){ + + $filteredRequest = $request->except('_token'); + + $filterField = array_filter($filteredRequest, function ($value) { + return filled($value); + }); + + return redirect()->route('dashboard.news.index', $filterField); + } } diff --git a/app/Http/Requests/NewsRequest.php b/app/Http/Requests/NewsRequest.php index 484a127..713edfa 100644 --- a/app/Http/Requests/NewsRequest.php +++ b/app/Http/Requests/NewsRequest.php @@ -28,14 +28,9 @@ public function rules(): array 'link' => ['nullable', 'url'], 'tag' => ['nullable'], 'category' => ['required', 'in:0,1'], + 'image' => 'nullable', 'image', 'mimes:png,jpg,jpeg', 'max:4096' ]; - if($this->isMethod('post')){ - $rules['image'] = ['required', 'image', 'mimes:png,jpg,jpeg', 'max:4096']; - }else if($this->isMethod('put')){ - $rules['image'] = ['nullable', 'image', 'mimes:png,jpg,jpeg', 'max:4096']; - } - return $rules; } diff --git a/app/Models/Legacy/OldNews.php b/app/Models/Legacy/OldNews.php new file mode 100644 index 0000000..ca95e0c --- /dev/null +++ b/app/Models/Legacy/OldNews.php @@ -0,0 +1,15 @@ +string('title'); $table->string('slug')->unique(); $table->text('content'); - $table->string('image'); + $table->string('image')->nullable(); $table->string('link')->nullable(); $table->string('tag')->nullable(); $table->enum('category', ['0', '1'])->default('0'); + $table->integer('views')->default(0); $table->unsignedBigInteger('author_id'); $table->foreign('author_id')->references('id')->on('users'); $table->timestamps(); + $table->softDeletes(); }); } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index aa05bd6..182fcb0 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -20,6 +20,7 @@ public function run(): void $this->call(SubClassificationSeeder::class); $this->call(LocationSeeder::class); $this->call(SubLocationSeeder::class); + $this->call(NewsSeeder::class); $this->call(CompanySeeder::class); $this->call(MediaSeeder::class); $this->call(JournalistSeeder::class); diff --git a/database/seeders/NewsSeeder.php b/database/seeders/NewsSeeder.php index 20a174f..b9c3ec7 100644 --- a/database/seeders/NewsSeeder.php +++ b/database/seeders/NewsSeeder.php @@ -2,8 +2,11 @@ namespace Database\Seeders; +use App\Models\Legacy\OldNews; +use App\Models\News; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; +use Illuminate\Support\Str; class NewsSeeder extends Seeder { @@ -12,6 +15,24 @@ class NewsSeeder extends Seeder */ public function run(): void { - // + $legacyNews = OldNews::all(); + + foreach($legacyNews as $item){ + News::create([ + 'id' => $item->id, + 'title' => $item->title, + 'slug' => Str::slug($item->title), + 'content' => $item->content, + 'category' => $item->category, + 'link' => $item->link, + 'image' => $item->image, + 'tag' => $item->tag, + 'created_at' => $item->created_at, + 'updated_at' => $item->updated_at, + 'deleted_at' => $item->deleted_at, + 'author_id' => $item->enhancer, + ]); + + } } } diff --git a/resources/views/dashboard/news/create.blade.php b/resources/views/dashboard/news/create.blade.php index fa1780d..0edebcf 100644 --- a/resources/views/dashboard/news/create.blade.php +++ b/resources/views/dashboard/news/create.blade.php @@ -12,13 +12,10 @@
{{$title}}
- @foreach ($errors->all() as $item) - {{$item}} - @endforeach -
+ @csrf
-
+
@@ -64,7 +61,7 @@
- +
@@ -85,7 +82,7 @@
Kembali - +
diff --git a/resources/views/dashboard/news/edit.blade.php b/resources/views/dashboard/news/edit.blade.php index 8b27e94..4620abe 100644 --- a/resources/views/dashboard/news/edit.blade.php +++ b/resources/views/dashboard/news/edit.blade.php @@ -12,14 +12,11 @@
{{$title}}
- @foreach ($errors->all() as $item) - {{$item}} - @endforeach - + @csrf @method('put')
-
+
@@ -65,7 +62,7 @@
- +
@@ -85,8 +82,12 @@

+ @if (request()->query('show')) + Kembali + @else Kembali - + @endif +
diff --git a/resources/views/dashboard/news/index.blade.php b/resources/views/dashboard/news/index.blade.php index 4671b2d..c5a6213 100644 --- a/resources/views/dashboard/news/index.blade.php +++ b/resources/views/dashboard/news/index.blade.php @@ -16,18 +16,11 @@
@@ -42,12 +35,12 @@ - + - +
NoNo Judul Link Kategori Tag
@@ -55,6 +48,56 @@
+
diff --git a/resources/views/dashboard/news/show.blade.php b/resources/views/dashboard/news/show.blade.php index 59af91d..7c6d493 100644 --- a/resources/views/dashboard/news/show.blade.php +++ b/resources/views/dashboard/news/show.blade.php @@ -7,96 +7,119 @@
-
-
-
-
{{$title}}
-
- @foreach ($errors->all() as $item) - {{$item}} - @endforeach -
- @csrf - @method('put') -
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- -
- -
-
-
-
-
- - - @error('image') - {{$message}} - @enderror -
-
-
-
- -
-
{!! $news->content !!}
-
-
-
-
-
-
- Kembali - Ubah -
-
-
-@include('partials.dashboard.footer', ['rich_editor' => true]) +@include('partials.dashboard.footer', ['rich_editor' => false]) diff --git a/resources/views/partials/app/datatable.blade.php b/resources/views/partials/app/datatable.blade.php index 04b367b..ef32676 100644 --- a/resources/views/partials/app/datatable.blade.php +++ b/resources/views/partials/app/datatable.blade.php @@ -254,7 +254,16 @@ $('#newsTable').DataTable({ processing: true, serverSide: true, - ajax: '{{ route('dashboard.news.data') }}', + ajax: { + url: '{{ route('dashboard.news.data') }}', + type: 'GET', + data: function(d) { + d.category = '{{ $category }}'; + d.year = '{{ $year }}'; + d.month = '{{ $month }}'; + d.date = '{{ $date }}'; + } + }, columns: [ { data: 'DT_RowIndex', name: 'DT_RowIndex', orderable: false, searchable: false }, { data: 'title', name: 'title' }, @@ -265,6 +274,23 @@ { data: 'created_at', name: 'created_at', visible: false }, { data: 'id', name: 'id', orderable: true, searchable: false, visible: false }, ], + columnDefs: [ + { targets: 0, width: 'auto' }, + // { targets: 2, width: '10%' }, + { targets: 3, width: '10%' }, + { targets: 4, width: '12%' }, + { targets: 5, width: '7%' }, + { + targets: [1,2], + createdCell: function(td, cellData, rowData, row, col) { + $(td).css({ + 'word-wrap': 'break-word', + 'white-space': 'normal', + 'overflow-wrap': 'break-word' + }); + } + } + ], language: datatableLanguage, autoWidth: false, order: [[6, 'desc']], diff --git a/resources/views/partials/dashboard/footer.blade.php b/resources/views/partials/dashboard/footer.blade.php index 17e76fc..e0a520d 100644 --- a/resources/views/partials/dashboard/footer.blade.php +++ b/resources/views/partials/dashboard/footer.blade.php @@ -101,6 +101,8 @@ @include('partials.app.sweetalert') +{{-- {{dd(request()->all())}} --}} + @if (isset($page)) @if ($page === 'create_issue_management') diff --git a/routes/web.php b/routes/web.php index 1fbdf6f..6eb1edc 100644 --- a/routes/web.php +++ b/routes/web.php @@ -147,6 +147,7 @@ Route::prefix('news')->name('news.')->group(function(){ Route::controller(NewsController::class)->group(function(){ Route::get('/', 'index')->name('index'); + Route::post('/filter', 'filter')->name('filter'); Route::get('/{id}/show', 'show')->name('show'); Route::middleware('role:1')->group(function(){