Today, We want to share with you Eloquent Dynamic Table name using Laravel 6.In this post we will show you wordpress plugin require another plugin, hear for How to declare the table name dynamically in Laravel 6 we will give you demo and example for implement.In this post, we will learn about How to get table name and table column names from model in Laravel 6? with an example.

Eloquent Dynamic Table name using Laravel 6

There are the Following The simple About Laravel 6 Eloquent join with dynamic table name in model Full Information With Example and source code.

As I will cover this Post with live Working example to develop PHP laravel 6 Eloquent Dynamic Table Name Example, so the Laravel Eloquent: access to properties and dynamic table names is used for this example is following below.

READ :  VueJS Features and Benefits - Vuejs advantages and disadvantages

How to dynamically set table name in Eloquent Model

using trait in Laravel

trait BindsDynamically
{
    protected $connection = null;
    protected $table = null;

    public function bind(string $connection, string $table)
    {
       $this->setConnection($connection);
       $this->setTable($table);
    }

    public function newInstance($attributes = [], $exists = false)
    {
       $table_model_name = parent::newInstance($attributes, $exists);
       $table_model_name->setTable($this->table);

       return $table_model_name;
    }

}

simple use in Laravel 6

class Cricketers_Log extends Model
{
   use BindsDynamically;
}

Laravel simple Call the method on instance

public function index() 
{
   $cricketers_Log = new Cricketers_Log;
   $cricketers_Log->setTable('cricketers_2020');
   $cricketers_Log->get(); // select * from cricketers_2020

   $cricketers_Log->teamTestName = 'india';
   $cricketers_Log->save(); // now saves into cricketers_2020
}

Laravel 6 dynamically set table name in model

add this in the Laravel model

public function scopeFromTable($query, $tableName) 
{
    $query->from($tableName);
}

use in Laravel Controller

$DynamicTableData = ModelName::fromTable($tableName)->get();

Dynamic table name for Laravel model

Cricketers.php

public function __construct($params=array()) {
    if (isset($params['table'])) {
        $this->table = $params['table'];
    }
}

anyController.php

$cricketers = new \App\Cricketers(['table'=>'Cricketers_2020' . $this->teamId]);
// checking out
dd($cricketers->getTable());

Dynamic table name for Laravel 6 model

in Model
class Cricketer extends Model
{
public function __construct( array $attributes = [] )
{
parent::construct($attributes);
if (array_key_exists(‘table’, $attributes)) {
$this->setTable($attributes[‘table’]) ;
}
else {
// some code here
}
}
}

READ :  Laravel Ignore duplicate record on insert

in controller

$cricketer_1 = new Cricketer([‘table’ => ‘cricketer_1’]);
$cricketer_2 = new Cricketer([‘table’ => ‘cricketer_2’]);
$cricketer_3 = new Cricketer([‘table’ => ‘cricketer_3’]);

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Dynamic table name for Laravel 6 model.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.