【问题标题】:What does the second paremeter in "json()" do (Lumen/Laravel)?“json()”中的第二个参数有什么作用(Lumen/Laravel)?
【发布时间】:2019-11-25 14:52:14
【问题描述】:

所以我在关注本教程,我在第 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]:未找到列:1054 '字段列表'中的未知列'created_at'(SQL:插入articlesupdated_atcreated_at)值(2019-11-25 14:18 :33, 2019-11-25 14:18:33))

我无法直接发布此标记,因为这样我的正文将超过最大字符数。 所以请随意将其粘贴到 .html 文件中,然后在本地显示。抱歉给您带来不便...

我真的不明白为什么会收到此错误,因为我没有在我的 POST 请求中引用这些列。

Article eloquent-model 本身看起来像这样:

    <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Article extends Model
    {

        protected $fillable = [
            'title', 'description', 'status'
        ];

    }

我在 DB 端的表如下所示:

https://imgur.com/GpnNBIH

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

【问题讨论】:

标签: php laravel lumen


【解决方案1】:

这里有多个问题。

  1. 根据 Laravel 抛出的异常,您的 db 列 created-at 应为 created_at 拼写错误:

SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列'created_at'(SQL:插入articlesupdated_atcreated_at)值(2019-11-25 14:18 :33, 2019-11-25 14:18:33))

  1. 您在 json 响应中收到错误 "The title field is required" 的原因是您的验证未通过

改用这种方法:

$validatedData = $request->validate([
  'title' => 'required',
  'description' => 'required',
]);
  1. 您的 POST 请求的应用程序类型也可能有误,尝试添加 Content-Type-header 并将其设置为 application/json 像这样
curl ... -H "Content-Type: application/json"
  1. -&gt;json($data, 201) 与其他人建议的 HTTP 响应代码相同,如 standard 中所述。

【讨论】:

  • 哇,非常感谢!关于第三点,Content-Type-Header:我需要在哪里插入这行代码?另外,我需要在句号中插入什么?我曾经从 httpd.conf 和 php 文件配置了我的 apache 头文件。在这样的文件中: Header("Access-Control-Max-Age: 360");在 httpd.conf 中像这样 Header set Access-Control-Allow-Origin "localhost:8000"</IfModule> 我需要在哪里设置这个?在文件中?还是在 apache 端?或 php 端?
  • @baryon123 第 3 点是关于您在屏幕截图中看到的 RESTClient 发送 的 curl 命令。您需要编辑 client 以发送 Content-Type 标头,以便 Laravel 理解传入的数据。
  • 关于 RESTClient 中 Header 的设置,我找到了一个不错的 SO Thread,其中包含多个有用的答案,说明如何在此处进行操作 :=) stackoverflow.com/questions/13132794/…
猜你喜欢
  • 1970-01-01
  • 2015-08-02
  • 2017-05-02
  • 2013-09-29
  • 2013-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
相关资源
最近更新 更多