get(); return view('bo.articles.index', compact('a')); } public function create() { $ac = ArticlesCategories::all(); return view('bo.articles.create', compact('ac')); } public function store(Request $request) { $request->validate([ 'title' => 'required|max:100', 'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=340,height=340', 'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=780,height=508', 'content' => 'required', ]); $title = $request->title; $slug = Str::slug($title); $picture = $request->picture; $picture_r = $request->picture_r; $content = $request->content; $categoryId = $request->category; $status = 0; $notification = $request->notification === null ? 0 : 1; if ($categoryId === "4") { $question = $request->question; $multipleResponse = $request->multiple_response === null ? 0 : 1; $stopPoll = $request->stop_poll === null ? 0 : 1; $answers = $request->answers; $request->validate([ 'question' => 'required|max:255', 'answers' => 'required', ]); } $imageName = Str::slug($title) .'-le-gaulois-' . date('d-m-Y') . '.' . $picture->extension(); $picture->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m')), $imageName); $imageName_r = Str::slug($title) .'-le-gaulois-' . date('d-m-Y') . '_r.' . $picture_r->extension(); $picture_r->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m')), $imageName_r); $a = new Articles(); $a->title = $title; $a->slug = $slug; $a->picture = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName; $a->picture_r = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName_r; $a->content = $content; $a->category_id = $categoryId; if ($categoryId === "4") { $a->question = $question; $a->multiple_response = $multipleResponse; $a->stop_poll = $stopPoll; } $a->status = $status; $a->notification = $notification; $a->save(); if ($categoryId === "4") { foreach ($answers as $answer) { if (!empty($answer)) { $a->answers()->create([ 'answer' => $answer, ]); } } } return redirect()->route('bo.articles.index'); } public function edit($id) { $a = Articles::findOrFail($id); $ac = ArticlesCategories::all(); return view('bo.articles.edit') ->with('a', $a) ->with('ac', $ac) ; } public function update(Request $request, $id) { $request->validate([ 'title' => 'required|max:100', 'content' => 'required', ]); $title = $request->title; $slug = Str::slug($title); $picture = $request->picture; $picture_r = $request->picture_r; $content = $request->content; $categoryId = $request->category; $status = $request->status === null ? 0 : 1; $notification = $request->notification === null ? 0 : 1; if ($categoryId === "4") { $question = $request->question; $multipleResponse = $request->multiple_response === null ? 0 : 1; $stopPoll = $request->stop_poll === null ? 0 : 1; $answers = $request->answers; $request->validate([ 'question' => 'required|max:255', 'answers' => 'required', ]); } if ($picture !== NULL && $picture_r !== NULL){ $request->validate([ 'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=340,height=340', 'picture_r' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=780,height=508', ]); }elseif ($picture !== NULL && $picture_r === NULL){ $request->validate([ 'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=340,height=340' ]); }elseif ($picture === NULL && $picture_r !== NULL){ $request->validate([ 'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=780,height=508' ]); } $a = Articles::findOrFail($id); if ($picture !== NULL){ if(file_exists(public_path() . '/' . $a->picture)) unlink(public_path() . '/' . $a->picture); $imageName = Str::slug($title) .'.'.$picture->extension(); $picture->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m') . '/'), $imageName); } if ($picture_r !== NULL) { if(file_exists(public_path() . '/' . $a->picture_r)) unlink(public_path() . '/' . $a->picture_r); $imageName_r = Str::slug($title) . '_r.' . $picture_r->extension(); $picture_r->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m') . '/'), $imageName_r); } $a->title = $title; $a->slug = $slug; if ($picture !== null) $a->picture = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName; if ($picture_r !== null) $a->picture_r = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName_r; $a->content = $content; $a->category_id = $categoryId; if ($categoryId === "4") { $a->question = $question; $a->multiple_response = $multipleResponse; $a->stop_poll = $stopPoll; $a->answers()->delete(); foreach ($answers as $answer) { if (!empty($answer)) { $a->answers()->create([ 'answer' => $answer, ]); } } } else { $a->question = null; $a->multiple_response = null; $a->stop_poll = null; $a->answers()->delete(); } $a->status = $status; $a->notification = $notification; $a->save(); if ($a->category_id == 1 && $a->status === 1) { $actus = Articles::where([['status', 1], ['category_id', 1]])->orderBy('updated_at', 'DESC')->limit(3)->get(); Cache::put('actusGlobal', $actus); } return redirect()->route('bo.articles.edit', $a->id)->with('success', 'Enregistré !'); } public function destroy($id) { $a = Articles::findOrFail($id); if (file_exists(public_path() . '/' . $a->picture)) unlink(public_path() . '/' . $a->picture); if (file_exists(public_path() . '/' . $a->picture_r)) unlink(public_path() . '/' . $a->picture_r); $a->delete(); return redirect()->route('bo.articles.index'); } }