Igor Pavlenko
Igor Pavlenko

Reputation: 707

SQLSTATE[22P02]: Invalid text representation error

Can't figure out why I am getting this error? I am using posgres I feel like I am missing some important link to figure this whole thing out ? Basically I need to link the comment to the blog post with the id

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "{blog}" (SQL: select * from "blogs" where "id" = {blog} limit 1)

Comments controller

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 head with request:

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

Upvotes: -1

Views: 2534

Answers (1)

user8034901
user8034901

Reputation:

As you have it now, action="/blog/{blog}/comments"> will return just /blog/{blog}/comments as action, since {blog} is not parsed by Blade.

Give your route a name:

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

In your <form> set the action to the named route (blogcomments), giving it the current $blog as parameter:

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

Read more on named routes and parameters at https://laravel.com/docs/5.7/routing#named-routes

Not tested! ;)

Upvotes: 3

Related Questions