Recently I've been learning to use Slim 3 and eloquent.
What I'm trying to do is this (if it's even possible that is)
So I have a Model.php file
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model as Model;
use Illuminate\Database\Capsule\Manager as DB;
class Course extends Model{
protected $table = "courses";
public function GetCourses(){
}
}
?>
And my Controller.php
<?php
namespace App\Controllers;
use App\Models\Course;
use Slim\Views\Twig as View;
class CourseController extends Controller{
public function index($request,$response){
return $this->view->render($response,'course/CourseNew.twig',$data);
}
}
?>
So my question is inside the Model.php is it possible to call another table somehow?
I've already called mine with protected $table = "courses";
I kind of understand that the table i defined is for the whole Class but is there a way or a workaround?
The main idea here is that i have some database tables that are very small and are not worth making another Model files for them
If this is not possible what is the alternative?
Do I have to make another Model file and call it on top of my controller where i need it use namespace App\Models\"new_model";
I would recommend to separate the data structure from the data access logic. For example: put the code for the database queries into a Repository. A repository does not care where the data comes from (table x or table y).
<?php
namespace App\Repository;
use App\Model\Course;
class CourseRepository extends Repository
{
public function __construct(Connection $db)
{
$this->db = $db;
}
public function findCourceById(int $courseId): ?Course
{
// execute query here
//$this->db->....
return $course;
}
public function findSomething(): array
{
// execute query here
//$this->db->....
return $rows ?: [];
}
}