Call to a member function getForeignKeyName() on null

I dont know why it's not working, I have the main table access_permits structure
id
permit_id
type
user_relationship_id
vehicle_relationship_id
request_date
expire_date
status
created_at
updated_at
id
permit_id
type
user_relationship_id
vehicle_relationship_id
request_date
expire_date
status
created_at
updated_at
and this is the other table access_permit_vehicles structure:
id
access_permit_id
vehicle_id
created_at
updated_at
id
access_permit_id
vehicle_id
created_at
updated_at
5 Replies
SoraKeyheart
SoraKeyheartOP6d ago
these are the models Vehicle:
public function accessPermits(): BelongsToMany
{
return $this->belongsToMany(AccessPermit::class, 'access_permit_vehicles', 'vehicle_id', 'access_permit_id');
}
public function accessPermits(): BelongsToMany
{
return $this->belongsToMany(AccessPermit::class, 'access_permit_vehicles', 'vehicle_id', 'access_permit_id');
}
AccessPermit:
public function vehiclesRelationship(): BelongsTo
{
return $this->belongsTo(Vehicle::class);
}

public function vehicles(): BelongsToMany
{
return $this->belongsToMany(Vehicle::class, 'access_permit_vehicles', 'access_permit_id', 'vehicle_id');
}
public function vehiclesRelationship(): BelongsTo
{
return $this->belongsTo(Vehicle::class);
}

public function vehicles(): BelongsToMany
{
return $this->belongsToMany(Vehicle::class, 'access_permit_vehicles', 'access_permit_id', 'vehicle_id');
}
and this is the migration for access_permit_vehicles:
Schema::create('access_permit_vehicles', function (Blueprint $table) {
$table->id();
$table->foreignId('access_permit_id')->constrained('access_permits')->onDelete('cascade');
$table->foreignId('vehicle_id')->constrained('vehicles')->onDelete('cascade');

$table->timestamps();
});
Schema::create('access_permit_vehicles', function (Blueprint $table) {
$table->id();
$table->foreignId('access_permit_id')->constrained('access_permits')->onDelete('cascade');
$table->foreignId('vehicle_id')->constrained('vehicles')->onDelete('cascade');

$table->timestamps();
});
this is for access_permits
Schema::create('access_permits', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('permit_id')->unique(); // Adjusted to unsignedInteger for proper ID type

// Other columns as defined
$table->string('type')->index();
$table->unsignedBigInteger('user_relationship_id');
$table->unsignedBigInteger('vehicle_relationship_id');
$table->dateTime('request_date');
$table->dateTime('expire_date');
$table->string('status')->default(\App\Enum\PermitStatus::Active);

$table->timestamps();

// Foreign key constraints
$table->foreign('user_relationship_id')->references('id')->on('users')->onDelete('cascade');
// Ensure the foreign key for vehicle_relationship_id if needed
$table->foreign('vehicle_relationship_id')->references('id')->on('vehicles')->onDelete('cascade');
});
Schema::create('access_permits', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('permit_id')->unique(); // Adjusted to unsignedInteger for proper ID type

// Other columns as defined
$table->string('type')->index();
$table->unsignedBigInteger('user_relationship_id');
$table->unsignedBigInteger('vehicle_relationship_id');
$table->dateTime('request_date');
$table->dateTime('expire_date');
$table->string('status')->default(\App\Enum\PermitStatus::Active);

$table->timestamps();

// Foreign key constraints
$table->foreign('user_relationship_id')->references('id')->on('users')->onDelete('cascade');
// Ensure the foreign key for vehicle_relationship_id if needed
$table->foreign('vehicle_relationship_id')->references('id')->on('vehicles')->onDelete('cascade');
});
awcodes
awcodes6d ago
Looks like your relationships are off based on laravel conventions.
SoraKeyheart
SoraKeyheartOP5d ago
May you kindly explain what this means?
Tuim
Tuim5d ago
Eloquent determines the foreign key name by examining the name of the relationship method and suffixing the method name with _id.
Tuim
Tuim5d ago
If you stick to the conventions of model classnames, relationship names and the proper column names it should work out without too much hassle. Read up on the documentation here > https://laravel.com/docs/11.x/eloquent-relationships#defining-relationships
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Want results from more Discord servers?
Add your server