The simplest solution I found is to create a parent model / class - as noted in this answer to a different question - for the current example let's call it BaseModel.
For Laravel this is stored along with the other models, directly in the App directory
...in my case the parent model / class can be abstract as it doesn't need to have it's own table (also meaning the BaseModel functions can only be accessed via a child model):
namespace App;
use Illuminate\Database\Eloquent\Model;
abstract class BaseModel extends Model
{
// ...put common model functions here
}
The trick to get this working (where a BaseModel function will need to access a child model's table) is to:
- have each child model / class extend the BaseModel class
- ensure that the $table is defined in all the child models - as noted here:
class MyFunkyModel extends BaseModel
{
protected $table = 'funktown';
// ...put child specific functions here
}
which means $this->table can be used in the BaseModel abstract parent class to reference the designated $table of the child model / class:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
abstract class BaseModel extends Model
{
function getRandomRow() {
$rowCount = DB::connection('mydatabasename')
->table($this->table)
->selectRaw("FLOOR(RAND() * COUNT(*)) AS offset")
->first();
$offset = $rowCount->offset;
$profileRow = DB::connection('mydatabasename')
->table($this->table)
->select()
->offset($offset)
->limit(1)
->first();
return $profileRow;
}
}
Now from a controller you can access any functions stored in the BaseModel simply via the child model(s):
namespace App\Http\Controllers;
use App\MyFunkyModel;
use App\MySeriousModel;
use App\MyHappyModel;
class MyController extends Controller
{
public function getRandomName($type){
switch ($type) {
case 'funky':
$modelFunky = new MyFunkyModel;
$funkyRow = $modelFunky->getRandomRow();
$randomName = $funkyRow->funky_name_column;
break;
case 'serious':
$modelSerious = new MySeriousModel;
$seriousRow = $modelSerious->getRandomRow();
$randomName = $seriousRow->serious_name_column;
break;
case 'happy':
$modelHappy = new MyHappyModel;
$happyRow = $modelHappy->getRandomRow();
$randomName = $happyRow->happy_name_column;
break;
return $randomName;
}
}