Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Learner's avatar

Model table name set publicaly

I am setting table name at run time on Model

$table = 'gazett_' . $input['ExamYear'] . '_' . $input['gender'] . '_' . $input['Examtype'];
$obj = new Gazett();

        $obj->setTable($table);

but I want to getting result with staticaly as,

$result = Gazett::all(); // How can do it  like this

I do not want to get result with inctance of model Gazett

0 likes
1 reply
carlosmora's avatar

Hi @Learner,

I'm also a newbie too, so may be I'm following your steps. What about to pass the table name as a parameter to the constructor, and, if set, override the protected $table?

I mean

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Gazett extends Model
{

     /**
     * The table's name
     *
     * @var string
     */

    protected $table;
     
    public function __constructor($table = 'gazett') {
        $this->table = $table;
        parent::__contructor();
    }
}

so you do

$table = 'gazett_' . $input['ExamYear'] . '_' . $input['gender'] . '_' .$input['Examtype'];
$obj = new Gazett($table);

Please or to participate in this conversation.