"json()"中的第二个参数是什么?做(流明/Laravel)? [英] What does the second paremeter in "json()" do (Lumen/Laravel)?

查看:67
本文介绍了"json()"中的第二个参数是什么?做(流明/Laravel)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我是按照本教程进行操作的,我在第27分钟显示 https://www.youtube.com /watch?v = 6Oxfb_HNY0U 在那里,控制器的代码如下:

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);

    }
}

现在让我感到困惑的是以下行中的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请求的历史记录日志中: https://imgur.com/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: https://imgur.com/To0Y6cJ

当我有以下几行时:

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

未发表评论,那么我总是得到以下答复: https://imgur.com/wTtZNrz

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"
}

当我评论这些行时,就会出现此错误: https://textuploader.com/1oq3n

When I comment these lines, then I get this error: https://textuploader.com/1oq3n

SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'created_at'(SQL:插入articles(updated_atcreated_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:

https://imgur.com/GpnNBIH

所以我真的看不出为什么这对我不起作用:(

So I don't really see any reason why this won't work for me :(

推荐答案

这里有多个问题.

  1. 根据Laravel抛出的异常,您的db列的拼写错误created-at应该为created_at:
  1. 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_atcreated_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))

  1. 在json响应中出现错误标题字段为必填项" 的原因是,您的验证未通过.
  1. 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',
]);

  1. 您的POST请求的应用程序类型也可能是错误的,请尝试添加Content-Type -header并将其设置为application/json,就像这样
  1. 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"

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

这篇关于"json()"中的第二个参数是什么?做(流明/Laravel)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆