49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Homepage;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\News;
|
|
use Illuminate\Http\Request;
|
|
|
|
class NewsController extends Controller
|
|
{
|
|
public function index(Request $request){
|
|
|
|
$data = [
|
|
'title' => 'Berita',
|
|
'page' => 'news',
|
|
];
|
|
|
|
$search = $request->input('search', null);
|
|
|
|
if($search){
|
|
$data['news'] = News::where('title', 'LIKE', "%{$search}%")
|
|
// ->orWhere('content', 'LIKE', "%{$search}%")
|
|
->where('category', '0')
|
|
->latest()
|
|
->simplePaginate(12);
|
|
}else{
|
|
$data['news'] = News::where('category', '0')->latest()->simplePaginate(12);
|
|
}
|
|
|
|
return view('homepage.news.index', $data);
|
|
}
|
|
|
|
public function read($slug){
|
|
$news = News::where('slug', $slug)->first();
|
|
$news->increment('views', 1);
|
|
|
|
$data = [
|
|
'title' => 'Berita',
|
|
'page' => 'news',
|
|
'news' => $news,
|
|
'popularNews' => News::where('category', '0')->orderBy('views', 'desc')->take(5)->get(),
|
|
'otherNews' => News::where('category', '0')->orderByDesc('id')->where('id', '!=', $news->id)->take(3)->get()
|
|
];
|
|
|
|
return view('homepage.news.read', $data);
|
|
|
|
}
|
|
}
|