4ndro1d 4ndro1d - 1 year ago 97
PHP Question

Dealing with Eloquent relationships in Laravel 5.3

I have the following tables created using


Schema::create('typen', function (Blueprint $table) {

Schema::create('auktionen', function (Blueprint $table) {


The table
is only created once at contains fixed values:

id| name
1 | Typ 1
2 | Typ 2

Those values are constant, no more will be added during the application lifetime. So every
I create should be associated with one
I created the model for
and thought of it as a one-to-one relationship (is this correct?).

I would like to create an Auktion with a given Typ using Eloquent queries like this:

$thetyp = App\Typ::where("id", "=", 1)->first();

and fetch the
of the
of my


Currently my model looks like this for

public function typ()
return $this->hasOne('App\Typ', 'typ');

which is not working. I already tried setting different relationships but I just end in different error codes ranging from undefined methods
to an error where an SQL statement failed trying to update my
table (when using
- which I really do not want to).

Can someone clarify this problem for me and explain which relationship I have to use?

EDIT1: As mentioned in a comment I already tried using

public function typ()
return $this->belongsTo('App\Typ', 'typ');

which results in
Undefined property: App\Auktion::$typ
when calling

$thetyp = App\Typ::where("id", "=", 1)->first();

and when calling

Trying to get property of non-object


I just figured that

echo $auktion->typ()->first()->name;

is indeed working. But referring to this answer this should be the same as

echo $auktion->typ->name;

What exactly am i doing wrong?


I tried using suggested code:

$thetyp = App\Typ::find($typ);

After I navigated to the view ehere I run the code I got this:

enter image description here

I got this the second time today, somwhow out of nowhere

Answer Source

Here is some code enhancement:

 $thetyp = App\Typ::where("id", "=", 1)->first();


//Will return null if Model is not found
$thetyp = App\Typ::find(1);
//You actually have to retrieve the relationship, because calling typ()
//will only retrieve the Relationship query

The problem is that the relationship is defined backwards. You need to make Auction belongTo Type, and change Type to hasMany Auctions. The statement would read:

"A Type has many Auctions. An Auction has one Type".

Here are the classes (in English, sorry, my German is bad :( so I just did it in English) with the migrations:

-Auction class:

class Auction extends Model
    protected $table = 'auction';

    public function type()
        return $this->belongsTo('App\Type');

-Auction migration:

Schema::create('auction', function(Blueprint $table){




-Type class:

class Type extends Model
    protected $table = 'type';

    public function auction()
        return $this->hasMany('App\Auction');

-Type migration:

 Schema::create('type', function(Blueprint $table){



First, you can create a Type object (or insert it with a query) so we can have a Type row that we can relate to an Auction object/entry, and do the following:

//Example of Type obj creation

$type = new Type();
$type->name = 'Type #1';
//Don't forget to save

//OR find/retrieve a Type obj

//Where $id is the id of the Type obj, else the obj will be null
$type = Type::find($id);

$auction = new Auction();

$auction->title = 'Title #1';
$auction->description = 'Test description';

$auction->type_id = $type->id;

//Don't forget to save

Now later in your code, whenever you are using an Auction object and you want to retrieve the associated type (if any), you can use:

$type = $auction->type()->get();

Which will return the instance of Type, and you will be able to retrieve the property name like so:


I hope this helps! Let me know if you have any more questions!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download