Laravel: 3 ways of Processing a Job for a deleted Model

Image for post
Image for post
“Two books on a desk near a MacBook with lines of code on its screen” by Émile Perron on Unsplash

Trying to pull our a rabbit from a hat, without the hat

Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\User]

A) From Model to Array to Model again

FarewellUser::dispatch($user->only([‘id’,’name’,’email’))
    public function __construct(array $user) {
$this->user = User::make($user)
}

B) Play with SoftDeletes and the primary key

    public function unregisterUser(User $user) {        FarewellUser::dispatch($user->id);        $user->delete();
}
public function __construct(int $id)
{
$this->user = User::withTrashed()->find($id);
}

C) Copy it to the Trash bin

public function unregisterUser(User $user)
{
FarewellUser::dispatch(
UserDeleted::create([
'payload' => $user->toArray()
])
);
$user->delete();}
public function __construct(UserDeleted $userDeleted) {
$this->user = $userDeleted;
}
public function __construct(UserDeleted $userDeleted) {
$this->user = User::make($userDeleted->payload);
}

Graphic Designer graduate. Full Stack Web Developer. Retired Tech & Gaming Editor.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store