Skip to content

Join Other table #1691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tolgatasci opened this issue Jan 10, 2019 · 3 comments
Closed

Join Other table #1691

tolgatasci opened this issue Jan 10, 2019 · 3 comments

Comments

@tolgatasci
Copy link

{ 
    "_id" : ObjectId("5c3667e09dc6d6646047a97b"), 
    "name" : "Deep Down", 
    "description" : null, 
    "image" : null, 
    "artist" : null, 
    "musics" : [
        "5c3529e79dc6d63f314976ed", 
        "5c3529e79dc6d63f314976f2", 
        "5c3529e79dc6d63f314976fa", 
        "5c3529e79dc6d63f31497700"
    ], 
    "updated_at" : ISODate("2019-01-09T21:30:08.000+0000"), 
    "created_at" : ISODate("2019-01-09T21:30:08.000+0000")
}

i need musics id get music table info

<?php

namespace App;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Album extends Eloquent
{
    protected $connection = 'mongodb';
    protected $collection = 'tt_albums';

    protected $fillable = [
        'name','image','description','musics','artist'
    ];
    public function musics()
    {
        return $this->hasMany('App\Music','musics','_id');
    }

}
@skasidej
Copy link

We're definitely running into the same issue now. I temporarily create a mutator function and use whereIn() by accepting ids as parameter, in this case your musics' id.

I'd love to know how to properly use relationships, so if anyone happen to stumble upon this and got a way out, please do share.

@tolgatasci
Copy link
Author

I have tried such a solution. I don't know how good the performance is. thank you so much. I moved on with your suggestion.

<?php

namespace App;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Album extends Eloquent
{
    protected $connection = 'mongodb';
    protected $collection = 'tt_albums';
    protected $fillable = [
        'name','image','description','musics','artist'
    ];
    public function __destruct()
    {

        var_dump(\DB::getQueryLog());

    }

    public function import($model,$foreign_key){
        $datas = (array)$this->{$foreign_key};
        $ids = array();
        foreach($datas as $data):
            $data = (array) $data;

            $ids[] = $data['oid'];
        endforeach;

        $foreign = $model::whereIn('_id',$ids)->get();

        $this->{$foreign_key} = $foreign;
        $this->attributes[$foreign_key] = $foreign;
        return $this;
    }


}

calling

  $album = Album::find(request()->query('id'))->import('\App\Music','musics');
    dd($album);

@zishiguo
Copy link

has solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants