Skip to content

Commit

Permalink
reference the user model's primary key directly
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgmyr committed Oct 15, 2016
1 parent cecd58b commit f6d6698
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Cmgmyr/Messenger/Models/Models.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cmgmyr\Messenger\Models;

use App\User;
use Illuminate\Database\Eloquent\Model;

class Models
{
Expand Down Expand Up @@ -60,7 +60,7 @@ public static function setThreadModel($model)
*/
public static function setUserModel($model)
{
static::$models[User::class] = $model;
static::$models[Model::class] = $model;
}

/**
Expand Down Expand Up @@ -145,7 +145,7 @@ public static function thread(array $attributes = [])
*/
public static function user(array $attributes = [])
{
return static::make(User::class, $attributes);
return static::make(Model::class, $attributes);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Cmgmyr/Messenger/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,17 @@ public function participantsString($userId = null, $columns = ['name'])
{
$participantsTable = Models::table('participants');
$usersTable = Models::table('users');
$userPrimaryKey = Models::user()->getKeyName();

$selectString = $this->createSelectString($columns);

$participantNames = $this->getConnection()->table($usersTable)
->join($participantsTable, $usersTable . '.id', '=', $participantsTable . '.user_id')
->join($participantsTable, $usersTable . '.' . $userPrimaryKey, '=', $participantsTable . '.user_id')
->where($participantsTable . '.thread_id', $this->id)
->select($this->getConnection()->raw($selectString));

if ($userId !== null) {
$participantNames->where($usersTable . '.id', '!=', $userId);
$participantNames->where($usersTable . '.' . $userPrimaryKey, '!=', $userId);
}

return $participantNames->implode('name', ', ');
Expand Down
9 changes: 9 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Cmgmyr\Messenger\Test\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
}
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
date_default_timezone_set('America/New_York');

use AdamWathan\Faktory\Faktory;
use Cmgmyr\Messenger\Models\Models;
use Illuminate\Database\Capsule\Manager as DB;
use Orchestra\Testbench\TestCase as Orchestra;

Expand All @@ -29,6 +30,9 @@ public function setUp()
require __DIR__ . '/factories.php';
};
$load_factories($this->faktory);

$userModel = User::class;
Models::setUserModel($userModel);
}

/**
Expand Down

0 comments on commit f6d6698

Please sign in to comment.