Challenges
Missing JSONB Metadata
The posts controller ignores the metadata field — add it to the INSERT and SELECT.
The Problem
The POST /posts endpoint accepts a metadata JSON field in the request body. However, the query that inserts the post ignores it, and the query that fetches the posts doesn't return it.
The posts table already has a metadata JSONB column. Your job is to wire it up in the controller so the data is saved and returned.
What You Need to Fix
Load this challenge:
php artisan challenge:start db-missing-jsonbThree files are copied into your project:
Http/controllers/posts/store.php— inserts a postHttp/controllers/posts/index.php— lists postsroutes/routes.php— registers the endpoints
Open Http/controllers/posts/store.php. The query looks like this:
$db->query(
'INSERT INTO posts (title, body, user_id) VALUES (:title, :body, :user_id)',
[
'title' => $_POST['title'] ?? '',
'body' => $_POST['body'] ?? '',
'user_id' => $user['id'],
]
);Open Http/controllers/posts/index.php. The query looks like this:
$posts = $db->query(
'SELECT id, title, created_at FROM posts ORDER BY created_at DESC'
)->get();What You Must Do
- Update the INSERT query: Add the
metadatacolumn to theINSERT INTOstatement and pass the:metadataparameter. - Update the parameter array: Pass
$_POST['metadata'] ?? nullto the:metadataparameter in the$db->querycall instore.php. - Update the SELECT query: Add the
metadatacolumn to theSELECTstatement inindex.php.
Hints
INSERT INTO posts (title, body, user_id, metadata) VALUES (:title, :body, :user_id, :metadata)- The value for
:metadatacan benullif the user didn't provide it, or the raw JSON string from$_POST['metadata']. - In
index.php, just addmetadatato the list of columns selected.
Verify
php artisan challenge:verify