Eloquent Dynamic Table name using Laravel 6

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.

READ :  Angular Insert Update Delete Using PHP MySQLi

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.

How to dynamically set table name in Eloquent Model

using trait in Laravel
[php]
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;
}

}
[/php]

simple use in Laravel 6
[php]
class Cricketers_Log extends Model
{
use BindsDynamically;
}
[/php]

Laravel simple Call the method on instance
[php]
public function index()
{
$cricketers_Log = new Cricketers_Log;
$cricketers_Log->setTable(‘cricketers_2020’);
$cricketers_Log->get(); // select * from cricketers_2020

READ :  C# pointers Tutorial with Examples

$cricketers_Log->teamTestName = ‘india’;
$cricketers_Log->save(); // now saves into cricketers_2020
}
[/php]

Laravel 6 dynamically set table name in model

add this in the Laravel model
[php]
public function scopeFromTable($query, $tableName)
{
$query->from($tableName);
}
[/php]

use in Laravel Controller
[php]
$DynamicTableData = ModelName::fromTable($tableName)->get();

[/php]

Dynamic table name for Laravel model

Cricketers.php
[php]
public function __construct($params=array()) {
if (isset($params[‘table’])) {
$this->table = $params[‘table’];
}
}
[/php]

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

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
}
}
}

in controller

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

READ :  Sorting and Searching using Vuejs - Vuejs table sort pagination
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.

Leave a Comment