2

I want to pull a array from outer array.

My result

enter image description here

I want to remove circle symbol of array

{
"Success": "1",
"Message": "Subtopic Wise Questions...",
"Subtopic Questions": [
    [
        {
            "id": "93",
            "topic_id": "36",
            "name": "Cell membrane and organelle",
            "created_at": "2018-08-29 23:06:34",
            "updated_at": "2018-08-29 23:06:34",
            "q_count": "127"
        }
    ],   
]

}

Here is my output result of array. the

My Controller code

 foreach($findid as $v)
        {
            $count[] = DB::table('questions')
                        ->join('subtopics', 'subtopics.id', 'questions.subtopic_id')
                        ->select('subtopics.*', DB::raw('count(questions.id) as q_count'))
                        ->where('subtopic_id', $v)
                        ->get();
        }


        return response([
                    'Success' => "1",
                    'Message' => "Subtopic Wise Questions...",
                    'Subtopic Questions'   => $count
                    ]); 
4
  • 1
    use $count instead of $count[] Commented Nov 29, 2018 at 5:52
  • make foreach loop through Subtopic Questions->Subtopic Questions Commented Nov 29, 2018 at 5:53
  • @JigneshJoisar if i use $count it's return only first column of result. Commented Nov 29, 2018 at 5:57
  • Seems to my you should try to fetch all required rows at once instead of one each time. Commented Nov 29, 2018 at 5:58

3 Answers 3

2

use first() to obtain just an object as per your image you want to remove extra array quotes so

$count[] = DB::table('questions')
                    ->join('subtopics', 'subtopics.id', 'questions.subtopic_id')
                    ->select('subtopics.*', DB::raw('count(questions.id) as q_count'))
                    ->where('subtopic_id', $v)
                    ->first();

Hope it helps you!

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

4 Comments

@KrishBhardwaj Eloquent will return an array of objects as you have displayed result it is clearly shown that its an array of objects if you dont use toArray() . So if you just want to pull array not object than you will need to use toArray()
i used toArray() but not working to achieve result according me.
ohh okay i got it ! please check my edited answer. @KrishBhardwaj
@KrishBhardwaj QueryBuilder will return Illuminate\Support\Collection. @LeenaPatel Eloquent will return `Illuminate\Database\Eloquent\Collection. Not array or array of objects.
1

use whereIn method instead of where method

note only store id in array then used whereIn method

$count = DB::table('questions')
                ->join('subtopics', 'subtopics.id', 'questions.subtopic_id')
                ->select('subtopics.*', DB::raw('count(questions.id) as q_count'))
                ->whereIn('subtopic_id', array_wrap($findid))
                ->get();

note if not working as u want restult then add toArray() method in query like this

$count->toArray();

 return response([
               'Success' => "1",
               'Message' => "Subtopic Wise Questions...",
               'Subtopic Questions'   => $count
     ]);

1 Comment

Can replace is_array($findid) ? $findid : [ $findid] with array_wrap($findid)
0

Your query is expensive. Since you are looping through array of ids, you are going to run queries count(findid) number of times. You can optimize and achieve the same as,

$count = DB::table('questions')
            ->join('subtopics', 'subtopics.id', 'questions.subtopic_id')
            ->select('subtopics.*', DB::raw('count(questions.id) as q_count'))
            ->whereIn('subtopic_id', $findid)
            ->get();


return response([
    'Success' => "1",
    'Message' => "Subtopic Wise Questions...",
    'Subtopic Questions'   => $count
]); 

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.