[英]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:
我已经看了多个教程..这是我得到的错误:
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.