1

I have say simple example of getting an object using this query:

return App\models\SuccessIndicatorYearWeight::find(1);

This returns:

{
    success_indicator_year_weight_id: "108",
    success_indicator_id_fk: "174",
    year_id_fk: "1",
    criteria_excel: "05-05-2015",
    criteria_very_good: "08-05-2015",
    criteria_good: "11-05-2015",
    criteria_fair: "15-05-2015",
    criteria_poor: "20-05-2015",
    created_by: "4",
    create_date: "2015-05-01 21:40:35",
    updated_by: "4",
    update_date: "2015-05-02 06:49:30",
}

Now I want to return only these properties from the object, criteria_excel, criteria_very_good, criteria_fair, criteria_poor.

I have tried with these:

return App\models\SuccessIndicatorYearWeight::find(1)->pluck('criteria_excel', 'criteria_very_good', 'criteria_fair', 'criteria_poor');

And

return App\models\SuccessIndicatorYearWeight::find(1)->select('criteria_excel', 'criteria_very_good', 'criteria_fair', 'criteria_poor')->get();

And was unsuccessful.

A simple workaround is this:

return App\models\SuccessIndicatorYearWeight::whereId(1)->select('criteria_excel', 'criteria_very_good', 'criteria_good', 'criteria_fair', 'criteria_poor')->get();

But, I was wondering if there are any other ways to return only selected properties of an object.

0

4 Answers 4

1

If you use select(...)->get(), it's always return Object.

But you can add toArray to extract your criteria. Like this :

return App\models\SuccessIndicatorYearWeight::find(1)
    ->select('criteria_excel', 'criteria_very_good', 'criteria_fair', 'criteria_poor')
    ->get()
    ->toArray();

It will return an Array of Associative Array (with your keys).

Sign up to request clarification or add additional context in comments.

Comments

0

If you will always return only those fields when you return JSON (or array) you can use protected $visible = ['fields', 'you', 'need', '...']; in your model.

http://laravel.com/docs/5.0/eloquent#converting-to-arrays-or-json

Comments

0

No, the latest method is aprecciated. Under Eloquent there is an SQL Builder to database. If you not specific select, that would produce following query:

SELECT * FROM indicator_year_weight;

That means to DB to return all fields from table and Eloquent will put them all to object. After adding select:

SELECT criteria_excel, criteria_very_good, criteria_good, criteria_fair, criteria_poor FROM indicator_year_weight;

That means to return only a specified fields, so Eloquent will put only it,

Comments

0

Probably what you need is list as arguments it takes the column you want to extract.

In your case it will be :

$criterias = App\models\SuccessIndicatorYearWeight::whereId(1)->list(
    'criteria_excel', 
    'criteria_very_good', 
    'criteria_fair', 
    'criteria_poor'
);

Here you can find the documentation: http://laravel.com/docs/5.0/queries#selects

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.