小编por*_*s Ψ的帖子

当 $request->validate 失败时 Laravel 抛出 404

我们正在为我们的应用程序创建一个 REST API。

网页.php

Route::post('add-new-student', 'StudentController@addNewStudent');
Run Code Online (Sandbox Code Playgroud)

学生控制器.php

Route::post('add-new-student', 'StudentController@addNewStudent');
Run Code Online (Sandbox Code Playgroud)

现在,当我们发送 JSON 响应时,如果出现错误,它不会转到 else 块,而是在邮递员中显示 404 页面。

我们不知道自己做错了什么!

笔记

  • 我们的路线是正确的(双重检查)
  • 在邮递员中,我们正在发送邮寄请求

php validation rest laravel

8
推荐指数
2
解决办法
6140
查看次数

Laravel 8 外键

我尝试迁移具有外键的表。每次我迁移表时都会产生一个错误,内容如下:

一般错误:1215 无法添加外键约束

这是我的表迁移:

Schema::create('profile_pictures', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('user_id')->nullable();
    $table->binary('image')->nullable();
    $table->timestamps();

    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Run Code Online (Sandbox Code Playgroud)

这是我的模型:

class ProfilePicture extends Model
{
    protected $fillable = [
        'user_id',
        'image'
    ];

    public function user()
    {
        $this->belongsTo(User::class, 'user_id', 'id');
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的用户表迁移:

Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('username');
    $table->string('first_name')->nullable();
    $table->string('last_name')->nullable();
    $table->string('email')->unique();
    $table->string('phone')->nullable();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)

migration laravel laravel-8

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

laravel ×2

laravel-8 ×1

migration ×1

php ×1

rest ×1

validation ×1