Laravel Model can I implement - laravel

Can I use implements on a model in laravel and if so how can I define a attribute in the $fillable array, If I can't use it with a variable I can just have a interface with a method getBool()?
I have two models both with a attribute in the fillable array called bool i would like to have a function that operation on both of these models.
So how can I create a interface class that I can implement on the models?

I don't really understand your question maybe because of the lingo. However, interface is just a contract definition, it's not designed to add functionality per-se, just structure.
You could use a PHP traits to have shared code between models.
PHP Traits

Related

Laravel Eloquent - Trait vs Observer

Lets say I have some models with the field "slug". On model saving, I want to set the slug automatically.
My first guess to do this, was to create an observer and reacting on the "creating" event. But after a short google search, I found https://github.com/spatie/laravel-sluggable
Why do they prefer Traits over Observers? When to use Traits , when should I use Observers?
Observers are nothing new but classes in Laravel Framework
You may use observers to group all of your listeners into a single class. Observers classes have method names which reflect the Eloquent events you wish to listen for
But Trais are for reusing codes
Traits is a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.
You use the package's trait in model and it does all the slugging-mechanism
The package of Spatie you linked uses traits to implement the code to your model, they listen to the events [here] just like you would do in an observer.
So they do it to make it easy to implement these features, via the trait.

Laravel add custom method for all eloquent models

I am looking for a way to add few custom methods which will be used in all models. I can imagine 3 ways of doing it :
Adding custom method to main Eloquent's Model.php class file(I want to avoid doing this as this is a core file)
Creating a custom model class with required custom methods, which will extend to eloquent's Model class and all the models in the project will extend to custom model class.
Adding a trait which will have my methods and include it inside all models
However, I want to do it more efficiently and best way possible. Is their any other way to do it?
PS I am using laravel 5.2 as its an old project.
Based on the comment discussion and adding my experience in Laravel I would suggest you to go either with #2 or #3 approach as #ceejayoz have specified in the comments
first one is definitely a bad approach as you need to modify the core which is not at all a good practice. Second and third are both good approaches.
But, before that you need to check your requirements if literally all models (including any future ones your app will ever have) need the extra functionality, however you can use traits for all models.
If I have the choice probably I will go for traits over custom models as traits are relatively simple then custom models

How Laravel models use function chaining?

I am working on Laravel models kind of thing, I am not able to understand how Laravel models use function chaining when the base model class is abstract. Please provide me some suitable explanation example. Thanks in advance.

How to keep helper methods when using Repository pattern in Laravel 5?

When working with repository pattern we have interface and some repository classes which implement this interface. If I'm not mistaken, one of the SOLID principles says that those repository classes should return the same type of data for each interface method so that if we switch implementation of the interface nothing breaks down.
If I have an eloquent repository class, which returns an array of eloquent classes of all users, like return User::all(), I have not a simple array but an array of Eloquent models. So instead I might want to use toArray() to return simple array, so that if I switch my implementation of the interface to some other ORM (for example UserDoctrineRepository or I don't know...) everything will still work.
If I understand correctly, inside UserEloquentRepository we use an eloquent model class to help us get data using Eloquent. However, inside my model (User class) I might have some helper methods, like getFullName(). If I simple use toArray() inside UserEloquentRepository I won't get this helper method in my controller, and, eventually in my view.
In other articles I've read they keep it like return User::all(), however, it means that I'm still coupled to Eloquent as I don't get a simple array, but an eloquent objects array
What you get from User::all() or basically every Eloquent query is a Illuminate\Database\Eloquent\Collection. The problem is that when you call toArray() on the collection it will convert all the items of the collection into an array too and you loose all the methods of your model. Instead you can call all() on the collection to get to the array of model objects:
$collection = User::all();
return $collection->all();
Yes that still means you will have Eloquent specific models in your resultset however if you don't use Eloquent features like attribute accessors you will have an easy time replacing it with another type of model.

Laravel - Flexible controllers and repository

To create a flexible controller and to use the IoC of Laravel, one would do the following (say for a table User)
Create interface UserInterface
Create class EloquentUserRepository implements UserInterface
App::bind the interface to the implement
But, then you also create a model User that extends Eloquent, and your EloquentUserRepository then basically calls this model. Other than the provided Eloquent functions, if you create a custom function or relation in User, then you need to create a function in EloquentUserRepository that simply returns that. Isn't that repetitive? Is there a way to directly have the model implement the interface?
Isn't that repetitive?
Yes. Unless you have a specific need - you can run the risk of 'over engineering' or 'over abstracting' your application.
If you dont plan to swap out your 'users' with anything in the future - there is no need to do so much. Just because you "can" do something doesnt mean you "should".
You could just have UserInterface - then your model does
class User extends Eloquent implements UserInterface
and just skip the Repository completely.

Resources