♻️ 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

@ -14,16 +14,16 @@ class Post extends Model
protected $fillable = [ protected $fillable = [
'content', 'image', 'date_post', 'user_id' 'content', 'image', 'date_post', 'user_id'
]; ];
/** /**
* The attributes that should be hidden for arrays. * The attributes that should be hidden for arrays.
* *
* @var array * @var array
*/ */
protected $hidden = [ protected $hidden = [
]; ];
/** /**
* The attributes that should be cast to native types. * The attributes that should be cast to native types.
* *
@ -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();
}
}
} }