Monday, February 11, 2019

Create and Maintain Users with Artisan Tinker in Laravel

Create and Maintain Users with Artisan Tinker in Laravel

Now, it’s time to create a user to the database. Of course, using register form we have just created to create a user is a standard process. By the way, it’s good to have the functionality test to the register form.

However, there is a way to create user without register form. It utilizes the ‘artisan tinker’ commands to do so. It’s quite easy and can be done before we create the register form.

> php artisan tinker

When execute the command above, the Psy Shell shows and we can type the tinker commands to it.

Create a user

>>> $user = User::create(array('name' => 'Tad White', 
    'email' => 'tad@test.com', 
    'password' => Hash::make('xxxxxxxx')));
=> App\User {#2941
     name: "Tad White",
     email: "tad@test.com",
     updated_at: "2019-02-10 16:08:24",
     created_at: "2019-02-10 16:08:24",
     id: 1,
   }
>>>

or

>>> $user = new User;
=> App\User {#2938}
>>> $user->name = 'a';
=> "a"
>>> $user->email = 'a@test.com';
=> "a@test.com"
>>> $user->password = Hash::make('xxxxxxxx');
=> "$2y$10$E5ODjTvQefxCvMtm5S2xyuto9gqOtOJJRTNErUH9x6AtSnTPCPoki"
>>> $user->save();
=> true

Show all users

>>> User::all();
=> Illuminate\Database\Eloquent\Collection {#2943
     all: [
       App\User {#2940
         id: 1,
         name: "Tad White",
         email: "tad@test.com",
         email_verified_at: null,
         birthday: null,
         is_admin: 0,
         created_at: "2019-02-10 16:08:24",
         updated_at: "2019-02-10 16:08:24",
       },
       App\User {#2936
         id: 2,
         name: "a",
         email: "a@test.com",
         email_verified_at: null,
         birthday: null,
         is_admin: 0,
         created_at: "2019-02-10 16:14:02",
         updated_at: "2019-02-10 16:14:02",
       },
     ],
   }

Find the number of users

>>> User::count();
=> 2

Now, there are two users in the database.

Find a user

Let’s find the first user:

>>> User::find(1);
=> App\User {#2933
     id: 1,
     name: "Tad White",
     email: "tad@test.com",
     email_verified_at: null,
     birthday: null,
     is_admin: 0,
     created_at: "2019-02-10 16:08:24",
     updated_at: "2019-02-10 16:08:24",
   }

Or, we can do this to find the name includes ‘Tad’:

>>> User::where('name', 'like', '%Tad%')->get();
=> Illuminate\Database\Eloquent\Collection {#2913
     all: [
       App\User {#2944
         id: 1,
         name: "Tad White",
         email: "tad@test.com",
         email_verified_at: null,
         birthday: null,
         is_admin: 0,
         created_at: "2019-02-10 16:08:24",
         updated_at: "2019-02-10 16:08:24",
       },
     ],
   }
>>>

To find the name equal to ‘a’ and get all users:

>>> User::where('name', '=', 'a')->get();

or

>>> User::where('name', 'a')->get();

Using get() method to show all users from the query result set

To show the result set or collection that meets the condition, use 'get()' method:

>>> User::where('id', '>' , '2')->get();

Delete a user

> $user = User::find(1);
> $user->delete();

Create fake seed data

>>> factory(App\User::class, 10)->create();
=> Illuminate\Database\Eloquent\Collection {#2956
     all: [
       App\User {#2952
         name: "Abigail Fahey",
         email: "jaqueline.gusikowski@example.org",
         email_verified_at: Illuminate\Support\Carbon @1549816574 {#2951
           date: 2019-02-10 16:36:14.637540 UTC (+00:00),
         },
         updated_at: "2019-02-10 16:36:14",
         created_at: "2019-02-10 16:36:14",
         id: 3,
       },
       :

1 comment: