问题
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://imgur.com/To0Y6cJ
When I have the following lines:
$this->validate($request, [
'title' => 'required',
'description' => 'required'
]);
not commented, then I ALWAYS get the following response: https://imgur.com/wTtZNrz
{
"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://imgur.com/GpnNBIH
So I don't really see any reason why this won't work for me :(
回答1:
There are multiple issues here.
- You have a misspelling of your db column
created-at
should becreated_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))
- The reason you get the errors "The title field is required" in your json response is because your validation does not pass.
Try with this approach instead:
$validatedData = $request->validate([
'title' => 'required',
'description' => 'required',
]);
- The application type of your POST request is likely also wrong, try adding the
Content-Type
-header and set it toapplication/json
like so
curl ... -H "Content-Type: application/json"
- The
->json($data, 201)
is as the others suggest the HTTP Response Code as described in the standard.
来源:https://stackoverflow.com/questions/59034465/what-does-the-second-paremeter-in-json-do-lumen-laravel