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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
## PHP
### Basic Syntax
##### Variables
* Variable Declaration
```php
$variable_name = value;
```
* Variable Types
```php
$integer_variable = 42;
$float_variable = 3.14;
$string_variable = "Hello, World!";
$boolean_variable = true;
$array_variable = array(1, 2, 3);
$null_variable = null;
```
- - -
##### Control Structures
* If Statement
```php
if (condition) {
// code to execute if condition is true
} elseif (condition2) {
// code to execute if condition2 is true
} else {
// code to execute if all conditions are false
}
```
* Switch Statement
```php
switch ($variable) {
case value1:
// code to execute if $variable equals value1
break;
case value2:
// code to execute if $variable equals value2
break;
default:
// code to execute if $variable doesn't match any case
break;
}
```
* For Loop
```php
for ($i = 0; $i < 5; $i++) {
// code to execute repeatedly
}
```
* While Loop
```php
while (condition) {
// code to execute repeatedly as long as condition is true
}
```
* Do-While Loop
```php
do {
// code to execute repeatedly at least once
} while (condition);
```
- - -
##### Functions
* Function Declaration
```php
function function_name($param1, $param2) {
// code to execute
}
```
* Function Call
```php
function_name($arg1, $arg2);
```
- - -
##### Arrays
* Indexed Array
```php
$indexed_array = array("apple", "banana", "cherry");
```
* Associative Array
```php
$associative_array = array("name" => "John", "age" => 25, "city" => "New York");
```
* Accessing Array Elements
```php
$indexed_array[0]; // returns "apple"
$associative_array["name"]; // returns "John"
```
- - -
##### Strings
* String Concatenation
```
$greeting = "Hello";
$name = "John";
$full_greeting = $greeting . ", " . $name . "!";
```
* String Length
```php
$length = strlen($string);
```
* Substring
```php
$substring = substr($string, $start, $length);
```
- - -
##### File Handling
* Read File
```php
$file_content = file_get_contents($file_path);
```
* Write to File
```php
file_put_contents($file_path, $data);
```
- - -
### Laravel
##### Routing
* Basic Routing
```php
Route::get('/url', 'Controller@method');
```
* Route Parameters
```php
Route::get('/user/{id}', 'UserController@show');
```
* Route Names
```php
Route::get('/url', 'Controller@method')->name('route.name');
```
- - -
##### Controllers
* Creating a Controller
```bash
$ php artisan make:controller ControllerName
```
* Controller Methods
```php
public function methodName(Request $request)
{
// code to execute
return response()->json($data);
}
```
- - -
##### Models
* Creating a Model
```bash
$ php artisan make:model ModelName
```
* Model Relationships
```php
public function relatedModel()
{
return $this->belongsTo(RelatedModel::class);
}
```
- - -
##### Migrations
* Creating a Migration
```bash
$ php artisan make:migration create_table_name
```
* Running Migrations
```bash
$ php artisan migrate
```
- - -
##### Views
* Blade Templates
```html
@extends('layout')
@section('content')
// code to display content
@endsection
```
* Passing Data to Views
```php
return view('view_name', ['data' => $data]);
```
- - -
##### Eloquent ORM
* Retrieving Data
```php
$users = User::all();
```
* Creating a New Record
```php
$user = new User;
$user->name = 'John';
$user->email = '[email protected]';
$user->save();
```
* Updating a Record
```php
$user = User::find($id);
$user->name = 'New Name';
$user->save();
```
* Deleting a Record
```php
$user = User::find($id);
$user->delete();
```
- - -
##### Middleware
* Creating Middleware
```bash
php artisan make:middleware MiddlewareName
```
* Applying Middleware
```php
Route::get('/url', 'Controller@method')->middleware('middleware_name');
```
- - -
##### Authentication
* User Registration
```php
use Illuminate\Support\Facades\Hash;
public function register(Request $request)
{
$user = new User;
$user->name = $request->name;
$user->email = $request->email;
$user->password = Hash::make($request->password);
$user->save();
}
```
* User Login
```php
use Illuminate\Support\Facades\Auth;
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed
}
}
```
|