Laravel Tinker Creating Data

We’ll go into the command line to create a new entry in posts:

php artisan tinker

Now add a new record:

$post = App\Models\Post::create(['title'=>'PHP post from tinker','content'=>'PHP content tinker','user_id'=>1]);

It will show you the results of the post including new ID.

To see again type:

$post

This means you can set the variable with all the information before you create a database entry

$post = new App\Models\Post

See the result

$post

Add rest of information:

$post->title = "New title for object"
$post->content = "New content for object"
$post->user_id = 2

See the result

$post

To save this post to the database just do like this:

$post->save()

Leave a Reply

Your email address will not be published. Required fields are marked *