Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lars-Janssen's avatar

Relations won't work

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!

0 likes
5 replies
sid405's avatar
sid405
Best Answer
Level 27

Try the inverse:

public function karakterrit() 
{ 
    return $this->belongsTo('App\karakterrit'); 
}
Lars-Janssen's avatar

Thanks for your reply. But I already tried that. Unfortunately it doesn't change anything.

sid405's avatar

What is the error you get on the inverse?

Lars-Janssen's avatar

Wow thanks I did it wrong I did put it in the model karakterrit. Now it works! Thankyou.

Please or to participate in this conversation.