dongyi6668 2017-01-04 11:16
浏览 84
已采纳

在Laravel 5.3 + MongoDB库'jenssegers / laravel-mongodb'中有很多关系问题

I am using MongoDB library in Laravel 5.3. I have two collections in MongoDB and I want to make a hasMany relation b/w them.

MongoDB:

1st Collection: Employee

{
    "_id" : ObjectId("586ca8c71a72cb07a681566d"),
    "employee_name" : "John",
    "employee_description" : "test description",
    "employee_email" : "john@email.com",
    "updated_at" : "2017-01-04 11:45:20",
    "created_at" : "2017-01-04 11:45:20"
},
{
    "_id" : ObjectId("586ca8d31a72cb07a6815671"),
    "employee_name" : "Carlos",
    "employee_description" : "test description",
    "employee_email" : "carlos@email.com",
    "updated_at" : "2017-01-04 11:45:20",
    "created_at" : "2017-01-04 11:45:20"
}

2nd Collection: Task

{
    "_id" : ObjectId("586ccbcf1a72cb07a6815b04"),
    "task_name" : "New Task",
    "task_description" : "test description",
    "task_status" : 1,
    "task_created_at" : "2017-04-01 02:17:00",
    "task_updated_at" : "2017-04-01 02:17:00"
},
{
    "_id" : ObjectId("586cd3261a72cb07a6815c69"),
    "task_name" : "2nd Task",
    "task_description" : "test description",
    "task_status" : 1,
    "task_created_at" : "2017-04-01 02:17:00",
    "task_updated_at" : "2017-04-01 02:17:00"
}

I have created pivot table between them

Employee_Task

{
    "_id" : ObjectId("586cd0cb1a72cb07a6815bf3"),
    "employee_task_employee_id" : "586ca8c71a72cb07a681566d",
    "employee_task_task_id" : "586ccbcf1a72cb07a6815b04",
    "status" : 1
},
{
    "_id" : ObjectId("586cd7851a72cb07a6815d7d"),
    "employee_task_employee_id" : "586ca8c71a72cb07a681566d",
    "employee_task_task_id" : "586cd3261a72cb07a6815c69",
    "status" : 1
}

Laravel:

Model:

Employee:

<?php
namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Employee extends Eloquent {

    protected $collection = 'employee';
    protected $primaryKey = '_id';

    public function tasks()
    {
        return $this->hasMany('App\Models\Task');
    }
}

Task:

<?php
namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Task extends Eloquent {

    protected $collection = 'task';
    protected $primaryKey = '_id';

    public function employees()
    {
        return $this->belongsToMany('App\Models\Employee');
    }
}   

Controller:

public function EmployeeData($data)
{
    $employee= Employee::find('586ca8c71a72cb07a681566d')->tasks;
    echo "<pre>";
    print_r($employee);
}

When I want to see task against employee it shows me below output:

Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
        )

)

How can I fix this?

  • 写回答

2条回答 默认 最新

  • duanquanhan2333 2017-01-04 22:43
    关注

    in Mongo Eloquent when creating Many to Many relationships you dont need to have a pivot table, thats SQL mindset, in mongo-eloquent many to many relations the foreign keys are stored in arrays. So the models should look like this:

    <?php namespace App\Models;
    
    use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
    
    class Employee extends Eloquent {
    
        protected $collection = 'employee';
        protected $primaryKey = '_id';
    
        public function tasks()
        {
            return $this->belongsToMany('App\Models\Task');
        }
    }
    
    
    
    
    
    <?php namespace App\Models;
    
    use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
    
    class Task extends Eloquent {
    
        protected $collection = 'task';
        protected $primaryKey = '_id';
    
        public function employees()
        {
            return $this->belongsToMany('App\Models\Employee');
        }
    }  
    

    Also you should load the relations before trying to retrieve them

     $employee= Employee::with('tasks')->find('586ca8c71a72cb07a681566d')->tasks;
    

    You can save the relation the same way you do it in the hasMany relation

    $employee->tasks()->save(new Task());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开