How can I have multiple implements in my model?
my current model is like this:
class Post extends Model implements ViewableContract
{
........
}
now i need to add implements Feedable as well.
should i use comma in between or put them in brackets or what?
Yeah! you can use comma in between the ViewableContract and Feedable like this:
class Post extends Model implements ViewableContract, Feedable
{
.....
}
Related
I'm trying to cast a model to another one that extends the same model.
Is there a build in way in Laravel this achieve this?
Example
In the code below I would like to cast User to ExtendedUser
class User extends Model
{
...
}
class ExtendedUser extends User
{
...
}
Using Relationships first time and having issue to understand it.
I have a Student Class and a Course Model Class. A student can take many courses. In Student Model I do this:
public function course()
{
return $this->belongsTo('Course');
}
and in controller when I do this: .$student->course()->course_title it gives an error:
FatalThrowableError in Model.php line 779:
Class 'Course' not found
What am I doing wrong?
replace your code with it
public function course()
{
return $this->belongsTo(Course::class);
}
Make sure your namespace in your Course class matches 'App\Models'. You might be trying to access an "empty" namespace.
Namespaces are declared at the top of your class file. So for example:
Course.php:
namespace App\Models;
use Illuminate\Http\Request;
...
class Course extends Model { ... }
OtherClass.php
namespace App\Models;
use Illuminate\Http\Request;
...
class OtherClass extends Model {
...
public function course()
{
return $this->belongsTo('App\Models\Course')
}
}
Please note that the namespace declared at the top of Course.php matches the path provided to the relationship method (return) belongsTo(). You can also provide Course::class to the belongsTo method, but you need to import aka use the class in your php class.
I hope this helps!
I had the same problem and i solved it by adding a name space in the top of my model
for your case,
use \App\Course ; // Your name space of your class in relation
Hope this help others
NB : I used Laravel 5.0
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.
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 {
}
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.