-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathUntitled Document
228 lines (105 loc) · 4.34 KB
/
Untitled Document
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
Setup Eloquent
First, create a new folder in the public directory and name it “eloquent”. Now run the following composer command to install Eloquent.
composer require illuminate/database
Wait for composer to finish the installation.
Create a new file and name it bootstrap.php. I will use this file to setup Eloquent. Now paste the following code in it.
<?php
require "vendor/autoload.php";
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
"driver" => "mysql",
"host" =>"127.0.0.1",
"database" => "acl",
"username" => "root",
"password" => ""
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
The code starts with requiring the composer autoload class, then the required Eloquent Manager (Capsule), and finally created its instance. Through Capsule, I have set the database connection as global, and then booted Eloquent. After that, I created a new database on the localhost with the name “acl”.
Create Table For Migration
First create a new folder and name it “database”. Now, create a new file for the users table and name it “User.php”. Next, paste the following code in it:
<?php
require "../bootstrap.php";
use Illuminate\Database\Capsule\Manager as Capsule;
Capsule::schema()->create('users', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('userimage')->nullable();
$table->string('api_key')->nullable()->unique();
$table->rememberToken();
$table->timestamps();
});
The above code is similar to what I would write in Laravel. The difference is the schema() with reference from Capsule class. I will now create table for the roles. For that, create a new file and name it “Todo.php”. Paste the following code in it:
<?php
require "../bootstrap.php";
use Illuminate\Database\Capsule\Manager as Capsule;
Capsule::schema()->create('todos', function ($table) {
$table->increments('id');
$table->string('todo');
$table->string('description');
$table->string('category');
$table->integer('user_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
I will now migrate all the files. For that open your browser and head to eloquent/database/user.php and eloquent/database/todo.php. If you see a blank page, the tables have been migrated successfully.
Create Model Class With Eloquent
I will now create model classes using Eloquent.
Create a new folder and name it classes. Now open composer.json file and paste the following lines after require:
"autoload": {
"classmap": [
"classes"
]
}
This will help me add classes folder as autoload. Now create a new file inside the classes folder and name it User.php. Paste the following code:
<?php
use Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','userimage'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/*
* Get Todo of User
*
*/
public function todo()
{
return $this->hasMany('Todo');
}
}
Next, create a new file, name it Todo.php and paste the following code:
<?php
use Illuminate\Database\Eloquent\Model as Eloquent;
class Todo extends Eloquent
{
protected $fillable = ['todo','category','description'];
}
All the classes are now in place and I could go ahead with the testing.
Test the PHP Code
I will now create a new file at the root and name it index.php. Open this file and paste the following code in it:
<?php
require "bootstrap.php";
$user = User::Create([ 'name' => "Ahmed Khan", 'email' => "ahmed.khan@lbs.com", 'password' => password_hash("ahmedkhan",PASSWORD_BCRYPT), ]);
print_r($user->todo()->create([
'todo' => "Working with Eloquent Without PHP",
'category' => "eloquent",
'description' => "Testing the work using eloquent without laravel"
]));
When you execute this file, you will see the following output: