Laravel-mongodb: is there's a way to implement mongodb "DBrefs" with this library?

Created on 20 Aug 2015  路  2Comments  路  Source: jenssegers/laravel-mongodb

hey there, is there's a way to implement mongodb "DBrefs" with this library? thanks a lot :)

Most helpful comment

Use
use MongoDB\BSON\ObjectId;
After all you, need to create DBRef struct, like mongodb doc

public function store(Request $request)
    {
        $data = $request->all();
        if($data['id_role']){
            $role = [
                '$ref' => 'role',
                '$id' => new ObjectId($dados['id_role'])
            ];
            unset($data['id_role']);
            $data['role'] = $role;
        }
        return User::create($data);
    }

Example of my controller, but you will need to load the DBRefs manually on show.

public function show($id)
    {
        $user = User::find($id);
        if($user){
            $user->role = Role::find($user->role['$id']);
            return $user;
        }

        return response('{"message":"NOT FOUND"}', 404);
    }

All 2 comments

i have the same question.

Use
use MongoDB\BSON\ObjectId;
After all you, need to create DBRef struct, like mongodb doc

public function store(Request $request)
    {
        $data = $request->all();
        if($data['id_role']){
            $role = [
                '$ref' => 'role',
                '$id' => new ObjectId($dados['id_role'])
            ];
            unset($data['id_role']);
            $data['role'] = $role;
        }
        return User::create($data);
    }

Example of my controller, but you will need to load the DBRefs manually on show.

public function show($id)
    {
        $user = User::find($id);
        if($user){
            $user->role = Role::find($user->role['$id']);
            return $user;
        }

        return response('{"message":"NOT FOUND"}', 404);
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

geofflancaster picture geofflancaster  路  3Comments

imrannazirbhat picture imrannazirbhat  路  3Comments

pirmax picture pirmax  路  3Comments

kschethan picture kschethan  路  3Comments

lgt picture lgt  路  3Comments