Skip to content
Advertisement

Codeigniter 4 : “Attempt to read property ‘name’ on array” error when executing ->findAll() on the BaseBuilder

this is my first time exploring Codeigniter 4 and I follow some tutorials to understand the basic crud process.

Below are my controller where the model query is used.

<?php

namespace AppControllers;

use CodeIgniterRESTfulResourcePresenter;

class Project extends ResourcePresenter
{

protected $modelName = 'AppModelsProjectModel';

public function index()
    {

    return view('projects/index', ['projects' => $this->model->orderBy('created_at', 'asc')->findAll()]);
    }
}

Then below are the index page where I want to display the list and the part where I have a problem.

<?php foreach ($projects as $project) : ?>
                <tr>
                    <td><?= $project->name ?></td>
                    <td><?= $project->description ?></td>
                    <td>
                        <form action="<?php echo base_url('/projects/delete/' . $project->id); ?>" method="post">
                            <a class="btn btn-outline-info" href="<?php echo base_url('/projects/show/' . $project->id); ?>">
                                Show
                            </a>
                            <a class="btn btn-outline-success" href="<?php echo base_url('/projects/edit/' . $project->id); ?>">
                                Edit
                            </a>
                            <button type="submit" class="btn btn-outline-danger">Delete</button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>

‘name’ and ‘description’ are the column names stored in the database.

Can anyone help me check whether the problem is because of code or another external factor since my friend who tried the same tutorial succeeded even though the code is the same?

Below are the errors that show up.

enter image description here

Advertisement

Answer

->findAll() returns an array of elements.

Instead of: ❌

  <td><?= $project->name ?></td>
  <td><?= $project->description ?></td>

Use this: ✅

  <td><?= $project['name'] ?></td>
  <td><?= $project['description'] ?></td>

Do the same for other sections. I.e :$project->id.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement