laracasts / The-PHP-Practitioner-Full-Source-Code

https://laracasts.com/series/php-for-beginners
177 stars 71 forks source link

Stuck on lesson 14 #4

Open vmussato opened 7 years ago

vmussato commented 7 years ago

Hi, i think i followed the code along correctly, but i keep getting this error

Fatal error: Uncaught Error: Call to a member function prepare() on null in /Users/vinicius/Projetos/php-learning/database/QueryBuilder.php:16 Stack trace: #0 /Users/vinicius/Projetos/php-learning/index.php(5): QueryBuilder->selectAll('todos') #1 {main} thrown in /Users/vinicius/Projetos/php-learning/database/QueryBuilder.php on line 16

It seems to be some problem related to my pdo object, it was working fine until the refactoring, but in some point it stopped working.

Heres my code

index.php

<?php

$query = require 'bootstrap.php';

$tasks = $query->selectAll('todos');

require 'index.view.php';

bootstrap.php

<?php

require 'database/Connection.php';
require 'database/QueryBuilder.php';

return new QueryBuilder(
    Connection::make()
);

Connection.php

<?php

class Connection

{
    public static function make()
    {
        try {

            return new PDO('mysql:host=127.0.0.1;dbname=mytodo', 'root', '');

        } catch (PDOException $e) {
            die($e->getMessage());
        }
    }
}

and QueryBuilder.php

<?php

class QueryBuilder
{

    protected $pdo;

    public function __contruct($pdo)
    {
        $this->pdo = $pdo;
    }

    public function selectAll($table)
    {
        $statement = $this->pdo->prepare("select * from {$table}");
        $statement->execute();
        return $statement->fetchAll(PDO::FETCH_CLASS);
    }

}

Any idea? I suspect the error is coming from Connection.php generating a messed up pdo and the QueryBuilder is launching an error when it tries to create the $statement.

yioteh commented 5 years ago

I feel your pain @vmussato I'm having the same error, saw threads under the video itself and in laracasts forums.

@JeffreyWay would you be so kind to look at this issue? )

jonathanbcsouza commented 4 years ago

I found your post when I was looking for a solution. The problem for me was that somehow that those lines on my bootstrap file:

return new QueryBuilder( 
Connection::make() 
);

Did not work!

Try to replace for that:

$query = new QueryBuilder(
    Connection::make()
);

I have been uploading this repository where you can find all solutions that are currently working. (21/03/2020).

Just bear in mind that I have named and written down some lines slightly different in order to make more understandable to me.

It will probably fix, I hope so.

jonathanbcsouza commented 4 years ago

Hey @JeffreyWay,

I think you might be able to close this issue now.

Apart from that, thanks for your such a great content on Laracasts :)

Sufian-CSTE commented 2 years ago

In this class QueryBuilder.php

public function __contruct($pdo) { $this->pdo = $pdo; }

The method name is __construct not contruct. this is a spelling mistake

vmussato