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

ekaitzastiz's avatar

L5.5, setTable() doesn't work? What I am doing wrong?

is it a bug? in L5.5

$movement = new Movement(); 
$movement->setTable('e')->create($data);

I was trying to change table dynamically, but can't change it. I tried putting 'e' and it works againts movements table, should throw an error...

0 likes
6 replies
Snapey's avatar

im not sure what you are trying to achieve but your code does not make any sense.

You cannot make a table from an eloquent model

If you want some inspiration about how to create a table, make a migration and review the code that gets generated.

This is quite advanced though so you probably have the wrong concepts somewhere

ekaitzastiz's avatar

just want to swith to another table. it's a temporal table. called movements_temp .with same fields.... but didn't work. do i tried with 'e' just to see if it trhows an errror. but its working on "movements" table, when it shouldn't.

Snapey's avatar

You should be able to switch to a different table if it already exists?

ekaitzastiz's avatar

i know that i should be able, but i tried abd it doesn't work. why? and if i put a table that doesn't exist, it should thrpw an error, shoukdn't? but i tried with the example I put here and it's working against the original table called "movements" all the time, it doesn't matter what I do. I would thank if someone tries or writes here an example.

Snapey's avatar
Snapey
Best Answer
Level 122

ok, I see what is happening.

setTable works fine, but when you use Create it does not use the instance of $movement. It creates a new model which uses the model table.

Instead of create, fill the model you already have;

$movement = new Movement(); 

$movement->setTable('e')->fill($data);

$movement->save();

1 like
ekaitzastiz's avatar

Ok. I will try and I will tell you, but it looks like good

Please or to participate in this conversation.