Whats's the proper way to loop through an array for an api in laravel 5.1

Posted 3 years ago by raygun

Hello fellow programmers I am trying to loop through an array. I have experimented a bit but it's not working properly. it outputs.but it doesn't loop through everything I have in my database. this is my code for my ApiController


namespace App\Http\Controllers;

use App\Post;
use App\Http\Requests;

class ApiController extends Controller
    public function index()
        foreach (Post::all() as $post )

        return array(
            'results' => [
                array('id' =>$post->title,
                    'marketname' => $post->subtitle),

also the curly bracket just before last is highlighted in my editor. When I hover over it, it says it's expecting a return. But if I move the return to where it says it should be, I get no output. To be clear I want to display something like this

"results": [
"id": "1002336",
"marketname": "0.2 Harvest Home Echo Park Market"
"id": "1006207",
"marketname": "0.3 Echo Park Market"
"id": "1003343",
"marketname": "0.4 La Familia Verde Farmers Market"

any help would be greatly appreciated

Please sign in or create an account to participate in this conversation.