Try the inverse:
public function karakterrit()
{
return $this->belongsTo('App\karakterrit');
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have currently three tables/models:
-User
-Rideregistration (it is for keeping track of the rides you make with your car)
-Karakterrit (it has two values (private and business) so it tells the Rideregistraion if the ride was for private or business purpose).
So a User has multiple Rideregistration and Rideregistration has one Karakterrit.
In my Ridesregistration I've declared the relation between the Karakterrit like this:
public function karakterrit() { return $this->hasOne('App\karakterrit'); }
When I want to search for a specific ride from a user I do that like this:
$rideregistration = Rideregistration:whereId($id)->firstorFail();
So when I pass this to a view and say:
-{!! $rideregistration-> id !!} -{!! $rideregistration->from !!} -{!! $rideregistration->to !!} -{!! $rideregistration->karakterrit()->first()->kind !!}
I receive the error:
ErrorException in Connection.php line 636:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'karakterrit.rideregistration_id' in 'where clause' (SQL: select * from karakterrit where karakterrit.rideregistration_id = 3 and karakterrit.rideregistration_id is not null limit 1) (View: D:\wamp\www\resources\views\ritten\rit.blade.php)
What am I doing wrong here? When I created the tables with migrations I did it like this:
Rideregistration:
public function up() { Schema::create('Rideregistration', function (Blueprint $table) { $table->increments('id'); $table->integer('karakterrit_id')->unsigned(); $table->text('from'); $table->text('to');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('karakterrit_id')
->references('id')
->on('karakterrit')
->onDelete('cascade');
});
}
Karakterrit:
public function up()
{
Schema::create('karakterrit',function(Blueprint $table)
{
$table->increments('id');
$table->text('kind'');
});
}
Can somebody help me please!
Try the inverse:
public function karakterrit()
{
return $this->belongsTo('App\karakterrit');
}
Please or to participate in this conversation.