因此,我是按照本教程进行操作的,我在第27分钟显示 www.youtube /watch?v = 6Oxfb_HNY0U 在那里,控制器的代码如下:
So Im following this tutorial, Im at minute 27 www.youtube/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); } }现在让我感到困惑的是以下行中的json()参数:
Now what I'm confused about is the parameter of json() in the following line:
return response()->json($article, 201);我在laravel或lumen文档的第二个选项中找不到任何东西. 到目前为止,我也无法通过此参数检测到任何影响.它仅出现在教程的Restlet Client的日志中,请参见屏幕截图.是港口吗??? 它确实出现在教程人员的HTTPS请求的历史记录日志中: imgur/To0Y6cJ
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: imgur/To0Y6cJ
当我有以下几行时:
$this->validate($request, [ 'title' => 'required', 'description' => 'required' ]);未发表评论,那么我总是得到以下答复: imgur/wTtZNrz
not commented, then I ALWAYS get the following response: imgur/wTtZNrz
{ "title": "The title field is required", "description": "The description field is required" }当我评论这些行时,就会出现此错误: textuploader/1oq3n
When I comment these lines, then I get this error: textuploader/1oq3n
SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'created_at'(SQL:插入articles(updated_at,created_at)值(2019-11-25 14:18 :33,2019-11-25 14:18:33))
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))
我无法直接发布此标记,因为这样我的身体将超过最大字符数. 因此,随时将其粘贴到an.html文件中,然后在本地显示.抱歉给您带来不便...
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...
我真的不明白为什么会收到此错误,因为我没有在POST请求中引用这些列.
I don't really understand why I get this error, because I don't reference these columns in my POST request.
Article雄辩模型本身如下所示:
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:
imgur/GpnNBIH
所以我真的看不出为什么这对我不起作用:(
So I don't really see any reason why this won't work for me :(
推荐答案这里有多个问题.
SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'created_at'(SQL:插入articles(updated_at,created_at)值(2019-11-25 14:18 :33,2019-11-25 14:18:33))
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))
尝试使用这种方法:
$validatedData = $request->validate([ 'title' => 'required', 'description' => 'required', ]);
curl ... -H "Content-Type: application/json"
更多推荐
"json()"中的第二个参数是什么?做(流明/Laravel)?
发布评论