♻️ Factorise la suppression de tags

Pour #8
This commit is contained in:
Clement Desmidt 2020-03-05 17:42:20 +01:00
parent d2d666da3b
commit d83d92ec42
2 changed files with 17 additions and 8 deletions

View File

@ -75,10 +75,7 @@ class HomeController extends Controller
'message' => 'required' 'message' => 'required'
]); ]);
$tags_to_delete = PostsTag::where('post_id', $today_post->id)->get(); $today_post->deleteTags();
foreach ($tags_to_delete as $tag_to_delete) {
$tag_to_delete->delete();
}
$tags = $tag_detector->detectFrom($data['message']); $tags = $tag_detector->detectFrom($data['message']);
@ -112,7 +109,8 @@ class HomeController extends Controller
$today_post = Post::whereDate('date_post', '=', (new DateTime())->format('Y-m-d')) $today_post = Post::whereDate('date_post', '=', (new DateTime())->format('Y-m-d'))
->where('user_id', Auth::user()->getAuthIdentifier()) ->where('user_id', Auth::user()->getAuthIdentifier())
->firstOrFail(); ->firstOrFail();
/** @var Post $today_post */
$today_post->deleteTags();
$today_post->delete(); $today_post->delete();
return Redirect::to('home')->withSuccess('Great! Your today\'s post is now deleted. You can make a new one!'); return Redirect::to('home')->withSuccess('Great! Your today\'s post is now deleted. You can make a new one!');
} catch (\ErrorException $e) { } catch (\ErrorException $e) {

View File

@ -32,4 +32,15 @@ class Post extends Model
protected $casts = [ protected $casts = [
'date_post' => 'datetime', 'date_post' => 'datetime',
]; ];
/**
* Delete this posts tags
*/
public function deleteTags()
{
$tags_to_delete = PostsTag::where('post_id', $this->id)->get();
foreach ($tags_to_delete as $tag_to_delete) {
$tag_to_delete->delete();
}
}
} }