我目前正在 youtube 上观看来自 laracasts 的 php 初学者播放列表,但由于上图中显示的错误,我已经被困了几天
我一直在重放视频和重写代码,但我仍然遇到同样的错误。从教程中我需要在我的 note.php 上制作我自己的获取方法(在这种情况下,它的方法名称是 findOrFail()),这是语法
<?php
$config = require('config.php');
$db = new Database($config['database']);
$heading = "Note" ;
$currentUserId = 1;
$notes = $db->query('select * from notes where id = :id', [
'id' => $_GET['id']
])->findOrFail();
if ($note['user_id'] !== $currentUserId){
abort(Response::FORBIDDEN);
}
require "views/note.view.php";
和 notes.php 语法
<?php
$config = require('config.php');
$db = new Database($config['database']);
$heading = "My Notes" ;
$notes = $db->query('select * from notes where user_id = 1')->fetchAll();
require "views/notes.view.php";
关于 Database.php 的语法,就在这里
<?php
class Database{
public $connection;
public $statement;
public function __construct($config, $username = 'root', $password=''){
$dsn = 'mysql:' . http_build_query($config, '', ';'); // example.com?host=localhost;port=3306;dbname=myapp
$this->connection = new PDO($dsn, $username, $password,[
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
}
public function query($query, $params = []){
$this->statement = $this->connection->prepare($query);
$this->statement->execute($params);
return $this;
}
public function find(){
return $this->statement->fetch();
}
public function findOrFail(){
$result = $this->find();
if(! $result){
abort();
}
return $result;
}
}