Override Model property or method in laravel using service provider - php

I am using Laravel 5.2 and want to override a property of framework model, I have lot of models and don't want to go each of them and override.
i:e protected $dateformat; // I don't want to write this in all of my models.
I have a solution in my mind,
Either, I create my own basemodel and extend all my models from this class and override a property in that basemodel like.
class MyBaseModel extends Model {
protected $dateformat;
}
class Employee extends MyBaseModel {
}
But, is there any other method like creating a service provider?

Related

Extending one model with another model in Laravel

I am pretty new to laravel and currently exploring its concepts. In some videos I saw a concept of models inheritance. I wonder if we can use models relationships in laravel 5.6 then why we need to inherit models. In which case we need to or should inherit models.
eg Base Model:
class User extends Authenticatable
{
}
eg Child Model:
Class UserTypeOne extends User()
{
}
eg Child Model2:
Class UserTypeTwo extends User
{
}
Thanks in advance.
You don't really need to inherit Models like class inherit. You should use Eloquent Relationship instead. Prior to development, you have to do proper database designing.
https://laravel.com/docs/5.6/eloquent-relationships

How to overwrite an vendor class

How can i overwrite a vendor class?
I'm using Laravel Spark and i wanna have Uuid for all models. Due Spark manage some models inside the package and i don't see a possibility to use my own model for Notifications etc. i would like to overwrite the base Model class from Illuminate\Database\Eloquent\Model, so i could include there my uuid trait.
I tried over the ServiceProvider with:
public function boot()
{
//
$this->app->bind('Illuminate\Database\Eloquent\Model', 'App\Models\Model');
}
But it didn't worked.
Is it possible or maybe exist a better way?
Thanks for any help.
Create a custom model class which will extend the eloquent model.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CustomModel extends Model {
// Your implementation
}
And then rest of the models you extend your custom model.
class Test extends CustomModel {
}

Add global method to all Eloquent Models in Laravel 5.2

I want add given method to all my Eloquent Models:
public function isNew(){
return $this->created_at->addWeek()->gt(Carbon::now());
}
Is this possible to do without bruteforce?
I could not find anything in the docs
Thanks
What you can do:
Create BaseModel class and put all similar methods in it. Then extend this BaseModel class in all models instead of Model class:
class Profile extends BaseModel
Use Global Scope.
Create trait and use it in all or some of your models.
Sure, you can do that. Just simply extend the Laravel's eloquent model like so:
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
abstract class BaseModel extends Model
{
public function isNew() {
return $this->created_at->copy()->addWeek()->gt(Carbon::now());
}
}
Now your model should extend from this new BaseModel class instead:
class User extends BaseModel {
//
}
This way you can do something like this:
User::find(1)->isNew()
Note that I also call copy() method on the created_at property. This way your created_at property would be copied and won't be accidentally added 1 week ahead.
// Copy an instance of created_at and add 1 week ahead.
$this->created_at->copy()->addWeek()
Hope this help.

Is there any method/way to find out all the functions of some class in laravel?

I am understanding Laravel(5.1 version although 5.2 now recently comes) framework...and studying it deeply...What I am trying to ask let me elaborate through some example: I have created the model named Blog:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Blog extends Model {
protected $fillable = ['title','body'];
}
Now in my controller I am accessing the function create() of this class in my controller store() method like: public function store(BlogRequest $request)
{
$input = Request::all();
Blog::create($input);
return redirect('blogs');
} As you can see that in above controller method we are accessing the static function/method create() i.e Blog::create($input) ..so the question is as there are so many other methods exists like create() method of a model(Blog) class which extends the Model class...is there any way/strategy/function to find out/know all the functions of this Model Class...
Yes! You can refer to the API documentation.
For example, I searched for model and found Illuminate\Database\Eloquent\Model, which is the class your models extend and there it is the create static method.
You can change the Laravel version on the top left and filter for classes, namespaces, interfaces and traits on the top right. Pretty neat!
Edit:
You can use the getMethods method on the ReflectionClass to list all available methods for a given class.
For example:
$methods = (new ReflectionClass('\App\Blog'))->getMethods();
dd($methods);

Laravel - extending models to controller

Is extending model to a controller a good idea?
Like say I want to make my functions in the model protected instead of public and then extend my model to the controller so that the controller can still call those functions?
Is it a good thing to do? Or should I just leave them public?
No, don't do that! That defeats the whole object of MVC. Leave all functions in the model public unless they are only to be used by that model or related models. In which case they can be private/protected respectively.
If you extend your model to the controller then the controller turns into a model.
I can't imagine how controllers could "extend" your model.
You can extend Eloquent models for example, and make inherited models like:
class ModelB extends ModelA
{
// code
}
class ModelA extends Eloquent
{
protected $something;
//code
}
and then use them in the controller accordingly. Your controllers extend a different class, BaseController and have a different purpose than models to begin with.
So the short asnwer is no.

Categories