-
Notifications
You must be signed in to change notification settings - Fork 1
/
laravel.html
481 lines (446 loc) · 24.4 KB
/
laravel.html
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Laravel Cheatsheet</title>
<!-- Bootstrap CDN -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<!-- CSS -->
<link rel="stylesheet" href="assets/style.css">
</head>
<body data-theme="light">
<div class="container">
<!-- Theme Toggle -->
<div class="button float-end"><i class="bi bi-circle-half"></i></div>
<h3 class="title">Laravel</h3>
<div class="section-list">
<ul>
<li><a href="#laravel-framework">Laravel Framework</a></li>
<li><a href="#folder-structure">Folder Structure</a></li>
<li><a href="#route">Routes</a></li>
<li><a href="#blade">Blades</a></li>
<li><a href="#composer">Composer</a></li>
<li><a href="#artisan">Artisan</a></li>
<li><a href="#database">Database</a></li>
</ul>
</div>
<div class="sections" id="laravel-framework">
<h4 class="heading"># Laravel Framework</h4>
<div class="content">
<p>Laravel is an open-source PHP web framework used for building web applications, based on the Model-View-Controller (MVC) architectural pattern. Laravel offers a wide range of features, including routing, middleware, authentication, sessions, caching, database management (with Eloquent ORM), and templating with Blade.</p>
<p>A web framework provides a structure, foundation and starting point for creating application, allowing to focus on creating application. It includes a collection of pre-written code, libraries, APIs, and tools that developers can use to build applications more efficiently.</p>
</div>
</div>
<div class="sections" id="folder-structure">
<h4 class="heading"># Folder Structure</h4>
<div class="content">
<ul>
<li>
<p><b><code>app</code> :</b> This directory contains the core code of the application. It includes subdirectories, like</p>
<ul>
<li><b><code>Console</code> :</b> Contains custom Artisan commands generated using the <code>make:command</code> command.</li>
<li><b><code>Exceptions</code> :</b> Contains custom exception handlers generated using the <code>make:exception</code> command..</li>
<li><b><code>Http</code> :</b> Contains controllers, middleware, and form requests.</li>
<li><b><code>Models</code> :</b> Contains the Eloquent ORM models.</li>
<li><b><code>Providers</code> :</b> Contains service providers for application bootstrapping.</li>
</ul>
</li>
<li>
<p><b><code>bootstrap</code> :</b> Contains the application's bootstrap files, including <code>app.php</code>, which bootstraps the Laravel framework.</p>
</li>
<li>
<p><b><code>config</code> :</b> Configuration files for various parts of the application, such as database, cache, and session configuration.</p>
</li>
<li>
<p><b><code>database</code> :</b> Contains database migrations, seeds, and model factories.</p>
<ul>
<li><b><code>migrations</code> :</b> Database migration files.</li>
<li><b><code>seeds</code> :</b> Database seeders.</li>
<li><b><code>factories</code> :</b> Model factories for generating test data.</li>
</ul>
</li>
<li>
<p><b><code>public</code> :</b> This is the web server's document root. It contains the entry point (<code>index.php</code>), which is the entry point for all requests entering the application and configures autoloading and publicly accessible assets like CSS, JavaScript, and image files.</p>
</li>
<li>
<p><b><code>resources</code> :</b> Contains resources that the application uses, such as views, language files, and assets.</p>
<ul>
<li><b><code>css, js, sass</code> :</b> Contains CSS and JavaScript assets.</li>
<li><b><code>lang</code> :</b> Language files for localization.</li>
<li><b><code>views</code> :</b> Blade templates for generating HTML.</li>
</ul>
</li>
<li>
<p><b><code>routes</code> :</b> Contains route definitions for the application.</p>
<ul>
<li><b><code>web.php</code> :</b> Routes for the web interface.</li>
<li><b><code>api.php</code> :</b> Routes for API endpoints.</li>
<li><b><code>console.php</code> :</b> Routes for Artisan commands.</li>
</ul>
</li>
<li>
<p><b><code>storage</code> :</b> Contains application storage, such as logs, temporary files, and uploaded files.</p>
<ul>
<li><b><code>app</code> :</b> Application-specific files generated by the application.</li>
<li><b><code>framework</code> :</b> Cache, sessions, and views used by the framework.</li>
<li><b><code>logs</code> :</b> Application log files.</li>
</ul>
</li>
<li>
<p><b><code>tests</code> :</b> The tests directory contains your automated tests, such as PHPUnit test cases for the application.</p>
</li>
<li>
<p><b><code>vendor</code> :</b> Contains Composer dependencies.</p>
</li>
<li>
<p><b><code>.env</code> :</b> Environment configuration file.</p>
</li>
<li>
<p><b><code>artisan</code> :</b> Command line utility for interacting with your Laravel application.</p>
</li>
</ul>
</div>
</div>
<div class="sections" id="route">
<h4 class="heading"># Routes</h4>
<div class="content">
<p>A route is a way of mapping HTTP request URIs to a specific controller action or a closure.</p>
<pre class="code-box"><code>Route::get('/view/{id}', function(string $id){
return "User" . $id;
});</code></pre>
<div class="code-box"><code>http://127.0.0.1/view/17</code></div>
<ul>
<li><b><code>/view</code> :</b> It is a route</li>
<li><b><code>/17</code> :</b> It is a routing parameter.</li>
</ul>
<h5>Route Methods :</h5>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Method</th>
<th>Description</th>
</tr>
</thead>
<tbody class="text-center">
<tr>
<td><code>get()</code></td>
<td>Read data</td>
</tr>
<tr>
<td><code>post()</code></td>
<td>Add/Create, Update, Delete</td>
</tr>
<tr>
<td><code>put()</code></td>
<td>Update</td>
</tr>
<tr>
<td><code>patch()</code></td>
<td>Update</td>
</tr>
<tr>
<td><code>delete()</code></td>
<td>Delete</td>
</tr>
<tr>
<td><code>options()</code></td>
<td>Request information about methods supported by server for resource</td>
</tr>
<tr>
<td><code>match()</code></td>
<td>Used to define route that responds to multiple requests</td>
</tr>
<tr>
<td><code>any()</code></td>
<td>Used to define route that responds to all requests</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sections" id="blade">
<h4 class="heading"># Blades</h4>
<div class="content">
<p>Blade is the simple yet powerful templating engine that is included with Laravel. All Blade templates are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to the application. Blade template files use the <code>.blade.php</code> file extension and are typically stored in the <code>resources/views</code> directory.</p>
<h5>Blade Directives :</h5>
<p>In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops called blade directives.</p>
<ul>
<li>
<h6>Displaying Data :</h6>
<pre class="code-box code-box-color"><code>{{ "Time : " . $time }}
{{!! "<h1>I'm Dev</h1>" !!}}</code></pre>
</li>
<li>
<h6>Comment :</h6>
<pre class="code-box code-box-color"><code>{{-- This is comment --}}</code></pre>
</li>
<li>
<h6>Raw PHP :</h6>
<pre class="code-box code-box-color"><code>@php strtoupper("abcd") @endphp</code></pre>
</li>
<li>
<h6>Control Structure :</h6>
<pre class="code-box code-box-color"><code>@if ($age = 18)
$permission = "Complete"
@elseif ($age < 18)
$permission = "Partial"
@else
"Incorrect Age"
@endif</code></pre>
</li>
<li>
<h6>Switch Stamement :</h6>
<pre class="code-box code-box-color"><code>@switch($val)
@case(1)
// Case 1
@break
@case(2)
// Case 2
@break
@default
// Default Case
@endswitch</code></pre>
</li>
<li>
<h6>Loops :</h6>
<pre class="code-box code-box-color"><code>{{-- For Loop --}}
@for ($i = 0; $i < 10; $i++)
Current value is {{ $i }}
@endfor
{{-- For Each --}}
@foreach ($users as $user)
This is user {{ $user->id }}
@endforeach
{{-- While Loop --}}
@while (true)
<p>I'm looping forever.</p>
@endwhile
{{-- For Else --}}
@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse</code></pre>
</li>
<li>
<h6>Loop Control :</h6>
<pre class="code-box code-box-color"><code>@foreach ($users as $user)
@if ($user->type == 1)
@continue
@endif
<li>{{ $user->name }}</li>
@if ($user->number == 5)
@break
@endif
@endforeach</code></pre>
</li>
<li>
<h6>Loop Variables :</h6>
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Property</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>$loop->index</code></td>
<td>Index of current iteration (starts at 0).</td>
</tr>
<tr>
<td><code>$loop->iteration</code></td>
<td>Current iteration (starts at 1).</td>
</tr>
<tr>
<td><code>$loop->remaining</code></td>
<td>Iterations remaining in the loop</td>
</tr>
<tr>
<td><code>$loop->count</code></td>
<td>Total number of items in array</td>
</tr>
<tr>
<td><code>$loop->first</code></td>
<td>Whether this is first iteration</td>
</tr>
<tr>
<td><code>$loop->last</code></td>
<td>Whether this is last iteration</td>
</tr>
<tr>
<td><code>$loop->even</code></td>
<td>Whether this is even iteration</td>
</tr>
<tr>
<td><code>$loop->odd</code></td>
<td>Whether this is odd iteration</td>
</tr>
<tr>
<td><code>$loop->depth</code></td>
<td>Nesting level of current loop</td>
</tr>
<tr>
<td><code>$loop->parent</code></td>
<td>When in nested loop, parent's loop variable</td>
</tr>
</tbody>
</table>
</div>
</li>
<li>
<h6>Empty :</h6>
<pre class="code-box code-box-color"><code>@empty($string)
// Variable, array or collection is Empty
@endempty</code></pre>
</li>
<li>
<h6>Isset :</h6>
<pre class="code-box code-box-color"><code>@isset($records)
// Variable or array key is Set and is Not Null
@endisset</code></pre>
</li>
</ul>
<h5>Passing Data Route to View :</h5>
<h6>Route - <code>web.php</code>
</code></h6>
<pre class="code-box code-box-color"><code>Route::get('/users', function(){
$name = "User Name";
return view('userpage', ['user' => $name]);
});</code></pre>
<h6>Blade - <code>userpage.blade.php</code></h6>
<pre class="code-box code-box-color"><code>{{ "Welcome " . $user }}</code></pre>
</div>
</div>
<div class="sections" id="composer">
<h4 class="heading"># Composer</h4>
<div class="content">
<p>Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on, and it will manage (install or update) them for you. Composer is not a package manager; it is a dependency manager.</p>
<h5>Composer Commands :</h5>
<ul>
<li>
<h6>Create Laravel Project :</h6>
<div class="code-box"><code>composer create-project laravel/laravel [Project-Name]</code></div>
</li>
<li>
<h6>Install Dependency :</h6>
<div class="code-box"><code>composer install</code></div>
</li>
<li>
<h6>Update Dependency :</h6>
<div class="code-box"><code>composer update</code></div>
</li>
</ul>
</div>
</div>
<div class="sections" id="artisan">
<h4 class="heading"># Artisan</h4>
<div class="content">
<p>Artisan is the command-line interface included with Laravel. Artisan exists at the root of your application as the artisan script and provides a number of helpful commands that can assist in building the application.</p>
<h5>Artisan Commands :</h5>
<ul>
<li>
<h6>Run Project :</h6>
<div class="code-box"><code>php artisan serve</code></div>
</li>
<li>
<h6>Create Controller :</h6>
<div class="code-box"><code>php artisan make:controller [Controller-Name]</code></div>
</li>
<li>
<h6>Create Resource Controller :</h6>
<div class="code-box"><code>php artisan make:controller [Resource-Controller] --resource</code></div>
</li>
<li>
<h6>Create Component :</h6>
<div class="code-box"><code>php artisan make:component [input]</code></div>
</li>
<li>
<h6>Create ORM Model :</h6>
<div class="code-box"><code>php artisan make:model [Model]</code></div>
</li>
<li>
<h6>Create Migration Table :</h6>
<div class="code-box"><code>php artisan make:migration [create-user-table]</code></div>
</li>
<li>
<h6>Run Migrations :</h6>
<div class="code-box"><code>php artisan migrate</code></div>
</li>
<li>
<h6>Rollback Migrations :</h6>
<div class="code-box"><code>php artisan migrate:rollback</code></div>
</li>
<li>
<h6>Reset Migrations :</h6>
<div class="code-box"><code>php artisan migrate:reset</code></div>
</li>
<li>
<h6>Refresh Migrations :</h6>
<div class="code-box"><code>php artisan migrate:refresh</code></div>
</li>
<li>
<h6>Create Import/Export Model :</h6>
<pre class="code-box"><code>php artisan make:import [ModelImport] --model=[Model]
php artisan make:export [ModelExport] --model=[Model]</code></pre>
</li>
</ul>
</div>
</div>
<div class="sections" id="database">
<h4 class="heading"># Database</h4>
<div class="content">
<p>Laravel provides two main approaches for interacting with databases: the query builder and the ORM (Object-Relational Mapping) called Eloquent. The configuration for Laravel's database services is located in the application's <code>config/database.php</code> configuration file.</p>
<h5>• Query Builder :</h5>
<p>Laravel Query Builder is a fluent interface for building and running database queries in Laravel without having to write raw SQL statements.</p>
<pre class="code-box"><code>$users = DB::table('users')->select('name', 'email')->get();
$users = DB::table('users')->where('age', '>', 18)->get();
DB::table('users')->insert([
'name' => 'John Doe',
'email' => '[email protected]',
]);</code></pre>
<h5>• Eloquent ORM :</h5>
<p>Laravel's Eloquent ORM is an ActiveRecord implementation for working with databases in PHP. It allows you to interact with your database tables using PHP objects. Each database table has a corresponding "model" that is used to interact with that table.</p>
<h6>User's Model -</h6>
<pre class="code-box"><code><?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasFactory;
protected $table = "users";
protected $primaryKey = "id";
protected $fillable = [
'name',
'email',
'status',
];
}</code></pre>
<h6>User's Controller -</h6>
<pre class="code-box"><code>$users = User::where('id', $id)->get()->toArray();
$result = User::create([
'name' => $name,
'email' => $email,
'status' => 1,
]);
$user = User::find($id);
$user->delete();</code></pre>
</div>
</div>
<!-- Navigation Buttons -->
<nav>
<a href="https://itspatkar.github.io/Cheatsheets/"><i class="bi bi-arrow-left-square-fill"></i></a>
<a href="#" class="float-end"><i class="bi bi-arrow-up-square-fill"></i></a>
</nav>
</div>
<!-- JavaScript -->
<script src="assets/script.js"></script>
</body>
</html>