0

I can not delete a row using a simple eloquent query. Even when I am using eloquent can not get the data from DB. I am getting null. But, in DB query method at least I am getting data but can not delete then. Following is my code:

DB::transaction(function () use ($lead, $comment, $request) {
    $lead->save();
    $lead->comments()->save($comment);

    if ($request->deleteAppointment) {
        $calendarEvent = DB::table('calendar_events')->where('id', $request->appointmentId)->first(); // I am getting data here.
        $calendarEvent = CalendarEvent::find($request->appointmentId); // But, here I am getting null, don't know why!
        
        if ($calendarEvent != null) {
            $calendarEvent->delete();
        }
    } 

My goal is to get the data using Eloquent and then Delete from database.

update: My Database Table enter image description here

CalendarEvent.php model

class CalendarEvent extends Model
{
    use SoftDeletes;

    /**
     * @var array
     */
    protected $casts = [
        'event_begin' => 'datetime',
        'event_end'   => 'datetime',
        'options'     => 'array',
    ];

    /**
     * @var array
     */
    protected $guarded = [
        'id',
    ];

    /**
     * @return mixed
     */
    public function users()
    {
        return $this->morphedByMany(User::class, 'eventable');
    }

    /**
     * @return mixed
     */
    public function attendees()
    {
        return $this->morphedByMany(User::class, 'eventable')->withPivotValue('role', 'atendee');
    }

    /**
     * @return mixed
     */
    public function companies()
    {
        return $this->morphedByMany(Company::class, 'eventable')->withPivotValue('role', 'company');
    }

    /**
     * @return mixed
     */
    public function invitees()
    {
        return $this->morphedByMany(User::class, 'eventable')->withPivotValue('role', 'invitee');
    }

    /**
     * @return mixed
     */
    public function leads()
    {
        return $this->morphedByMany(Lead::class, 'eventable')->withPivotValue('role', 'lead');
    }
}
8
  • You seem to be using soft deletes. It's possible your model is softdeleted but this won't be reflected when getting it via DB because the softdeleted trait is on the model. Check if deleted_at is not null Commented Jan 29, 2021 at 21:18
  • 1
    Please provide your CalendarEvent.php file and can you confirm that the record doesn't have a deleted_at timestamp Commented Jan 29, 2021 at 21:39
  • 1
    Although it should work out the table name by itself, declare it in your model protected $table = 'calendar_events'; and try again - see if that gets the record Commented Jan 29, 2021 at 21:58
  • 1
    If you are saying that deleted_at is not null then that is why you're unable to retrieve that record. Commented Jan 29, 2021 at 22:00
  • 1
    @SimonR Now, I got the point. I actually did not know about the soft delete! Now, I understand what is the problem. It has already been deleted! My bad. Commented Jan 29, 2021 at 22:03

1 Answer 1

1

Why not just:

CalendarEvent::where('id', $request->appointmentId)->delete();

Also, check the deleted_at column. If that is not null, then the select will return null, unless you add the ->withTrashed() method.

When using Eloquent objects, the SoftDelete trait is used, when using DB:: directly, then the SoftDelete trait is not used.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it had been already deleted. My bad. I did not know about the Soft Delete in Laravel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.