-2

How can i define create method with variables and model in laravel? Like this:

public function createMethodName($variable, $variable2)
{
   \App\ModelName::create([...]);
}
Zollie
  • 1,171
  • 7
  • 14

1 Answers1

0

You have to just insert the passed variables in the array like:

// include model at the top of your file:
use \App\Models\ModelName;

public function createMethodName($variable1, $variable2)
{
    $my_stuff = ModelName::create([
       ‘something’ => $variable1,
       ‘something_else’ => $variable2,
    ]);
}

Also, do not forget to make some of your Model fields fillable (fields which are included in the array) in your Model, if you want to save values to the table with using create(). Check the Reference about this: https://laravel.com/docs/8.x/eloquent#mass-assignment

Short update:

Maybe I misunderstood your question a bit since according to your new comments under your question it is getting clearer that you maybe first would like to create a Table with Schema::create() method (which is a totally different part of the fun). Reference: https://laravel.com/docs/8.x/migrations#creating-tables

Then after you created your Table, you can create a Model using artisan like: php artisan make:model ModelName Reference: https://laravel.com/docs/8.x/eloquent#defining-models

and then you can use ModelName::create() method in your Controller as I gave it above to saving your data into your Table. So you should study these steps a bit more if it is the case.

Zollie
  • 1,171
  • 7
  • 14