Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JustAPC's avatar

API parameters not passing

Hello everyone! I'm using Laravel 7 with Vue 2 to show some results on landing pages. I wanted to pass the data from an object to an API stuctured in this way.

// file config/posts.php

[
        "id" => 1,
        "name" => "...",
        "description" => "...",
        "github_url" => "...",
        "site_url" => "...",
        "imgs" => [
            "img1" => "...",
            "img2" => "...",
            "img3" => "...",
        ],
    ],

// file api.php

Route::namespace('Api')->group(function () {
    Route::get('/', 'PostController@index');
    Route::get('/posts/{id}', 'PostController@show');
});

I have a problem when i want to click on a single element and get the results of the element I clicked on through the API. In my PostController@show function I structured the data to pass as so:

49 public function show($id)
50    {
51      $posts = config('posts');
52      $post = $posts->find($id);
53
54      return response()->json(compact('post'));
    }

The result of the API gives me an error:

Error: Call to a member function find() on array in file app\Http\Controllers\Api\PostController.php on line 52

How can I fix this? Thank you!

0 likes
7 replies
vincent15000's avatar

When retrieving the data from the configuration file, you retrieve an array and the find() method cannot be applyed to an array.

Here are the helpers accessible for the arrays.

https://laravel.com/docs/9.x/helpers

I assume you have put the array in the configuration file in order to test, but you will retrieve JSON data from the API and not an array.

This way your test will no be relevant.

JustAPC's avatar

@vincent15000 i put the array in the config folder to use it globally in the project and then convert it into an API to be used from Vue components. My goal is to retrieve the id of an element I click on a page, to land on a new page displaying the data only matching that specific id. I thought that the way to do so would have been by modifing the API in order to get only the corresponding id data. What is the correct helper to do so?

kokoshneta's avatar

@JustAPC wrote:

i put the array in the config folder to use it globally in the project and then convert it into an API to be used from Vue components.

Okay, then do that. At the moment, you’re not converting anything – you just retrieve the array from the config file, and then you immediately try to call ->find() on that array. There’s no API involved.

I don’t know what you mean by “convert it into an API”. You can’t convert an array into an API, that doesn’t make any sense. An array is a piece of data; an API is a system used to access data. Converting one to the other would be like saying you want to convert a tennis shoe into the act of walking.

Also, if these are posts, why do you have them in an array in a config file to begin with? Why aren’t they in a database?

1 like
JustAPC's avatar

@kokoshneta cause i just need to store them locally. I will be deploying this site as my portfolio using heroku, so i won't need a database. The api is gonna be involved when I wanna retrieve the data from a Vue component. In the show function I use the array to retrieve all the posts data. Then i would like to get from that array only the post data of the clicked post. I generally used the find() function when filtering the models. I wanted to know if there's a way to do the same thing for the arrays.

1 like
kokoshneta's avatar

@JustAPC No, not really. Arrays don’t have functions the way models (which are objects) do.

What you would normally do is structure your array the same way the built-in config files do: use a nested array, where each key is a post ID and the value is an array of data:

// config/posts.php

return [
	1 => [
		"id" => 1,
		"name" => "...",
		"description" => "...",
		"github_url" => "...",
		"site_url" => "...",
		"imgs" => [
			"img1" => "...",
			"img2" => "...",
			"img3" => "...",
		],
	],

	2 => [
		"id" => 2,
		"name" => "...",
		"description" => "...",
		"github_url" => "...",
		"site_url" => "...",
		"imgs" => [
			"img1" => "...",
			"img2" => "...",
			"img3" => "...",
		],
	],

	...
];

Then in your controller, you should be able to just do this:

public function show($id) {
	return config('posts')[$id];
}
2 likes
JustAPC's avatar

@kokoshneta that pretty much worked out, I had to do some tweaking but got it to work. Thank you very much!

1 like
kokoshneta's avatar

@JustAPC If the problem is fixed, you can mark the question as solved by clicking on “Set Best Answer” on the answer that solved it for you. That gets the question off the Unsolved list.

1 like

Please or to participate in this conversation.