How To Apply Laravel SoftDeletes on all Models at once? - php

Is there a way I can include use Illuminate\Database\Eloquent\SoftDeletes;
and use SoftDeletes; on all my Models without needing to edit each model?
I want some way to make my models import it automatically.

You can create abstract Model, and put there SoftDeletes and inherit other models from this abstract model.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
abstract class AbstractModel extends Model
{
use SoftDeletes;
}
And for example User model will be:
namespace App\Models;
class User extends AbstractModel
{
}

Related

Implement interface and trait into User model

I have installed the following package into my application so that users are able to follow and unfollow other users.
https://github.com/hypefactors/laravel-follow
In the documentation it states:
Preparing the Eloquent Models To allow an entity to be followed or to
follow other entities, the corresponding models have to implement an
interface and make usage of a trait.
Here's how we do it for a User and Company entity, where a user will
be able to follow a company and the company will be able to be
followed:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Hypefactors\Laravel\Follow\CanFollow;
use Hypefactors\Laravel\Follow\Contracts\CanFollowContract;
class User extends Model implements CanFollowContract
{
use CanFollow;
}
However my user model looks like this:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
I have tried including the files in various ways with no success. I assume its something to do with my user model class looking like this:
class User extends Authenticatable
and not this:
class User extends Model
Can anyone point me in the right direction?
If i do this:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Hypefactors\Laravel\Follow\CanFollow;
use Hypefactors\Laravel\Follow\Contracts\CanFollowContract;
class User extends Authenticatable implements CanFollowContract
{
use Notifiable;
use Uuids;
use CanFollow;
I get the following error:
Trait 'Hypefactors\Laravel\Follow\CanFollow' not found
I decided to change package to this: https://github.com/overtrue/laravel-follow which has worked nicely as expected.
#ceejayoz Thanks for your suggestions

User must implement CanResetPassword interface. Password Reset Laravel 5.4

password reset issue
User.php
namespace App;
use Eloquent;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
class User extends Eloquent implements Authenticatable
{
use AuthenticableTrait;
Add the interface CanResetPassword to your User class:
// at the top of the file
use Illuminate\Auth\Passwords\CanResetPassword;
// at the class declaration
class User extends Eloquent implements Authenticatable, CanResetPassword
{
// ...
}

How to use "Jenssegers\Mongodb" in User model - Laravel 5.5

I want to use "Jenssegers\Mongodb" package in a Laravel 5.5 app.
The package documentation tells to use:
use Jenssegers\Mongodb\Eloquent\Model;
class User extends Model {}
instead of:
use Illuminate\Database\Eloquent\Model;
class User extends Model {}
But, inUser model:
class User extends Authenticatable{}
and in "Illuminate\Foundation\Auth\User" class (what 'Authenticatable' refers to) it's using Illuminate\Database\Eloquent\Model not Jenssegers\Mongodb\Eloquent\Model.
I found this solution to extend what 'Authenticatable' extends directly in my model instead of extending 'Authenticatable' itself, so that I can use Jenssegers\Mongodb\Eloquent\Model;
Is there another better solution or should I make it like that??
use Jenssegers\Mongodb\Auth\User as Authenticatable;
then
class User extends Authenticatable

Laravel mutual methods for many models

I added a method to one of my models:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Test extends Model
{
public static function boot()
{
parent::boot();
static::created(function ($model) {
//log $model or maybe do something more complex here
});
}
}
And i wish to extend the same behaviour to many other models. Copy pasting this doesent seem like a good idea because if something changes i dont want to change it in different places. What is the best solution to write this only once and get the same behaviour to multiple models?
Traits are built for that. Write once and use on all classes.
Create a trait in app/Traits/MyTraitName.php
<?php
namespace App\Traits;
use Carbon\Carbon;
trait MyTraitName
{
public function someName() {
// TO DO;
}
}
and then just import it on top of any model class for example User:
<?php
namespace App;
use App\Traits\MyTraitName;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable, MyTraitName;
..............
The difference between extending a class and making a trait is that when you make a trait that function is already written and it's automatically used the same way on every model, and extending from some base class is better if that function is not going to be the same for every Model.

Class 'App\Eloquent' not found in laravel after running php artisan db:seed

<?php
namespace App;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface
{
}
Your model should extend the Model class:
use Illuminate\Database\Eloquent\Model;
class User extends Model
If you want to use Laravel authentication system, it should extend Illuminate\Foundation\Auth\User:
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable

Categories