0
0
Laravelframework~20 mins

Tinker for database interaction in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
Tinker for database interaction
📖 Scenario: You are working on a Laravel project and want to practice interacting with your database using Laravel's Tinker tool. Tinker allows you to run PHP code in the command line to create, read, update, and delete records easily.
🎯 Goal: Learn how to use Laravel Tinker to create a new user record, set a condition variable, retrieve users based on that condition, and finally update a user's attribute using Tinker commands.
📋 What You'll Learn
Create a new user record with specific attributes using Tinker
Define a variable to filter users by email domain
Retrieve users whose email contains the specified domain
Update the name of a user with a specific email
💡 Why This Matters
🌍 Real World
Laravel developers often use Tinker to quickly test database queries and manipulate data without writing full code or using a database GUI.
💼 Career
Knowing how to use Tinker helps developers debug, seed, and manage database records efficiently during development and testing.
Progress0 / 4 steps
1
Create a new user record
Use Laravel Tinker to create a new user with the name 'Alice', email 'alice@example.com', and password 'password'. Write the exact command to create this user using the User model and create method.
Laravel
Need a hint?

Use User::create([...]) with an array of attributes. Remember to hash the password with bcrypt().

2
Set a filter variable
In Tinker, create a variable called $domain and set it to the string 'example.com'. This variable will be used to filter users by their email domain.
Laravel
Need a hint?

Simply assign the string 'example.com' to the variable $domain.

3
Retrieve users by email domain
Use the User model to retrieve all users whose email contains the value in $domain. Use the where method with like operator and the get method to fetch the results. Assign the result to a variable called $users.
Laravel
Need a hint?

Use User::where('email', 'like', "%{$domain}%")->get() to find users with emails containing the domain.

4
Update a user's name
Find the user with email 'alice@example.com' using the User model and where method. Then update the user's name to 'Alice Smith' using the update method.
Laravel
Need a hint?

Use User::where('email', 'alice@example.com')->update(['name' => 'Alice Smith']) to update the user's name.