Laravel belongsTo not working

2019-01-25 02:59发布

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

5条回答
放我归山
2楼-- · 2019-01-25 03:14

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');
    }
}
查看更多
趁早两清
3楼-- · 2019-01-25 03:14

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

查看更多
太酷不给撩
4楼-- · 2019-01-25 03:19

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');
   }
查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-25 03:20

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 ;)

查看更多
SAY GOODBYE
6楼-- · 2019-01-25 03:22

In my case the related models data was deleted & laravel don't get soft deleted data in general query. To get soft deleted data you've to use "withTrashed() or onlyTrashed()".

You can check the documentation here.

https://laravel.com/docs/5.6/scout#soft-deleting

查看更多
登录 后发表回答