2

i have this controller which access the pivot table in many to many relationships. the columns in the pivot table are values and created_at. i want this controller to return me an array of values and created_at

public function status($s_type)
{

    $o_health_status = Status::where('name', 'health')->first();
    $o_speed_status = Status::where('name', 'speed')->first();

    if ($s_type === 'health'){

        $o_response = $this->statuses()->where('status_id', $o_health_status->id)
        ->select('values','created_at')->orderBy('created_at','ASC')->first();

        if($o_response === null){
            return 'unsigned';
        }
        $o_response = $o_response->values;
        return $o_response;         
    }else{

        $o_response = $this->statuses()->where('status_id', $o_speed_status->id)
        ->select('values','created_at')->orderBy('created_at', 'desc')->first();
        if($o_response === null){
            return 'unsigned';
        }

        $o_response = $o_response->values;
        return $o_response;


    }
1
  • 1
    It's very unclear what you are asking. Commented Nov 15, 2016 at 15:07

2 Answers 2

1

You need to use pluck for this. this will pluck all required elements from a collection: so I clean up a bit and should looks something like this:

public function status($s_type)
{

    $o_health_status = Status::where('name', 'health')->first();
    $o_speed_status = Status::where('name', 'speed')->first();
    $o_response = $s_type === 'health' ? $this->statuses()->pluck('values','created_at')->where('status_id', $o_health_status->id)
            ->select('values', 'created_at')->orderBy('created_at', 'asc')->first() : $this->statuses()->pluck('values','created_at')->where('status_id', $o_speed_status->id)
            ->select('values', 'created_at')->orderBy('created_at', 'desc')->first();

    return ! $o_response ? 'unsigned' : $o_response->values;
}
Sign up to request clarification or add additional context in comments.

2 Comments

for same reason, your code looks perfect, but when i replaced my code with your code. it doesnt work at all.
if its not working., why you marked as the answer? please check again.
0

In laravel, if you need to return an array from the query, you can use pluck() instead of select()

$o_response = $this->statuses()->where('status_id', $o_health_status->id)
    ->pluck('values','created_at')->orderBy('created_at','ASC');// ->first();
//I think you no need to use ->first(); here.

Docs (scroll down to Retrieving A List Of Column Values)

2 Comments

for same reason, when i wrote the code you suggested. it gave me error that says, Method orderBy does not exist! . are you allowed to use pluck and orderdBy at the same time?
normally the resultant array comming according to the values having in the database. if they are ordered, then nothing to worry.

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.