【问题标题】:SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integerSQLSTATE [22P02]:无效的文本表示:7 错误:整数的输入语法无效
【发布时间】:2023-04-09 17:18:02
【问题描述】:

我登录了我的 Heroku 应用程序,这是一个酒店评论应用程序:http://immense-beach-76879.herokuapp.com/。显然,它不会显示我在http://immense-beach-76879.herokuapp.com/reviews 输入的数据。它只是显示错误。它说整数是错误的选择,因为“kiki”应该是一个字符串,对吗?如果你需要看我的代码,这里是:https://github.com/kikidesignnet/hotelreviews

这是我的错误:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "kiki" (SQL: select * from "reviews" where "user_id" in (1, kiki, k@k.com, ?, 2020-02-07 05:57:47, 2020-02-07 05:57:47) order by "created_at" desc limit 20)

我一直在关注本教程以了解 Laravel 以及 React/Laravel 如何协同工作:https://kaloraat.com/articles/laravel-react-crud-tutorial 和他们的 github 存储库:https://github.com/kaloraat/laravel-react-crud

这是我的迁移create_reviews_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateReviewsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('reviews', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->index();
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('reviews');
    }
}

如您所见,这个应用程序应该注册一个新用户。然后当用户登录并提交对酒店的评论时,reviews.api 应该将数据保存在并在评论表单下方显示评论。

我只能看到表单正在工作,正在提交下面的数据但它没有显示任何数据......

【问题讨论】:

  • SELECT 查询看起来明显错误,但您的代码不包含构建该查询的任何片段。请分享您的代码的正确部分,以及您的调试尝试
  • 请张贴此错误指向的行
  • 这是我的 github 仓库:github.com/kikidesignnet/hotelreviews
  • @TsaiKoga 怎么样?我不知道错误是什么。

标签: php database laravel


【解决方案1】:

您可以在 App\Http\Controllers\ReviewController 内的 index 方法中更改此行 (17)

$allReviews = $review->whereIn('user_id', $request->user())->with('user');

$allReviews = $review->where('user_id', $request->user()->id)->with('user');

或者

$allReviews = $review->where('user_id', \Auth::id())->with('user');

【讨论】:

  • 非常感谢!是不是whereIn 导致了问题?
  • 是的,WhereIn 方法在其第二个参数中接受数组,但是 $request->user() 返回一个对象(当前经过身份验证的用户)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
  • 2012-12-21
  • 1970-01-01
相关资源
最近更新 更多