add next and prev button in show blade with and update show() method in newscontroller, some css

This commit is contained in:
Nicolas Demangeat 2024-03-15 09:59:40 +01:00
parent 6707463e2d
commit 04777c331e
5 changed files with 37 additions and 25 deletions

View file

@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
use App\Models\News;
use App\Models\Section;
use App\Models\StaticPage;
use Illuminate\Database\Eloquent\Builder;
class NewsController extends Controller
{
@ -19,7 +20,7 @@ class NewsController extends Controller
{
$staticPageModel = StaticPage::query()->where('type', StaticPageType::we_move)->first();
$sectionModels = Section::query()->where('page_id', $staticPageModel->id)->get();
$newsModels = News::query()->where('published', true)->orderBy('order')->simplePaginate(4);
$newsModels = News::query()->where('published', true)->orderBy('publish_at', 'DESC')->simplePaginate(2);
return view('fo.pages.news.index', \compact('staticPageModel', 'sectionModels', 'newsModels'));
}
@ -27,25 +28,25 @@ class NewsController extends Controller
/**
* Display a news articles
*
* @param string $slug
* @param \App\Models\News $news
* @return \Illuminate\Contracts\View\View
*/
public function show(News $news): \Illuminate\Contracts\View\View
{
/** @var \App\Models\News $nextNews */
if ($news->order === News::query()->where('published', true)->max('order')) {
$nextNews = News::query()
->where('published', true)
->orderBy('order')
->firstOrFail();
} else {
$nextNews = News::query()
->where('order', '>', $news->order)
->where('published', true)
->orderBy('order')
->firstOrFail();
}
// Get only id from published news.
$publishedNews = News::query()->where('published', true)->orderBy('publish_at', 'DESC')->get()->pluck('id');
// Search the position of the current news id.
$positionId = \array_search($news->id, $publishedNews->toArray());
// Check if it's the last position.
$nextPositionId = ($positionId === count($publishedNews) - 1) ? 0 : intval($positionId) + 1 ;
// Find the next news from the id.
$nextNews = News::query()->where('id', $publishedNews[$nextPositionId])->firstOrFail();
return view('fo.pages.news.show', \compact('news', 'nextNews'));
// Check if it's the first position.
$prevPositionId = ($positionId === 0) ? count($publishedNews) - 1 : intval($positionId) - 1 ;
// Find the next news from the id.
$prevNews = News::query()->where('id', $publishedNews[$prevPositionId])->firstOrFail();
return view('fo.pages.news.show', \compact('news', 'nextNews', 'prevNews'));
}
}

View file

@ -45,7 +45,6 @@ class RouteServiceProvider extends ServiceProvider
Route::model('user', User::class);
Route::model('strip', Strip::class);
Route::model('news', News::class);
}
/**

View file

@ -15,6 +15,10 @@
.sub-card{
background-color: color.adjust($color-sixt, $lightness: + 35%);
}
.rotate-arrow {
transform: rotate(180deg);
}
}
// LLLLLLLLLLL GGGGGGGGGGGGG

View file

@ -42,13 +42,21 @@
</div>
</div>
</div>
<div class="col-12 text-center mt-5">
<a class="btn rounded-5 fw-bold border border-2 border-primary"
href="{{ route('fo.news.show', $nextNews) }}"
title="{{ __('Voir l\'article') }} {{ $nextNews->title }}">
{{ __('Article suivant') }}
<img src="{{ Vite::asset('resources/assets/images/misc/blue-arrow.svg') }}" />
</a>
<div class="row justify-content-center container-fluid">
<div class="col-12 mt-5 d-flex justify-content-center">
<a class="btn text-center rounded-5 fw-bold border border-2 border-primary"
href="{{ route('fo.news.show', $prevNews) }}"
title="{{ __('Voir l\'article') }} {{ $prevNews->title }}">
<img class="rotate-arrow" src="{{ Vite::asset('resources/assets/images/misc/blue-arrow.svg') }}" />
{{ __('Article précédent') }}
</a>
<a class="btn text-center rounded-5 fw-bold border border-2 border-primary"
href="{{ route('fo.news.show', $nextNews) }}"
title="{{ __('Voir l\'article') }} {{ $nextNews->title }}">
{{ __('Article suivant') }}
<img src="{{ Vite::asset('resources/assets/images/misc/blue-arrow.svg') }}" />
</a>
</div>
</div>
</div>
</section>

View file

@ -22,7 +22,7 @@
</div>
{{-- FIXME: check wysiwyg str limit --}}
<div @class(['card-text', $speachClass => !empty($speachClass)])>
{!! Str::limit(clean($speach), 150, '...') !!}
{!! clean($speach) !!}
</div>
</div>
</div>