"json()" to (Lumen/Laravel) 中的第二个参数是什么意思?
What does the second paremeter in "json()" do (Lumen/Laravel)?
所以我正在学习本教程,我在第 27 分钟 https://www.youtube.com/watch?v=6Oxfb_HNY0U
在那里,控制器的代码如下所示:
<?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() 的参数:
return response()->json($article, 201);
我在 laravel 或 lumen 文档中找不到关于第二个选项的任何内容。
到目前为止,我也无法检测到此参数的任何影响。它只出现在教程的 Restlet Client 的日志中,请参见屏幕截图。是端口吗???
它确实出现在教程人的 HTTPS 请求的历史日志中:
https://imgur.com/To0Y6cJ
当我有以下几行时:
$this->validate($request, [
'title' => 'required',
'description' => 'required'
]);
没有评论,那么我总是得到以下回复:
https://imgur.com/wTtZNrz
{
"title": "The title field is required",
"description": "The description field is required"
}
当我评论这些行时,我得到这个错误:
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))
我不能直接 post 这个标记,因为那样我的正文会超过最大字符数。
所以请随意将其粘贴到 an.html 文件中,然后在本地显示。抱歉给您带来不便...
我真的不明白为什么会出现此错误,因为我没有在我的 POST 请求中引用这些列。
文章 eloquent-模型本身如下所示:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $fillable = [
'title', 'description', 'status'
];
}
我的 table 在数据库端看起来像这样:
所以我真的不明白为什么这对我不起作用:(
这里有多个问题。
- 根据 Laravel 抛出的异常,您的数据库列
created-at
应该是 created_at
的拼写错误:
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))
- 您在 json 响应中收到错误 "The title field is required" 的原因是您的 验证未通过 .
试试这个方法:
$validatedData = $request->validate([
'title' => 'required',
'description' => 'required',
]);
- 您的 POST 请求的应用程序类型也可能是错误的,尝试添加
Content-Type
-header 并将其设置为 application/json
像这样
curl ... -H "Content-Type: application/json"
-
->json($data, 201)
是其他人建议的 HTTP 响应代码,如 standard 中所述。
所以我正在学习本教程,我在第 27 分钟 https://www.youtube.com/watch?v=6Oxfb_HNY0U 在那里,控制器的代码如下所示:
<?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() 的参数:
return response()->json($article, 201);
我在 laravel 或 lumen 文档中找不到关于第二个选项的任何内容。 到目前为止,我也无法检测到此参数的任何影响。它只出现在教程的 Restlet Client 的日志中,请参见屏幕截图。是端口吗??? 它确实出现在教程人的 HTTPS 请求的历史日志中: https://imgur.com/To0Y6cJ
当我有以下几行时:
$this->validate($request, [
'title' => 'required',
'description' => 'required'
]);
没有评论,那么我总是得到以下回复: https://imgur.com/wTtZNrz
{
"title": "The title field is required",
"description": "The description field is required"
}
当我评论这些行时,我得到这个错误: 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))
我不能直接 post 这个标记,因为那样我的正文会超过最大字符数。 所以请随意将其粘贴到 an.html 文件中,然后在本地显示。抱歉给您带来不便...
我真的不明白为什么会出现此错误,因为我没有在我的 POST 请求中引用这些列。
文章 eloquent-模型本身如下所示:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $fillable = [
'title', 'description', 'status'
];
}
我的 table 在数据库端看起来像这样:
所以我真的不明白为什么这对我不起作用:(
这里有多个问题。
- 根据 Laravel 抛出的异常,您的数据库列
created-at
应该是created_at
的拼写错误:
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))
- 您在 json 响应中收到错误 "The title field is required" 的原因是您的 验证未通过 .
试试这个方法:
$validatedData = $request->validate([
'title' => 'required',
'description' => 'required',
]);
- 您的 POST 请求的应用程序类型也可能是错误的,尝试添加
Content-Type
-header 并将其设置为application/json
像这样
curl ... -H "Content-Type: application/json"
-
->json($data, 201)
是其他人建议的 HTTP 响应代码,如 standard 中所述。