kramsuiluj's avatar

Inserting data into db using php artisan tinker

I have a function inside my model class that inserts data into the database it's something like this:

public function insert($name){
	$this->name = $name;
}

Then on the terminal I'm calling the function to insert a data:

$project->insert('Test')
$project->save()

I am able to insert data into the database, but when I want to insert another set of data the first one that I inserted just gets overwritten. What do I do so it won't get overwritten?

0 likes
4 replies
MichalOravec's avatar
Level 75

You always update the field name on your retrieved project model.

If you want to create a new project then

$project = new Project;

$project->name = 'Foo';

$project->save();

or with mass assigment

$project = Project::create([
    'name' => 'Foo'
]);
1 like
kramsuiluj's avatar

Oh, so every time I want to insert a new data, I also need to create a new instance of the Model, or do that mass assignment.

Thanks a lot!

Please or to participate in this conversation.