Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

if you dd($project); or var_dump($project) from within you foreach, are you getting the full response expected?

Also, how many arrays do you have inside $projects? More than one I guess.

You probably don't need the [0]. Your first array inside $projects may be set as 0, however, the others will be 1, 2, 3 etc.

First off, I would see if you are getting the results expected without marking the first [0] part of the element. In your array ($projects) you are saying that the array is 'marked' with a 0. If for example your array is more like:

projects => [
                    'proj1' => []
                 ]

...as an example, then you would use $project['proj1']->id. Its really how the array is labeled up and what your keys are.

You can also use a key as part of your array like this - Which will use the actual key and not a number:

foreach($projects as $key=>$project)
{
    $tagged_project_item[] = array(
        'id'    => ($project[$key]->id),
        'title' => $project[$key]->title,
        'text'  => $project[$key]->text,
        'extra' => $project[$key]->extra
    );
}

However, my guess is that this should work:

foreach($projects as $project)
{
    $tagged_project_item[] = array(
        'id'    => ($project->id),
        'title' => $project->title,
        'text'  => $project->text,
        'extra' => $project->extra
    );
}

It really depends on how you data is structured in the array. Hopefully that might make a little sense.

Last updated 1 year ago.
0

Thank you for your reply/explanation. When i diedump $project this is what i get: (and that is correct)

array(1) {
  [0]=>
  object(stdClass)#366 (4) {
    ["id"]=>
    int(129)
    ["title"]=>
    int(535)
    ["text"]=>
    int(536)
    ["extra"]=>
    int(537)
  }
}

I also already set it to $key, but didn't change anything.

When i try to remove the [0] or [$key] i will get the error "Trying to get property of non-object". Which makes sense according to the data structure.

But none of it works :/

Last updated 1 year ago.
0

lorienhd said:

I keep getting the error "Undefined offset: 0", and i don't understand why. When i debug $project[0] it has the output i would expect.

$projects[] = \DB::table('tblproject_items')->where('id', $tagged_project->id)->get();

foreach($projects as $project)
{
   $tagged_project_item[] = array(
       'id'    => ($project[0]->id),
       'title' => $project[0]->title,
       'text'  => $project[0]->text,
       'extra' => $project[0]->extra
   );
}

Any advice on what could be going wrong?

Looking at your WHERE clause and your [0]'s put everywhere, I would say that you expect only one project item to match. It also seems like you're using the primary key to filter. If you have a model for the project item, you should use

$projectItems[] = ProjectItem::find($tagged_project->id);

Then $projects would be an array of projectItems. You could also use

$projectItems[] = \DB::table('tblproject_items')->where('id', $tagged_project->id)->first();

Now your code would be at least simpler. Take advantage of the toArray function.

foreach($projectsItems as $item)
{
    $tagged_project_item[] = $item->toArray();
}

I don't understand why your code isn't working. Maybe the error is somewhere else.

Last updated 1 year ago.
0

may be this sounds crazy but $tagged_project_item[] is not an array defined, before the each bucle init it

$projects[] = \DB::table('tblproject_items')->where('id', $tagged_project->id)->get();

$tagged_project_item = array();

foreach($projects as $project)
{
    $tagged_project_item[] = array(
        'id'    => ($project[0]->id),
        'title' => $project[0]->title,
        'text'  => $project[0]->text,
        'extra' => $project[0]->extra
    );
}
Last updated 1 year ago.
0

cast $project as an array.

foreach($projects as (array)$project){

}

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

lorienhd lorienhd Joined 20 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.