Laravel 5 global model methods? - php

I have a number of tables / models setup that have 'locked_at' and 'locked_by' columns, in essence what I would like to do is call something like:
$model->lock();
This method would check that row is not already locked, set the both cells and then save the model.
I can create the lock method inside each of my models, but that doesn't seem like the best idea. I would prefer to create the method once and have it accessible to all models.
Is there a way of doing this in laravel?

Just like ceejayoz suggested, you could achieve it by using a Trait.
Step 1
Create a folder called Traits and inside that create trait class like the following
<?php
namespace App\Traits;
trait Lockable {
public function lock() {
$this->lock = 1;
$this->save();
}
}
Step 2:
Now import the trait on your model class like the following
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Traits\Lockable;
class User extends Authenticatable
{
use Lockable;
}
Thats it! Now you could call the function like
$user = \App\User::find(1);
$user->lock();

You can create BaseModel class that extends Model and make all your models that using lock() method extend BaseModel class instead of theModel. Then just define lock() method in the BaseModel class.

Related

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.

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.

Using Laravel 5 Model

I have created a model using
php artisan make:model Sessions
which creates a model named Sessions in the App\ folder. My Model (Sessions) and the controller(School) calling the model are below
Model (Sessions):
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Sessions extends Model {
//
protected $table = "sch_sessions";
protected $primaryKey = "session_id";
}
Controller (School)
<?php
namespace App\Http\Controllers;
use App;
class SchoolController extends Controller {
public function sessionManagement(){
$Sessions = Sessions::all();
}
}
I get the following error
FatalErrorException in SchoolController.php line 7: Class
'App\Http\Controllers\Session' not found
I've tried putting the model in a folder and "use"ing it but nada. I'm stumped as to why the error is referring to a controller in the first place. Can anyone see something I don't (can't) please?
add on the top
use App\Sessions;
or use it with prefix
\App\Sessions::all();
If user Ali Mohammed's answer doesn't clear it up for you, it looks to me like your error is happening elsewhere. App\Http\Controllers\Session note Session is not plural.
Are you attempting to use the Session class elsewhere in your controller? If so, make sure to prefix it so it's using the Session declared in the global namespace. You can do this with \Session.

Laravel namespace in relations confused me

In my app I have Event and User models.Because of Event model I have to put it into namespace .so I created namespace as of following.
Event
<?php namespace App\Models;
class Event extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
User
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use App\Models\Event;
class User extends Eloquent implements UserInterface, RemindableInterface {
public function events()
{
return $this->hasMany('Event');
}
The relations between User and Event are simple OneToMany. So in my EventController I use POST method to create new event resource .
$e = new Event(array('keys'=>'values')); //without user_id filled
$user->events()->save($e);
At the same time i got an error .
Call to undefined method Illuminate\Support\Facades\Event::newQuery()
If I am not wrong i guess it is namespace error.But namespaces are already declared correctly I guess.
But I try visiting similar questions and used alternative way in relationship and then it worked fine.Personally I don't find it satisfied.Any idea why this is occurred ?
Change the above relation to
public function events()
{
return $this->hasMany('\App\Models\Event');
}
If you put Event inside a namespace you need to use it with it's fully qualified classname.
You can either import it into your controller:
use App\Models\Event;
Or specify the namespace inline:
new App\Models\Event();
Also, I would recommend that if you create a namespace for models that you put all of them in there. (User is missing a namespace in your code)

Categories