【问题标题】:SQLSTATE[22P02]: Invalid text representation errorSQLSTATE[22P02]:无效的文本表示错误
【发布时间】:2018-10-02 09:30:42
【问题描述】:

无法弄清楚为什么我会收到此错误?我正在使用 posgres 我觉得我错过了一些重要的链接来解决整个问题? 基本上我需要将评论链接到带有 id 的博客文章

SQLSTATE[22P02]:文本表示无效:7 错误:输入无效 整数语法:"{blog}" (SQL: select * from "blogs" where "id" = {blog} 限制 1)

评论控制器

use App\Blog;
use App\Comment;

class CommentsController extends Controller
{
    // add store method
    public function store(Blog $blog)
    {
        Comment::create([
            'body'=> request('body'),
            'blog_id' => $blog->id
        ]);

        return back();
    }
}

web.php

Route::post('blog/{blog}/comments', 'CommentsController@store');

带有请求的表单头:

<form method="POST" action="/blog/{blog}/comments">

【问题讨论】:

  • 听起来{blog} 没有被转换成整数?可以查一下吗?
  • 你的意思是这样吗?

标签: php laravel


【解决方案1】:

正如您现在所拥有的,action="/blog/{blog}/comments"&gt; 将仅返回 /blog/{blog}/comments 作为操作,因为 Blade 不解析 {blog}。

为您的路线命名:

Route::post('blog/{blog}/comments', 'CommentsController@store')->name('blogcomments');

在您的 &lt;form&gt; 中,将操作设置为命名路由 (blogcmets),并将当前的 $blog 作为参数:

<form method="POST" action="{{ route('blogcomments', ['blog' => $blog] }}">

在https://laravel.com/docs/5.7/routing#named-routes阅读有关命名路由和参数的更多信息

未经测试! ;)

【讨论】:

    猜你喜欢
    • 2013-07-22
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2020-10-01
    • 2012-12-21
    • 1970-01-01
    相关资源
    最近更新 更多