So Im following this tutorial, Im at minute 27 https://www.youtube.com/watch?v=6Oxfb_HNY0U There, the code for the controller looks like this:
<?php
namespace App\Http\Controllers;
use App\Article;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
//
public function showAllArticles(){
return response()->json(Article::get(['title', 'description', 'status'])); // ::get([]) spezifiziert die zu referenzierenden Attribute
// ::all() referenziert alle Attribute einer Tabelle/Relation
}
public function showOneArticle($id){
return response()->json(Article::find($id));
}
public function create(Request $request){
//dd($request); //for debugging whether the request is actually being processed
$this->validate($request, [
'title' => 'required',
'description' => 'required'
]);
//dd($request); //for debugging whether the specified fields are required
//insert record
$article = Article::create($request->all());
return response()->json($article, 201);
}
}
Now what I'm confused about is the parameter of json() in the following line:
return response()->json($article, 201);
I couldnt find anything on this second option on the laravel or lumen documentation. So far I couldn't detect any effect by this parameter either. It only appears in the log of the Restlet Client of the tutorial, see screenshot. Is it a port??? It does appear in the history log of HTTPS Requests of the tutorial guy: https://i.sstatic.net/VH0A6.jpg
When I have the following lines:
$this->validate($request, [
'title' => 'required',
'description' => 'required'
]);
not commented, then I ALWAYS get the following response: https://i.sstatic.net/bftky.jpg
{
"title": "The title field is required",
"description": "The description field is required"
}
When I comment these lines, then I get this error: https://textuploader.com/1oq3n
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'created_at' in 'field list' (SQL: insert into
articles
(updated_at
,created_at
) values (2019-11-25 14:18:33, 2019-11-25 14:18:33))
I couldnt post this markup directly because then my body would exceed the maximum number of characters. So feel free to paste it into an.html file and then display it locally. Sry for that inconvenience...
I don't really understand why I get this error, because I don't reference these columns in my POST request.
The Article eloquent-model itself looks like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $fillable = [
'title', 'description', 'status'
];
}
My table on the DB side looks like this:
https://i.sstatic.net/Ncenb.jpg
So I don't really see any reason why this won't work for me :(
There are multiple issues here.
created-at
should be created_at
as per the Exception thrown by Laravel:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'created_at' in 'field list' (SQL: insert into
articles
(updated_at
,created_at
) values (2019-11-25 14:18:33, 2019-11-25 14:18:33))
Try with this approach instead:
$validatedData = $request->validate([
'title' => 'required',
'description' => 'required',
]);
Content-Type
-header and set it to application/json
like so curl ... -H "Content-Type: application/json"
->json($data, 201)
is as the others suggest the HTTP Response Code as described in the standard.