【问题标题】:laravel model relation not workinglaravel 模型关系不起作用
【发布时间】:2016-05-01 17:23:19
【问题描述】:

我为我的应用程序创建了一个 laravel api。我为不同的模块使用了 Pingpong 模块包。我很难建立多对多关系。我有 3 个表:roles,groups,group_roles。我的模型是:

组.php

namespace Modules\User\Entities;
use Illuminate\Database\Eloquent\Model;
class Group extends Model {
   protected $fillable = [];
   protected $table='groups';

   public static function roles(){
      return $this->belongsToMany('Modules\User\Entities\Role','group_roles','group_id','role_id');
   }
}

角色.php

namespace Modules\User\Entities;
use Illuminate\Database\Eloquent\Model;
class Role extends Model {
   protected $fillable = [];
   protected $table='roles';
   public function groups(){
      return $this->belongsToMany('Modules\User\Entities\Group','group_roles','group_id','role_id');
   }
}

还有我的控制器

namespace Modules\User\Http\Controllers;
use Pingpong\Modules\Routing\Controller;
use Modules\User\Entities\Group;
use Modules\User\Entities\Role;

use Illuminate\Http\Request;
use App\Login;

use Input; 
use Validator;
use Hash;
use Response;

class UserController extends Controller {
   public function getGroupById(Request $request){
      $groups=Group::with('roles')->get();
      return Response::json ([
            'status'=>'ok',
            'group'=>$groups
      ],200);
   }   
}

问题是我无法建立模型之间的关系,getGroupById 返回 500 内部错误响应。$group=Group::all(); $group=Group::find($request['id']); 返回正常,但未返回相关角色。

类似的结构和代码在不使用乒乓球的应用上也能正常工作。

【问题讨论】:

    标签: laravel orm eloquent many-to-many


    【解决方案1】:

    虽然 haakym 的回答非常详细,但您也可以尝试将映射表名称更改为基于约定的“group_role”而不是“group_roles”。使用此方法,您只需为 belongsToMany 调用提供一个参数。

    请注意,通常其他参数是否正确无关紧要,但这只是调试的另一个步骤!

    【讨论】:

      【解决方案2】:

      你们的关系目前是这样的:

      // not sure why this is static?
      public static function roles(){
          return $this->belongsToMany('Modules\User\Entities\Role', 'group_roles', 'group_id', 'role_id');
      }
      
      public function groups(){
          return $this->belongsToMany('Modules\User\Entities\Group', 'group_roles', 'group_id', 'role_id');
      }
      

      请注意文档中关于belongsToMany 方法的信息:

      第三个参数是您要定义关系的模型的外键名称,而第四个参数是您要加入的模型的外键名称...

      因此,考虑到这一点,我认为您的关系可能不正确,因为在您的 belongsToMany 方法调用中使用了错误的参数。我觉得应该是这样的:

      public function roles(){
          return $this->belongsToMany('Modules\User\Entities\Role', 'group_roles', 'group_id', 'role_id');
      }
      
      public function groups(){
          return $this->belongsToMany('Modules\User\Entities\Group', 'group_roles', 'role_id', 'group_id');
      }
      

      此外,如果您有中间表列,则需要在 belongsToMany 调用中声明这些列。

      希望有帮助!

      编辑

      首先,您说 getGroupById 返回 500 内部错误响应。您是否尝试过检查实际错误是什么!? 500 内部错误并没有提供太多信息,如果您通过 laravel 的通常错误响应页面找到确切的问题,我相信您会更快地了解事情的真相。

      我假设您是通过 ajax 请求执行此操作的,因此如果您使用的是 chrome,则可以使用网络选项卡,然后单击 500 请求以查看 laravel 返回的错误,或者您可以使用 postman 之类的东西并点击通过那个网址。

      如果我想快速检查模型关系方法的功能,我会执行以下操作:

      在为组和关系设置一些数据后,您可以尝试在 tinker 或路由中运行它以进行测试/调试。

      $g = Group::first(); // get the first group, or you could use find($id) if you had a specific group in mind
      
      // if you're in tinker
      $g->roles; // show the roles
      
      // if you're running on a route
      dd($g->roles); // show the roles
      

      【讨论】:

      • 好的,我确信这可能仍然是个问题。我会用一些提示来更新我的答案以尝试调试。
      猜你喜欢
      • 2019-03-25
      • 2018-01-10
      • 2014-01-09
      • 2015-03-11
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 2015-09-24
      • 2017-07-15
      相关资源
      最近更新 更多