for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Xetaravel\Observers;
use Illuminate\Support\Facades\Auth;
use Xetaravel\Models\DiscussPost;
use Xetaravel\Models\Repositories\DiscussPostRepository;
class DiscussPostObserver
{
/**
* Handle the "creating" event.
*/
public function creating(DiscussPost $discussPost): void
if (is_null($discussPost->user_id)) {
is_null($discussPost->user_id)
false
$discussPost->user_id = Auth::id();
}
* Handle the "deleting" event.
public function deleting(DiscussPost $discussPost): void
$conversation = $discussPost->conversation;
if ($conversation->first_post_id == $discussPost->getKey()) {
$conversation->delete();
if ($conversation->last_post_id == $discussPost->getKey()) {
$previousPost = DiscussPostRepository::findPreviousPost($discussPost, true);
$conversation->last_post_id = !is_null($previousPost) ? $previousPost->getKey() : null;
if ($conversation->solved_post_id == $discussPost->getKey()) {
$conversation->solved_post_id = null;
$conversation->is_solved = false;
$conversation->save();