4
4
use Illuminate \Database \Eloquent \Model as Eloquent ;
5
5
use Illuminate \Database \Eloquent \ModelNotFoundException ;
6
6
use Illuminate \Database \Eloquent \SoftDeletingTrait ;
7
+ use Illuminate \Support \Facades \Config ;
7
8
8
9
class Thread extends Eloquent
9
10
{
@@ -30,6 +31,13 @@ class Thread extends Eloquent
30
31
*/
31
32
protected $ dates = ['created_at ' , 'updated_at ' , 'deleted_at ' ];
32
33
34
+ /**
35
+ * "Users" table name to use for manual queries
36
+ *
37
+ * @var string|null
38
+ */
39
+ private $ usersTable = null ;
40
+
33
41
/**
34
42
* Messages relationship
35
43
*
@@ -235,16 +243,16 @@ public function participantsString($userId=null, $columns=['name'])
235
243
{
236
244
$ selectString = $ this ->createSelectString ($ columns );
237
245
238
- $ participantNames = $ this ->getConnection ()->table (' users ' )
239
- ->join ('participants ' , ' users .id ' , '= ' , 'participants.user_id ' )
246
+ $ participantNames = $ this ->getConnection ()->table ($ this -> getUsersTable () )
247
+ ->join ('participants ' , $ this -> getUsersTable () . ' .id ' , '= ' , 'participants.user_id ' )
240
248
->where ('participants.thread_id ' , $ this ->id )
241
249
->select ($ this ->getConnection ()->raw ($ selectString ));
242
250
243
251
if ($ userId !== null ) {
244
- $ participantNames ->where (' users .id ' , '!= ' , $ userId );
252
+ $ participantNames ->where ($ this -> getUsersTable () . ' .id ' , '!= ' , $ userId );
245
253
}
246
254
247
- $ userNames = $ participantNames ->lists (' users .name ' );
255
+ $ userNames = $ participantNames ->lists ($ this -> getUsersTable () . ' .name ' );
248
256
249
257
return implode (', ' , $ userNames );
250
258
}
@@ -271,25 +279,50 @@ public function hasParticipant($userId)
271
279
* @param $columns
272
280
* @return string
273
281
*/
274
- private function createSelectString ($ columns )
282
+ protected function createSelectString ($ columns )
275
283
{
276
284
$ dbDriver = $ this ->getConnection ()->getDriverName ();
277
285
278
286
switch ($ dbDriver ) {
279
287
case 'pgsql ' :
280
288
case 'sqlite ' :
281
- $ columnString = implode (" || ' ' || " . $ this ->getConnection ()->getTablePrefix () . " users . " , $ columns );
282
- $ selectString = "( " . $ this ->getConnection ()->getTablePrefix () . " users . " . $ columnString . ") as name " ;
289
+ $ columnString = implode (" || ' ' || " . $ this ->getConnection ()->getTablePrefix () . $ this -> getUsersTable () . " . " , $ columns );
290
+ $ selectString = "( " . $ this ->getConnection ()->getTablePrefix () . $ this -> getUsersTable () . " . " . $ columnString . ") as name " ;
283
291
break ;
284
292
case 'sqlsrv ' :
285
- $ columnString = implode (" + ' ' + " . $ this ->getConnection ()->getTablePrefix () . " users . " , $ columns );
286
- $ selectString = "( " . $ this ->getConnection ()->getTablePrefix () . " users . " . $ columnString . ") as name " ;
293
+ $ columnString = implode (" + ' ' + " . $ this ->getConnection ()->getTablePrefix () . $ this -> getUsersTable () . " . " , $ columns );
294
+ $ selectString = "( " . $ this ->getConnection ()->getTablePrefix () . $ this -> getUsersTable () . " . " . $ columnString . ") as name " ;
287
295
break ;
288
296
default :
289
- $ columnString = implode (", ' ', " . $ this ->getConnection ()->getTablePrefix () . " users . " , $ columns );
290
- $ selectString = "concat( " . $ this ->getConnection ()->getTablePrefix () . " users . " . $ columnString . ") as name " ;
297
+ $ columnString = implode (", ' ', " . $ this ->getConnection ()->getTablePrefix () . $ this -> getUsersTable () . " . " , $ columns );
298
+ $ selectString = "concat( " . $ this ->getConnection ()->getTablePrefix () . $ this -> getUsersTable () . " . " . $ columnString . ") as name " ;
291
299
}
292
300
293
301
return $ selectString ;
294
302
}
303
+
304
+ /**
305
+ * Sets the "users" table name
306
+ *
307
+ * @param $tableName
308
+ */
309
+ public function setUsersTable ($ tableName )
310
+ {
311
+ $ this ->usersTable = $ tableName ;
312
+ }
313
+
314
+ /**
315
+ * Returns the "users" table name to use in manual queries
316
+ *
317
+ * @return string
318
+ */
319
+ private function getUsersTable ()
320
+ {
321
+ if ($ this ->usersTable !== null ) {
322
+ return $ this ->usersTable ;
323
+ }
324
+
325
+ $ userModel = Config::get ('messenger.user_model ' );
326
+ return $ this ->usersTable = (new $ userModel )->getTable ();
327
+ }
295
328
}
0 commit comments