Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel belongsTo not working

I have 2 models in my app, 'User' & 'MedicineType' (each User belongs to one MedicineType).

I made the one-to-many relation between two model using belongsTo() and hasMany(). hasMany() relation works perfectly but belongTo() doesn't work. Does anyone know where did I make a mistake?

User::find(1)->medicine_type [this returns nothing]

MedicineType::find(1)->users [this returns users]

Here's the code to Models:

class MedicineType extends Eloquent {

    public function users()
    {
        return $this->hasMany('User');
    }
}


class User extends Eloquent {

    public function medicine_type()
    {
        return $this->belongsTo('MedicineType');
    }
}

And here is my database structure:

users:
    id
    name
    medicine_type_id 

medicine_types:
    id
    name
like image 994
Yashari Avatar asked Jun 28 '14 17:06

Yashari


People also ask

How to define relationship in laravel?

To define a relationship, we need first to define the post() method in User model. In the post() method, we need to implement the hasOne() method that returns the result. Let's understand the one to one relationship through an example. First, we add the new column (user_id) in an existing table named as posts.

What is belongsTo in laravel?

BelongsTo relationship in laravel is used to create the relation between two tables. belongsTo means create the relation one to one in inverse direction or its opposite of hasOne. For example if a user has a profile and we wanted to get profile with the user details then we can use belongsTo relationship.

What is polymorphic relation in laravel?

A one-to-one polymorphic relationship is a situation where one model can belong to more than one type of model but on only one association. A typical example of this is featured images on a post and an avatar for a user. The only thing that changes however is how we get the associated model by using morphOne instead.


4 Answers

The reason your relation is not working is not because of the relations specified in the model, but because of the method naming in the User model and not specifying the foreign key.

Instead of:

public function medicine_type()
{
    return $this->belongsTo('MedicineType');
}

Use:

public function medicineType()
{
    return $this->belongsTo('MedicineType', 'id');
}

I hope this works for you ;)

Everything together:

<?php // app/models/MedicineType.php

class MedicineType extends Eloquent {

   // Determines which database table to use
   protected $table = 'medicine_types';

   public function users() 
   {
      return $this->hasMany('User');
   }

}

and:

<?php // app/models/User.php

class User extends Eloquent {

   // Determines which database table to use
   protected $table = 'users';

   public function medicineType() 
   {
      return $this->belongsTo('MedicineType', 'id');
   }

}

Testing if it works:

$user = User::find(1);
return $user->medicineType->name;

This successfully returns the related medicine_type's name.

I hope this helps you further ;)

like image 144
Melvin Koopmans Avatar answered Oct 07 '22 09:10

Melvin Koopmans


Maybe there's an issue with Eloquent finding the foreign key. Try this:

class User extends Eloquent {

    public function medicine_type()
    {
        return $this->belongsTo('MedicineType', 'medicine_type_id');
    }
}

EDIT:

Also, Eloquent tries to find the table "medicinetypes" and not "medecine_types", so you need to specify that as well using the $table variable.

class MedicineType extends Eloquent {
    protected $table = 'medicine_types';

    public function users()
    {
        return $this->hasMany('User');
    }
}
like image 29
Joseph Avatar answered Oct 07 '22 08:10

Joseph


I made the stupid mistake of not adding the "return" in the relationship method!

Make sure you return the relationship... Obviously this will not work:

public function medicineType() 
   {
      $this->belongsTo('MedicineType', 'id');
   }
like image 39
HPage Avatar answered Oct 07 '22 08:10

HPage


I change "medicine_type" to "medicineType" and everythings got OK...

like image 1
Yashari Avatar answered Oct 07 '22 08:10

Yashari