You can add this in your model.
protected $table = 'your_table_name';
Tell me if it helps ;).
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Is any way to make a form wich will display DB table name and when i want to change existing table with a new table by writing new name on form and submit so now laravel can fetch data from new table i jus change name. Is any way to do this? Or should i run any cron job function wich detect where table should change ( base on a funtion) ? I`m jus asking
You can add this in your model.
protected $table = 'your_table_name';
Tell me if it helps ;).
@vincent15000 This is not what i`m looking for. I mean to change $table = "table_name" by a form on view.
It's possible to do but I wouldn't recommend it sice you would have to put a shitload of effoert into making validation and and using the correct route and controller for creating the records. Then you would have to dynamically build the html form with javascript depending on the table structure.
It's not worth the hassle to try to get it to work properly.
@Tray2 To already have done it, I really don't understand what could be the problem and why it would be necessary to build the form with javascript. Well ... I didn't have any problem.
@vincent15000 maybe I misunderstod What the OP wants to do.
Why? I dont understand the use case
@Sinnbeck couse i fetch saome data from a DB which have many tables wich change every time. For example Now i am using table = custom_1, after one weak this table will change to custom_2, also for two weak to custom_3 and infinit same process. So i need a way to let laravel know the table name is changed
@usertxr That sounds like a very weird database design? Is it possible to get the table by some logic?
public function getTable()
{
return 'custom_' . now()->weekOfYear;
}
@Sinnbeck You have helped me some time and thanks for helping me. I think you do not undestand me very good. Maybe i have explained in wrong way. So : I have a system when i need to put some table in db. This table has name "custom_1, custom_2, custom_3" and go to infinit number. So now i fetch this table in my admin panel, until now every thing is okay (now i am using custom_1). But later maybe 3 day maybe one weak it depend , i need to upload new table wich is custom_2 or custom_(infinit number) . Here i need a way for laravel to know table was changed and to fetch data from new table (custom_infinit number)
@usertxr So you do the importing and such manually? Perhaps use a setting in your env/config?
in env
TABLE_NAME=custom_3
config/database.php
'current_table' => env('TABLE_NAME')
and model
public function getTable()
{
return config('database.current_table');
}
@usertxr this sounds very strange to me. Why the hell do you need to create a new table like that. Use the same table instead.
@Sinnbeck If i do this i need to change TABLE_NAME on .env every time i will upload a new table?
@usertxr Yes. But it sounds like you need to do it manually anyways? How would laravel know what you name the new table?
@Sinnbeck This is my real question. I have some idea. But if i do manually i would prefer a way when i will write new table name in a form then i can submit by a form. Is this posibile?
@usertxr you could probably save it to a text file and then read the contents in the getTable method
@Sinnbeck And how should i do this exactly?
@usertxr save to a file?
file_put_contents(storage_path('tablename.txt'), $request->tablename);
//and get contents
$table = trim(file_get_contents(storage_path('tablename.txt'));
$cusTable = DB::connection('mysql2')->table($table)->get();
@Sinnbeck I read the entire post ... I don't understand why it is necessary to write the name of the table in the .env file ... I never used that ...
@vincent15000 Please can you show me how can i do this?
@usertxr did my answer not work for you?
I have another problem. When i do foreach loop function he continue and after get last data . How should i stop when get all data and do not continue after geting all data?
@usertxr A foreach only loops as long as there is an element left it doesn't loop more than the number of items in the array or collection.
Dump your data and you will see that you have empty items in the array.
@usertxr please start a new thread if you have an unrelated issue
@Tray2 i have this loop function, in custom_1002 table i have 5000 rows. But when this function runs.. in table Totals goes more than 5000. Where is the problem here
$cusTable = DB::connection('mysql2')->table('custom_1002')->get();
foreach($cusTable as $key => $cus) {
DB::connection('mysql')->table('Totals')->insert([
"user_id" =>1001,
"lead_id" =>$cus->lead_id,
"mosha" =>$cus->mosha,
"statusi" =>$cus->statusi
]);
}
@usertxr How is that related to changing the table name?
@usertxr If you have more than 5000 records in your cusTable it will add them all.
If you only want 5000 you could use a regular for loop.
Something like this.
for ($i = 0; $i < 5000; $i++) {
DB::connection('mysql')->table('Totals')->insert([
"user_id" =>1001,
"lead_id" =>$cusTable[$i]['lead_id'],
"mosha" =>$cusTable[$i]['mosha'],
"statusi" =>$cusTable[$i]['statusi']
]);
}
@Sinnbeck No si, This is another problem i find on my way. Sorry for that
@Tray2 table have no fixed numbers. He change couse i upload more data every time. So you mean function i have write is okay? This function should get all my data and stop when get last row?
@usertxr Yes an foreach will continue until every row returned in your query has been processed.
@Tray2 Yes but Total table gets unlimited rows. Also browser stay on reloading my page
@usertxr Of corse it does. It will do that until the loop is finished or the request times out.
Stuff like this should be done with a job in the background and never from the client side.
@Tray2 I know couse i have done before and it works fine. But in this case it is not working fine. maybe is my mistake. But i never used schedule before. Please can you give me an example with my function ? I mean how should i call this function on schedule task?
@usertxr Try debugging it..
dd(DB::connection('mysql2')->table('custom_1002')->count());
@usertxr Here is basically all you need to know
your totals table will contain all the rows from the input table PLUS all the rows it had before you started
@Snapey yes but Total table is empty and it gets all data from custom_1002. So total table need to get just data from custom table. My problem is where Total table goes to infinit rows
Please or to participate in this conversation.