How to write a NotMapped object in a Laravel model?

I'm new to laravel , so my question might be weird for someone. Well, to my question, how can I write an object in the Laravel Model class, which after my transfer will not create a single field in the database. for example

 class JobseekerModel extends Model { use SoftDeletes; protected $table='dbl_jobseekers'; protected $primaryKey='id'; protected $fillable=[ 'FirstName', 'MiddleName', 'LastName', 'Dob', 'Education', 'DesireField', 'Skill', 'SpecialSkill', 'Experience', 'Location', 'HomeAddress', 'Salary', 'Comenteries', 'Resume' ]; protected $dates = ['deleted_at']; } 

This is my model, now I want to add another property called "PagedListSize" in my model, however I do not like to create it as a database column. So how do I do this?

For example, I am familiar with using the NotMapped property in the .Net Framework , which is written as

 [NotMapped] public int PagedListSize {set; get;} 

So how can I do this. Is there a way to do this in laravel? I am working on Laravel 5.4

+7
php eloquent laravel-5 model
source share
3 answers

The best way to do this in Laravel is with custom mutators . In addition, mutators can be configured to display json or model array output in a dump. for example for PagedListSize we could:

 public functionGetPagedListSizeAttribute() { return #some evaluation; } protected $appends = array('pagedListSize'); 

Thus, PagedListSize will be available not only directly as a field, but will also be available when the model is serialized as Json or Array.

0
source share

You can create your own Mutators to have custom properties that do not display them in the database fields in Laravel.

 class Tag extends Model { public function getFullNameAttribute() { return $this->first_name.' '.$this->last_name; } public function setFullNameAttribute($value) { list($this->first_name, $this->last_name) = explode(' ', $value); } } 

and then you can use it as soon as you initialize the model:

 $tag->full_name = "Christos Lytras"; echo $tag->first_name; // prints "Christos" echo $tag->last_name; // prints "Lytras" 

Here is a screenshot of the tinker:

Artisan Tinker example

+4
source share

You can add protected properties to the Laravel Model, which is great if they do not interfere with field names. In addition, with Laravel, migration solves database structures, not models, so you do not risk automatically creating fields (in fact, models work by default without any properties out of the box).

EDIT: example from the default package User.php

 <?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * You can add some properties here * will require getter and/or setters * does not need to be fillable */ protected $someLogicalProperty; } 

The actual db structure is defined in the migration (2014_10_12_000000_create_users_table.php):

 Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); 

As you can see, the timestamps and token are not even specified in the user model. All populated ones, if defined, will be set as a public property in the user object ( $user->name = 'Bob'; but you can also pass it as an argument to the inherited create () / save () methods). Objects do not have direct access to Laravel, but they exist and can be further refined if necessary.

+2
source share

All Articles