简体   繁体   English

当我尝试在 Invoice 表上添加外键时,为什么会出现此错误?

[英]Why am i getting this error when i try to add foreign keys on Invoice table?

I have to tables: users and invoices this is the up function for the users migration:我必须表:用户和发票这是用户迁移的 function:

    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->string('email')->unique();
            $table->string('phone')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('comentarii');

            $table->rememberToken();
            $table->timestamps();
        });
    }

and this is the up function for the invoices这是发票的 function

 public function up()
    {
        Schema::create('invoices', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('user_id');
            $table->foreign('user_id')->refferences('id')->on('users');
            $table->integer('InvoiceNumber');
            $table->dateTime('date');
            $table->string('serviceInvoiced');
            
            $table->timestamps();
        });
    }

All i am trying to do is to make a one to many relationship as in a user can have multiple invoices我要做的就是建立一对多的关系,因为用户可以有多个发票

here is the User model:这是用户 model:


namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $guarded = [];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function getInvoices()
    {
        return $this->hasMany(Invoice::class);
    }
}

and here is the Invoice model:这是发票 model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Invoice extends Model
{
    use HasFactory;

    protected $guarded=[];

    public function getUsers()
    {
        return $this->belongsTo(User::class);
    }
}

What am i doing wrong?我究竟做错了什么? i watched multiple tutorials alrerady.. and here are the errors that im getting:我已经看了多个教程..这是我得到的错误:

errors错误

The columns need to be of the same type.列必须是相同的类型。 id() is an alias of bigIncrements() , so id()bigIncrements()的别名,所以

$table->unsignedInteger('user_id');

should be应该

$table->unsignedBigInteger('user_id'); 

Also note: it's ->references('id') , not ->refferences('id')另请注意:它是->references('id') ,而不是->refferences('id')

More on Available Column Types有关 可用列类型的更多信息

you have tow issues:你有两个问题:

1- you have a typo in your migration statement 1-您的迁移声明中有错字

2- $this->id() make unsignedBigInteger so user_id should be unsignedBigInteger 2- $this->id()使unsignedBigInteger所以user_id应该是unsignedBigInteger

alter you migration lines to this:将您的迁移行更改为:

 $table->unsignedBigInteger('user_id');
 $table->foreign('user_id')->references('id')->on('users'); //references not refferences 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我尝试删除帖子时出现此错误? - Why am i getting this error when i try to delete a post? 尝试启动连接时为什么会出现NULL? - Why am I getting NULL when I try to initiate a connection? 尝试打开Materialize Modal时为什么会出现此错误? - Why am I getting this error when I try to open Materialize Modal? 为什么我在尝试运行 cron 作业时收到 mysql 扩展错误? - Why am I getting mysql extension error when I try to run a cron job? 当我尝试将PHP应用程序部署到Google App Engine时,为什么突然出现400错误? - When I try to deploy my PHP application to Google App Engine, why am I suddenly getting a 400 error? 为什么会出现此错误 - Why am I getting this error 我正在使用db forge进行数据库迁移。add_foreign_key将密钥从第一个创建的表添加到第二个表 - I am using db forge for database migration.add_foreign_key add keys from first created table to second one 尝试通过脚本传递POST变量时,为什么没有收到响应? - Why am I not getting a response when I try to pass POST vars via script? 为什么我在尝试计算 mysqli 结果时会收到警告? - Why am I getting a warning when I try to count a mysqli result? 为什么当我尝试在 php 文件中使用 curl 时会被禁止 - why am i getting Forbidden when i try to use curl in php file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM