Bird
Raised Fist0
Laravelframework~20 mins

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

Choose your learning style10 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
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
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
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
Hint

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

Practice

(1/5)
1. What is the main purpose of Laravel Tinker when working with databases?
easy
A. To create full web pages automatically
B. To interact with the database quickly using command line
C. To design database schemas visually
D. To deploy the Laravel application to a server

Solution

  1. Step 1: Understand Tinker's role

    Tinker is a command line tool that lets you run PHP code and interact with your Laravel app quickly.
  2. Step 2: Focus on database interaction

    It allows you to create, read, update, and delete database records without building full pages.
  3. Final Answer:

    To interact with the database quickly using command line -> Option B
  4. Quick Check:

    Tinker = Quick database interaction [OK]
Hint: Tinker is a command line tool for quick database tests [OK]
Common Mistakes:
  • Thinking Tinker creates web pages
  • Confusing Tinker with deployment tools
  • Assuming Tinker designs database visually
2. Which of the following is the correct way to start Laravel Tinker from the terminal?
easy
A. php artisan tinker
B. php tinker start
C. artisan tinker run
D. laravel tinker open

Solution

  1. Step 1: Recall Laravel command syntax

    Laravel commands are run using 'php artisan' followed by the command name.
  2. Step 2: Identify the correct Tinker command

    The correct command to start Tinker is 'php artisan tinker'.
  3. Final Answer:

    php artisan tinker -> Option A
  4. Quick Check:

    Start Tinker = php artisan tinker [OK]
Hint: Use 'php artisan' then command name to run Laravel tools [OK]
Common Mistakes:
  • Using 'php tinker start' which is invalid
  • Omitting 'artisan' in the command
  • Confusing command order or names
3. What will be the output of this Tinker command if the 'users' table has 3 records?
App\Models\User::count();
medium
A. 3
B. [User, User, User]
C. null
D. Error: Method count() not found

Solution

  1. Step 1: Understand the count() method

    The count() method returns the number of records in the database table for the model.
  2. Step 2: Apply to the 'users' table

    Since there are 3 records, the method returns 3 as an integer.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    count() returns number of records [OK]
Hint: count() returns number of records, not list [OK]
Common Mistakes:
  • Expecting a list of users instead of a number
  • Thinking count() returns null if records exist
  • Assuming count() is not a valid method
4. You run this Tinker command to create a new user:
App\Models\User::create(['name' => 'Anna']);
But you get an error about mass assignment. What is the likely cause?
medium
A. The database connection is missing
B. You forgot to run migrations
C. The 'create' method does not exist
D. The 'name' field is not fillable in the User model

Solution

  1. Step 1: Understand mass assignment protection

    Laravel requires you to specify which fields can be filled using 'fillable' property in the model.
  2. Step 2: Identify cause of error

    If 'name' is not listed in 'fillable', Laravel blocks mass assignment and throws an error.
  3. Final Answer:

    The 'name' field is not fillable in the User model -> Option D
  4. Quick Check:

    Mass assignment error = missing fillable field [OK]
Hint: Add fields to $fillable to fix mass assignment errors [OK]
Common Mistakes:
  • Assuming database connection is the problem
  • Thinking 'create' method is missing
  • Ignoring fillable property in model
5. You want to update the email of the user with ID 5 using Tinker. Which command correctly does this?
hard
A. App\Models\User::update(['email' => 'new@example.com'])->where('id', 5);
B. App\Models\User::where('id', 5)->email = 'new@example.com';
C. App\Models\User::find(5)->update(['email' => 'new@example.com']);
D. App\Models\User::edit(5, ['email' => 'new@example.com']);

Solution

  1. Step 1: Find the user by ID

    Use find(5) to get the user model instance with ID 5.
  2. Step 2: Call update() on the model instance

    Use update() with an array of fields to change the email.
  3. Final Answer:

    App\Models\User::find(5)->update(['email' => 'new@example.com']); -> Option C
  4. Quick Check:

    Find then update = correct update method [OK]
Hint: Use find(id)->update([...]) to update a record in Tinker [OK]
Common Mistakes:
  • Trying to update without finding the model first
  • Using non-existent methods like edit()
  • Calling update() before where()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard