Laravel Jensenggers Eloquent Model Sort Model with Relationship Model

I am trying to sort the entire dataset of the main model through the column of the relational model. I am using Laravel ORM 5.2.43 and Jensenggers MongoDb 3.1

Here are the models that I have

UserEventActivity.php - Mongo Model

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class UserEventActivity extends Eloquent 
{

    protected $collection = 'user_event_activity';
    protected $connection = 'mongodb';

    public function handset() {

        return $this->hasOne('HandsetDetails', '_id', 'handset_id');
    }

    public function storeDetail() {

        return $this->hasOne('StoreDetails', 'st_id', 'store_id');
    }

}

HandsetDetails.php - Model Mongo

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class HandsetDetails extends Eloquent 
{

    var $collection = 'user_handset_details';
    var $connection = 'mongodb';

}

StoreDetails.php - MySql model

use Jenssegers\Mongodb\Eloquent\HybridRelations;
use Illuminate\Database\Eloquent\Model as Eloquent;

class StoreDetails extends Eloquent 
{

    use HybridRelations;

    protected $connection = 'mysql';
    protected $table = 'icn_store';

}

Php script

$activity = UserEventActivity::join('handset ', 'handset._id', '=', 'handset_id')
    ->join('storeDetail', 'store_id', '=', 'storeDetail.st_id')
    ->orderBy('handset.handset_make', 'desc')
    ->select('storeDetail.*', 'handset.*')
    ->get()
    ->toArray();

This data is UserEventActivitynot stored based on the field handset_makeregarding the phone.

Please help me achieve the expected result.

+4
source share
1

, MongoDB , .

.

, UserEventActivity :

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class UserEventActivity extends Eloquent 
{

    protected $collection = 'user_event_activity';
    protected $connection = 'mongodb';

    public function handset() {

        return $this->hasOne('HandsetDetails', '_id', 'handset_id');
    }

    public function storeDetail() {

        return $this->hasOne('StoreDetails', 'st_id', 'store_id');
    }

    public function getHandsetMakeAttribute()
    {
        return $this->handset->handset_make;
    }


}

getHandsetMakeAttribute(). :

$activity = UserEventActivity::with('storeDetail')
    ->with('handset')
    ->get()
    ->sortByDesc('handset_make')
    ->toArray();

, .

+1

Source: https://habr.com/ru/post/1664536/


All Articles