orderBy chapter number & chpater number unique while on the same manga

This commit is contained in:
Htet Phone Aung 2023-09-09 19:44:15 +06:30
parent 0ef892df4d
commit b601f09cb2
5 changed files with 28 additions and 11 deletions

View File

@ -37,19 +37,25 @@ class ChapterController extends Controller
$formData = $request->validated(); $formData = $request->validated();
$formData['user_id'] = Auth::id(); $formData['user_id'] = Auth::id();
if($request->hasFile('images')) { if ($request->hasFile('images')) {
foreach ($request->file('images') as $key => $image) { foreach ($request->file('images') as $key => $image) {
$images[] = $image->store($request->manga_id.'ChapterImages', 'public'); $images[] = $image->store($request->manga_id . 'ChapterImages', 'public');
} }
$formData['images'] = $images; $formData['images'] = $images;
} }
//chapter number
$manga = Manga::where('id', $request->manga_id)->first(); if ($request->chapter_no) {
$formData['chapter_no'] = $manga->chapters->count() + 1; $formData['chapter_no'] = $request->chapter_no;
} else {
//chapter number
$manga = Manga::where('id', $request->manga_id)->first();
$latestChap = $manga->chapters()->latest()->first()->chapter_no;
$formData['chapter_no'] = $latestChap + 1;
}
Chapter::create($formData); Chapter::create($formData);
return redirect()->route('home')->with(['message' => 'New Chapter has been added!!']); return redirect()->route('home')->with(['message' => 'New Chapter has been added!!']);
} }
/** /**
@ -91,4 +97,3 @@ class ChapterController extends Controller
return view('chapter.manageChapter', compact('manga')); return view('chapter.manageChapter', compact('manga'));
} }
} }

View File

@ -2,6 +2,8 @@
namespace App\Http\Requests; namespace App\Http\Requests;
use Illuminate\Validation\Rule;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
class StoreChapterRequest extends FormRequest class StoreChapterRequest extends FormRequest
@ -21,11 +23,14 @@ class StoreChapterRequest extends FormRequest
*/ */
public function rules(): array public function rules(): array
{ {
// dd(request()->manga_id);
$id = request()->manga_id;
return [ return [
'title' => 'required|min:3', 'title' => 'required|min:3',
'chapter_no' => ['nullable', 'numeric', Rule::unique('chapters', 'chapter_no')->where(fn (Builder $query) => $query->where('manga_id', $id)) ],
'manga_id' => 'required|exists:mangas,id', 'manga_id' => 'required|exists:mangas,id',
'images' => 'required', 'images' => 'required',
'images.*' => 'file|mimes:png,jpg' 'images.*' => 'file|mimes:png,jpg',
]; ];
} }

View File

@ -24,9 +24,16 @@
<p class="text-danger small">{{ $message }}</p> <p class="text-danger small">{{ $message }}</p>
@enderror @enderror
</div> </div>
<div class="mb-3">
<label for="">Chapter Number <span class="text-danger small">*Optional</span> </label>
<input type="number" name="chapter_no" class="form-control">
@error('chapter_no')
<p class="text-danger small">{{ $message }}</p>
@enderror
</div>
<div class="mb-3"> <div class="mb-3">
<label for="">Chapter Title</label> <label for="">Chapter Title</label>
<input type="text" name="title" class="form-control"> <input type="text" name="title" class="form-control" value="{{old('title')}}">
@error('title') @error('title')
<p class="text-danger small">{{ $message }}</p> <p class="text-danger small">{{ $message }}</p>
@enderror @enderror

View File

@ -12,7 +12,7 @@
</div> </div>
<div class="row"> <div class="row">
@foreach ($manga->chapters()->latest('id')->paginate(20) as $chapter) @foreach ($manga->chapters()->orderBy('chapter_no', 'desc')->paginate(20) as $chapter)
<div class="col-2"> <div class="col-2">
<div class="d-flex p-2 border border-1 border-dark rounded align-items-center justify-content-between mb-3"> <div class="d-flex p-2 border border-1 border-dark rounded align-items-center justify-content-between mb-3">
<p class="small mb-0"> Chapter {{ $chapter->chapter_no }} </p> <p class="small mb-0"> Chapter {{ $chapter->chapter_no }} </p>

View File

@ -10,7 +10,7 @@
Latest Chapters Latest Chapters
</h5> </h5>
@forelse ($manga->chapters()->latest('id')->get() as $chapter) @forelse ($manga->chapters()->orderBy('chapter_no', 'desc')->get() as $chapter)
<a href="{{ route('page.chapter', [$manga->slug, $chapter->chapter_no]) }}" <a href="{{ route('page.chapter', [$manga->slug, $chapter->chapter_no]) }}"
class="list-group-item list-group-item-action pb-0 border-0 border-bottom class="list-group-item list-group-item-action pb-0 border-0 border-bottom
d-inline-block d-flex align-items-center justify-content-between"> d-inline-block d-flex align-items-center justify-content-between">