Laravel: pluck method on Model instance - laravel

I am just learning about Laravel, so excuse me for any newbie questions. In a tutorial I have seen people using pluck directly on Model classes, such as App\MyModel::pluck('id').
My question is where the class gets this method from. It extends the Model class but neither there nor in any trait is it defined. I can only see it defined for collections. I have looked at the source intensely but cannot see it.
Thanks
Zibellon

All of Laravel models they do extend a base class named Model. Example when you define a model the class will look like
class Channel extends Model
{
//stuff
}
Class channel extends base class Model, if you go to Model base class you will find it under Illuminate\Database\Eloquent\Model
That is the abstract base Model class which through some magic methods it recalls the method defined on QueryBuilder under 'Illuminate\Database\Query\Builder' there method pluck is defined

Related

Accessing involved models in MorphPivot class on boot methods

I have morph many-to-many relation between Tag vs Video and Thread, I'm using a custom model to represent the intermediate table of my relationships, I created a model which extends MorphPivot, So my question is, How to get models on boot method ? For example:
use Illuminate\Database\Eloquent\Relations\MorphPivot;
class TagResource extends MorphPivot
{
protected static function boot()
{
parent::boot();
self::created(function ($model){
dd($model);
});
}
}
I want whenever there is a relationship between my models is created i get them with created event on boot method, But when i dd($model) in the method it just returns the table attributes of TagResource and not the Tag or Video model, Is there a way to get involved models instances ?

How to use different method of a controller in laravel application

In my laravel application i should use some create and update method of some controller in another controller
According to my search is not a good thing to call a method from controller in another
I cant see the why don't call a controller method in another controller
I'm doing this way :
class Controller extends BaseController
{
protected $variable;
public function __construct()
{
$this->variable = "Hello";
}
}
and
class ClientController extends Controller
{
public function __construct()
{
parent::__construct();
}
}
The __constructor is a magic method of class. It calls when you trying to create instance of class. So there is no way to use constructor without creating an instance or extendeding from another class. If you have a common code in different classes there a best way to use traits. thats give you an opportunity to include your trait and use methods ,making your code beutiful , flexible , readable following principes DRY,KISS.
you can create a base class with constructor and extend other controller of it
or you can put your code in to Http\Controllers\controller.php ('main controllers constructor')
also you can use trait

Laravel model existed but return Class 'App\Entities\OrignalTripModel' not found

Model included in my Controller as
use App\Entities\OrignalTripModel;
But it returns
Class 'App\Entities\OrignalTripModel' not found
i am using it into controller as
OrignalTripModel::create($inputs);
Kindly include it in your controller like this:
use App\Entities\OrignalTripModel;
And please check namespace for the model OriginalTripModel to see if it has namespace App\Entities; and that is extends model like this class OriginalTripModel extends Model

Method [validate] does not exist

Method validate does not exit. I tried to validate my form values as shown in the figure. But it give me this error. Please tell me . I am new to laravel.
[
You aren't extending the correct Controller.
class UserController extends Controller {
...
}
Controller then extends BaseController.
If you open up Controller which resides in the same namespace as your other controllers, you will see it uses the trait ValidatesRequests which is what provides the validate method.
You can also remove the line use Illuminate\Routing\Controller as BaseController;. There should be no reason to import that.

Extending Eloquent Models in Laravel (use different tables)

I’m building a Laravel application that involves tracking different types of leads. For example, there are Refinance leads and Purchase leads.
Since the leads share a lot of information and functionality, but not all, my thinking was to create a Lead class, which extends Laravel’s Model class, and then a RefinanceLead class, which extends the Lead class.
So I’d have:
class Lead extends Model
{
// shared lead stuff
}
class RefinanceLead extends Lead
{
// stuff specific to refinance leads
}
My questions are:
Does this strategy make sense?
If it does, how is Eloquent going to handle the data? Will I have a leads table and a refinance_leads table?
Will a new instance of the RefinanceLead class utilize anything in the leads table?
I’ve had trouble answering this question via the documentation, but if I missed where this is explained, please let me know. Thanks.
1. Yes, it makes perfect sense to have all the common functionality in a parent model.
2. Basically each Eloquent model will handle the data from its own table defined in the protected $table variable. You can override the parent variable to set a separate table for all the different child models. Laravel Table Names
For example if you use the getId() method on a RefinanceLead instance it will return the id from refinance_lead table. If you use it on a PurchadeLead instance it will retirn the id from purchade_table
class Lead extends Model
{
public function getId() {
return $this->id;
}
}
class RefinanceLead extends Lead
{
protected $table = 'refinance_leads';
}
class PurchaseLead extends Lead
{
protected $table = 'purchase_leads';
}
3. I don't know what are your exact needs, but in general I'll suggest making the Lead class abstract and so you don't associate a table for it. Use it only to separate common functionality, relations, etc...
Of course as it was suggested in the comments, implementing an interface is always a good idea.
abstract class Lead extends Model implements LeadContract
{
// class body
}

Resources