0

I wanna ask about some method how to create a new field when I create a new data in Laravel, for imaging the result I write my expectation in below :

// method to insert a new data

function create_data_example(Request $some_data) {
    $new_data = new MyModel();

    $new_data = $some_data->name;
    $new_data = $some_data->age;
    $new_data = $some_data->gender;

    $new_data->save();
}

// the result just an insert a new data to field name, age, and gender

How about if I make some code to insert a new field (example field is hobby) and insert a new data to my new field (field hobby) with the same method like above? Thank You

ProLuck
  • 331
  • 4
  • 18
  • Adding new field to table should be done through migration. [Here](https://laravel.com/docs/8.x/migrations) is how. Also, check [this](https://stackoverflow.com/questions/16791613/add-a-new-column-to-existing-table-in-a-migration) answer. – Tpojka Oct 27 '20 at 07:31
  • nope, no like that, I mean when I create a new value and insert my data into my field and also I want create a new field and insert new value into my new field to – ProLuck Oct 27 '20 at 08:36
  • Can you edit your question and explain real reason for having such an anti-pattern? You can't leave to yourself (as application developer) possibility that you don't know what kind of data you have in DB. Because it is not way to do it. Maybe (big maybe) would be to have json field. Or some kind of attribute table that belongs to main table hasMany/belongsTo. – Tpojka Oct 27 '20 at 09:14

0 Answers0