user3617088
user3617088

Reputation:

Select a fake column in Laravel Eloquent

Is there anyway I can select a fake column from a table using Laravel Eloquent?

E.g., There are column1 and column2 columns in table1 but I want to show its type also which I know already. Something like : Select 'type1 as type', column1, column2 from table1;

How can I achieve this using Eloquent?

Upvotes: 0

Views: 2601

Answers (1)

Amarnasan
Amarnasan

Reputation: 15569

Option 1:

Just set it in the model after retrieving it:

$whatever = Model::all()->first();
$whatever->fakeit = 'till_you_make_it';
echo $whatever->fakeit 

'till_you_make_it'

Option 2: (more cooler and laravelish)

Define an accessor in your model (just add a public function with the specific name getXXXXXAttribute, where "XXXXX" is the name of the column to fake, and make it return whatever value you want it to have.

class MyModel {
.
.
   public function getFakeitAttribute(){
        return "till_you_make_it";
   }
.
.
}

Upvotes: 2

Related Questions