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
-
<ul>
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
-
<li>added a route, inside <codeclass="text-primary bg-light p-1 rounded">routes/backpack/custom.php</code></li>
<p>You can now click on the new sidebar item (or <ahref="{{backpack_url('user') }}">here</a>) and you'll be able to see the entries in the <codeclass="text-primary bg-light p-1 rounded">users</code> table. Even though generated CRUDs work out-of-the-box, they might not be <i>exactly</i> what you need. But that's where Backpack shines, in how easy it is to customize.</p>
18
+
<p>Go ahead, run it in your terminal. You can now click on the new sidebar item (or <ahref="{{backpack_url('user') }}">here</a>) and you'll be able to see the entries in the <codeclass="text-primary bg-light p-1 rounded">users</code> table. Even though generated CRUDs work out-of-the-box, they might not be <i>exactly</i> what you need. But that's where Backpack shines, in how easy it is to customize.</p>
26
19
27
-
<p>To dig a little deeper, <ahref="#"data-toggle="collapse"data-target="#customizeUsersCRUD"aria-expanded="true"aria-controls="customizeUsersCRUD">let's make a few changes to the Users CRUD</a>.</p>
20
+
<p>To dig a little deeper, <ahref="#"data-toggle="collapse"data-target="#customizeUsersCRUD"aria-expanded="true"aria-controls="customizeUsersCRUD">let's make a few changes to the Users CRUD <iclass="la la-angle-double-right"></i></a></p>
28
21
29
22
<divclass="collapse"id="customizeUsersCRUD">
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>
23
+
<p><strong>1. When Listing, let's remove the "password" column</strong> - no point in showing the hash. 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
24
<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
25
<ul>
33
26
<li>to remove the current validation:<br>
@@ -39,17 +32,17 @@
39
32
</li>
40
33
<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:
<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>
@@ -60,11 +53,11 @@ protected function setupCreateOperation()
60
53
$entry->password = \Hash::make($entry->password);
61
54
});
62
55
}
63
-
</pre>
56
+
</code></pre>
64
57
</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>
58
+
<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" (because 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>
@@ -79,7 +72,7 @@ protected function setupUpdateOperation()
79
72
}
80
73
});
81
74
}
82
-
</pre>
75
+
</code></pre>
83
76
</p>
84
77
<p>
85
78
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>
@@ -116,3 +109,12 @@ protected function setupUpdateOperation()
116
109
<pclass="mt-3 mb-0"><small>* this card is only visible on <i>localhost</i>. Follow the last step to hide it from <i>localhost</i> too.</small></p>
0 commit comments