Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

    {
        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

 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

here is the User 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:

<?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

>Solution :

The columns need to be of the same type. id() is an alias of bigIncrements(), so

$table->unsignedInteger('user_id');

should be

$table->unsignedBigInteger('user_id'); 

Also note: it’s ->references('id'), not ->refferences('id')

More on Available Column Types

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading