You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>Go ahead, run it in your terminal. You'll notice it has:</p>
19
19
<ul>
20
20
<li>added an item to the sidebar, in <codeclass="text-primary bg-light p-1 rounded">resources/views/vendor/backpack/base/inc/sidebar_content.blade.php</code></li>
21
21
<li>added a route, inside <codeclass="text-primary bg-light p-1 rounded">routes/backpack/custom.php</code></li>
@@ -28,12 +28,15 @@
28
28
29
29
<divclass="collapse"id="customizeUsersCRUD">
30
30
<p><strong>1. Let's remove the "password" column</strong> - no point in showing it. To do that, go to <codeclass="text-primary bg-light p-1 rounded">UserCrudController::setupListOperation()</code> and remove the line saying <codeclass="text-primary bg-light p-1 rounded">CRUD::column('password');</code>. Easy-peasy, right?</p>
31
-
<p><strong>2. On Create & Update, let's add validation to forms</strong>. There are <ahref="https://backpackforlaravel.com/docs/5.x/crud-operation-create#validation"target="_blank">multiple ways to add validation</a>, but for this simple example, let's just use <ahref="https://backpackforlaravel.com/docs/5.x/crud-operation-create#validating-fields-using-field-attributes"target="_blank">field attribute validation</a>:</p>
31
+
<p><strong>2. On Create & Update, let's add validation to forms</strong>. There are <ahref="https://backpackforlaravel.com/docs/5.x/crud-operation-create#validation"target="_blank">multiple ways to add validation (A, B, C)</a>. Let's change the standard <ahref="https://backpackforlaravel.com/docs/5.x/crud-operation-create#validating-fields-using-formrequests"target="_blank">validation using FormRequests</a> (A), to a simpler validation using <ahref="https://backpackforlaravel.com/docs/5.x/crud-operation-create#validating-fields-using-field-attributes"target="_blank">field attributes</a> (C):</p>
32
32
<ul>
33
-
<li>inside <codeclass="text-primary bg-light p-1 rounded">UserCrudController</code>, let's remove <codeclass="text-primary bg-light p-1 rounded">use App\Http\Requests\UserRequest;</code> from the top;</li>
<li>delete the <codeclass="text-primary bg-light p-1 rounded">App\Http\Requests\UserRequest;</code> file;</li>
38
+
</ul>
39
+
</li>
37
40
<li>a quick way to add validation is to go to <codeclass="text-primary bg-light p-1 rounded">setupCreateOperation()</code> and specify validation rules directly on the fields:
38
41
<p>
39
42
<preclass="text-primary bg-light p-1 rounded">
@@ -44,7 +47,7 @@
44
47
</p>
45
48
</li>
46
49
</ul>
47
-
<p><strong>3. On Create, let's hash the password.</strong> Ok so... now that we have basic validation, if we create a new User, it'll work. But if you look in the database... you'll notice the password is stored in plain text. We don't want that - we want it hashed. There are multiple ways to achieve this - inside the Model, the Request or the CrudController. Let's use Model Events inside UserCrudController. Here's how our <codeclass="text-primary bg-light p-1 rounded">setupCreateOperation()</code> would look, with the validation above rules and us tapping into the <codeclass="text-primary bg-light p-1 rounded">creating</code> event, to hash the password:</p>
50
+
<p><strong>3. On Create, let's hash the password.</strong> Currently, if we create a new User, it'll work. But if you look in the database... you'll notice the password is stored in plain text. We don't want that - we want it hashed. There are <ahref="https://backpackforlaravel.com/docs/5.x/crud-operation-create#use-events-in-your-setup-method"target="_blank">multiple ways to achieve this too</a>. Let's use Model Events inside <codeclass="text-primary bg-light p-1 rounded">setupCreateOperation()</code>. Here's how our method could look, when we also tap into the <codeclass="text-primary bg-light p-1 rounded">creating</code> event, to hash the password:</p>
48
51
<p>
49
52
<preclass="text-primary bg-light p-1 rounded">
50
53
protected function setupCreateOperation()
@@ -59,7 +62,7 @@ protected function setupCreateOperation()
59
62
}
60
63
</pre>
61
64
</p>
62
-
<p><strong>4. On Update, let's not require the password</strong>. It should only be needed if an admin wants to change it, right? That means the validation rules will be different for "password". But it'll also be different for "email" (on Update we need to pass the ID to the unique rule in Laravel). Since 2/3 rules are different, let's just delete what was inside <codeclass="text-primary bg-light p-1 rounded">setupUpdateOperation()</code> and code it from scratch:</p>
65
+
<p><strong>4. On Update, let's not require the password</strong>. It should only be needed if an admin wants to change it, right? That means the validation rules will be different for "password". But then again... the rules will also be different for "email", right? On Update, we need to pass the ID to the unique rule in Laravel. Since 2/3 rules are different, let's just delete what was inside <codeclass="text-primary bg-light p-1 rounded">setupUpdateOperation()</code> and code it from scratch:</p>
63
66
<p>
64
67
<preclass="text-primary bg-light p-1 rounded">
65
68
protected function setupUpdateOperation()
@@ -78,7 +81,9 @@ protected function setupUpdateOperation()
78
81
}
79
82
</pre>
80
83
</p>
81
-
<p>That's it. You have a working Users CRUD. Plus, you've already learned some not-so-easy-to-do things, like using Model events inside CrudController and using field-validation instead of form-request-validation. Of course, this only scratches the surface of what Backpack can do. So we heavily recommend you move on to the next step, and learn the basics.</p>
84
+
<p>
85
+
That's it. You have a working Users CRUD. Plus, you've already learned some advanced techniques, like <ahref="https://backpackforlaravel.com/docs/5.x/crud-operation-create#use-events-in-your-setup-method"target="_blank">using Model events inside CrudController</a> and <ahref="https://backpackforlaravel.com/docs/5.x/crud-operation-create#b-validating-fields-using-a-rules-array"target="_blank">using field-attribute-validation instead of form-request-validation</a>. Of course, this only scratches the surface of what Backpack can do. To really understand how it works, and how you can best use Backpack's features, <strong>we heavily recommend you move on to the next step, and learn the basics.</strong>
0 commit comments