首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 9不向MySQL发布动作

Laravel 9不向MySQL发布动作
EN

Stack Overflow用户
提问于 2022-02-27 19:51:03
回答 1查看 89关注 0票数 0

我刚到拉勒维尔,遇到了一个问题。每当我执行表单请求时,Laravel都会给我一个表单的JSON响应,但不会发布到MySQL。我确实创建了另一个Laravel应用程序,但后来Laravel版本为8,我可以将我的数据上传到MySQL。我检查了我的代码十亿次,但我似乎不能区别,我不明白它没有发布。

// PostController@store

代码语言:javascript
复制
public function store(Request $request)
    {
        return $request->all();
        
        $this->validate($request, [
            'title' => 'required',
            'body' => 'required',
            'meta_description' => 'required',
            'slug' => 'required',
            'status' => 'required',
            'posted_by' => 'required',
            'image' => 'required',
            'like' => 'required',
            'dislike' => 'required'
        ]);

        if ($request->hasFile('image')) {
            $imageName = $request->image->store('public');
        }else{
            return 'No';
        }

        $slug2 = Str::slug($request->title, '-');

        $post = new Post;
        $post->meta_description = $request->input('meta_description');
        $post->title = $request->input('title');
        $post->body = $request->input('body');
        $post->status = $request->status;
        $post->slug = $slug2;
        $post->posted_by = 'Onur Sedef';
        $post->image = $imageName;
        $post->like = "1";
        $post->dislike = '0';
        $post->save();
    }

//数据库迁移

代码语言:javascript
复制
Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('title', 255);
            $table->mediumText('body');
            $table->mediumText('meta_description', 150);
            $table->string('slug', 120)->nullable();
            $table->boolean('status')->nullable();
            $table->integer('posted_by')->nullable();
            $table->string('image')->nullable();
            $table->integer('like')->nullable();
            $table->integer('dislike')->nullable();
        });

// JSON响应

代码语言:javascript
复制
{
  "_token": "I6v9cZmUaPKQsCINj6CvNcJYqEU6ERbEj6P9tTyk",
  "title": "title",
  "meta_description": "meta",
  "body": "<h1><u>Heading Of Message</u></h1>\r\n<h4>Subheading</h4>\r\n<p>But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure? On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee</p>\r\n<ul>\r\n<li>List item one</li>\r\n<li>List item two</li>\r\n<li>List item three</li>\r\n<li>List item four</li>\r\n</ul>\r\n<p>Thank you,</p>\r\n<p>John Doe</p>",
  "category": "Deneme",
  "tag": "tag",
  "status": "1",
  "image": {
    
  }
}

编辑:问题不是拉拉维尔的版本,但我似乎还是找不到。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-27 22:46:19

因此,再过几个小时,我发现问题出在PostController@store中,因为空字段没有发到MySQL。Laravel将$post->posted_by = "Onur Sedef";视为空字段,并且由于它是必需字段,所以不会将其发布到MySQL。我的index.blade.php文件中有一个错误捕捉器,但是它属于一个老版本的Laravel,所以它不起作用,这就是为什么我这么挣扎的原因。在将session()->has('error)更改为($errors->any())之后,我捕获了该错误。谢谢大家宝贵的意见。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71287993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档