title: Laravel delete linked model's data by user_id in booted function of User model
body: <p>I've read part of the <a href="https://laravel.com/docs/8.x/eloquent#events-using-closures" rel="nofollow noreferrer">Laravel</a> docs for events and closures for models, I've got various models in my project whereby a user may have data linked to them in another table by a <code>user_id</code> column, the <code>user_id</code> column that I have in my various tables is structured as an unsigned integer <em>(I'm aware I could've gone with a foreignId column by kind of a legacy approach here)</em></p>
  <p>It looks like:</p>
  <pre class="lang-php prettyprint-override"><code>$table-&gt;integer('user_id')-&gt;unsigned()-&gt;nullable()-&gt;index();
  </code></pre>
  <p>I'd like to delete user data by their ID within these other tables and rather than creating a delete function and grabbing each model I want to delete data against, I've utilised the closure <code>booted</code> function and what I believe to be an event to listen and delete related model data, but I experience an error when trying to delete my user account, other data in other tables isn't deleted, the error I get is:</p>
  <blockquote>
  <p>Call to undefined method App\Models\User::releationship()</p>
  </blockquote>
  <p>My user model looks like:</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\Models;
  
  use Illuminate\Contracts\Auth\MustVerifyEmail;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Database\Eloquent\SoftDeletes;
  use Illuminate\Database\Eloquent\Model;
  
  use Tymon\JWTAuth\Contracts\JWTSubject;
  
  class User extends Authenticatable implements JWTSubject, MustVerifyEmail
  {
      use Notifiable, SoftDeletes;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'first_name', 'last_name', 'email', 'password'
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token'
      ];
  
      /**
       * The attributes that should be cast to native types.
       *
       * @var array
       */
      protected $casts = [
          'email_verified_at' =&gt; 'datetime'
      ];
  
      /**
       * Get the identifier that will be stored in the subject claim of the JWT.
       *
       * @return mixed
       */
      public function getJWTIdentifier()
      {
        return $this-&gt;getKey();
      }
  
      /**
       * Return a key value array, containing any custom claims to be added to the JWT.
       *
       * @return array
       */
      public function getJWTCustomClaims()
      {
        return [];
      }
  
      /**
       * Route notifications for the Slack channel.
       *
       * @param  \Illuminate\Notifications\Notification  $notification
       * @return string
       */
      public function routeNotificationForSlack($notification)
      {
          $url = $this-&gt;slack_webhook;
          $webhook = (isset($url) &amp;&amp; !empty($url)) ? $url : null;
  
          return $webhook;
      }
  
      /**
       * The &quot;booted&quot; method of the model.
       *
       * @return void
       */
      protected static function booted()
      {
          static::deleted(function ($model) {
              $model-&gt;relationship()-&gt;delete();
          });
      }
  }
  </code></pre>
  <p>And an example <em>(of many)</em> model I have, <code>UptimeChecks</code> looks like:</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  
  class UptimeChecks extends Model
  {
  
      /**
      * The table associated with the model.
      *
      * @var string
      */
      protected $table = 'uptime_checks';
  
      /**
      * Join user table
      */
      public function user()
      {
        return $this-&gt;belongsTo('App\User');
      }
  }
  </code></pre>
  <p>All is then kicked off by a <code>deleteAccount</code> function in my API, which is deleting the user's account, but isn't deleting data in other tables. <strong>What am I missing and how could I do a check to make sure other data is deleted before confirming to the user that their account and linked data is gone?</strong></p>
  <pre class="lang-php prettyprint-override"><code>/**
   * Delete account
   *
   * @return Response
   */
  public function deleteAccount(Request $request)
  {
  
      // attempt to delete account
      try {
  
          $user = User::findOrFail(Auth::id());
          $user-&gt;delete();
  
          // everything went okay!
          return response()-&gt;json(['success' =&gt; true, 'message' =&gt; 'Your account has been deleted'], 200);
  
      } catch (Exception $e) {
  
          // catch the error
          return response()-&gt;json(['success' =&gt; false, 'message' =&gt; 'We was unable to delete your account at this time'], 422);
  
      }
  
  }
  </code></pre>