"json()"中的第二个参数是什么?做(流明/Laravel)?

编程入门 行业动态 更新时间:2024-10-28 03:28:01
本文介绍了"json()"中的第二个参数是什么?做(流明/Laravel)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

因此,我是按照本教程进行操作的,我在第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 :(

推荐答案

这里有多个问题.

  • 根据Laravel抛出的异常,您的db列的拼写错误created-at应该为created_at:
  • You have a misspelling of your db column created-at should be created_at as per the Exception thrown by Laravel:
  • 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))

  • 在json响应中出现错误标题字段为必填项" 的原因是,您的验证未通过.
  • The reason you get the errors "The title field is required" in your json response is because your validation does not pass.
  • 尝试使用这种方法:

    $validatedData = $request->validate([ 'title' => 'required', 'description' => 'required', ]);

  • 您的POST请求的应用程序类型也可能是错误的,请尝试添加Content-Type -header并将其设置为application/json,就像这样
  • The application type of your POST request is likely also wrong, try adding the Content-Type-header and set it to application/json like so
  • curl ... -H "Content-Type: application/json"

  • ->json($data, 201)与其他建议一样,如标准.
  • The ->json($data, 201) is as the others suggest the HTTP Response Code as described in the standard.
  • 更多推荐

    "json()"中的第二个参数是什么?做(流明/Laravel)?

    本文发布于:2023-10-24 02:50:33,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1522725.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:第二个   参数   quot   json   Laravel

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!