Skip to content

Commit ea01166

Browse files
authored
Merge pull request #4 from TanHongIT/3-update
3 update
2 parents dc4a2fc + 2e923aa commit ea01166

File tree

4 files changed

+109
-67
lines changed

4 files changed

+109
-67
lines changed

app/Http/Controllers/StudentController.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
class StudentController extends Controller
99
{
10-
public function save_student(){
10+
public function save_student()
11+
{
1112
$student = new Student;
1213
$student->name = request()->name;
1314
$student->email = request()->email;
@@ -16,12 +17,25 @@ public function save_student(){
1617
return 'fersgdre';
1718
}
1819

19-
public function all_students(){
20+
public function all_students()
21+
{
2022
$students = Student::paginate(5);
2123
return response()->json($students);
2224
}
23-
public function edit_student($id){
25+
26+
public function edit_student($id)
27+
{
2428
$student = Student::find($id);
2529
return response()->json($student);
2630
}
31+
32+
public function update_student()
33+
{
34+
$student = Student::find(request()->id);
35+
$student->name = request()->name;
36+
$student->email = request()->email;
37+
$student->phone = request()->phone;
38+
$student->update();
39+
return 'done update';
40+
}
2741
}

public/js/app.js

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,7 +2132,6 @@ __webpack_require__.r(__webpack_exports__);
21322132
//
21332133
//
21342134
//
2135-
//
21362135
/* harmony default export */ __webpack_exports__["default"] = ({
21372136
data: function data() {
21382137
return {
@@ -2142,7 +2141,8 @@ __webpack_require__.r(__webpack_exports__);
21422141
phone: "",
21432142
edit_name: "",
21442143
edit_email: "",
2145-
edit_phone: ""
2144+
edit_phone: "",
2145+
id: ""
21462146
};
21472147
},
21482148
mounted: function mounted() {
@@ -2151,31 +2151,51 @@ __webpack_require__.r(__webpack_exports__);
21512151
},
21522152
methods: {
21532153
saveStudent: function saveStudent() {
2154+
var _this = this;
2155+
21542156
axios.post("save_student", {
21552157
name: this.name,
21562158
email: this.email,
21572159
phone: this.phone
2160+
}).then(function (response) {
2161+
_this.getResults(); //show list student after add
2162+
21582163
});
21592164
},
21602165
// Our method to GET results from a Laravel endpoint
21612166
getResults: function getResults() {
2162-
var _this = this;
2167+
var _this2 = this;
21632168

21642169
var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
21652170
axios.get("all_students/?page=" + page).then(function (response) {
21662171
console.log(response.data);
2167-
_this.students = response.data;
2172+
_this2.students = response.data;
21682173
});
21692174
},
21702175
// create method edit student
21712176
editStudent: function editStudent(id) {
2172-
var _this2 = this;
2177+
var _this3 = this;
21732178

21742179
axios.get("edit_student/" + id).then(function (response) {
21752180
console.log(response.data);
2176-
_this2.edit_name = response.data.name;
2177-
_this2.edit_email = response.data.email;
2178-
_this2.edit_phone = response.data.phone;
2181+
_this3.id = response.data.id;
2182+
_this3.edit_name = response.data.name;
2183+
_this3.edit_email = response.data.email;
2184+
_this3.edit_phone = response.data.phone;
2185+
});
2186+
},
2187+
updateStudent: function updateStudent() {
2188+
var _this4 = this;
2189+
2190+
console.log(this.id);
2191+
axios.put("update_student", {
2192+
id: this.id,
2193+
name: this.edit_name,
2194+
email: this.edit_email,
2195+
phone: this.edit_phone
2196+
}).then(function (response) {
2197+
_this4.getResults(); //show list student after update
2198+
21792199
});
21802200
}
21812201
}
@@ -38687,8 +38707,8 @@ var render = function() {
3868738707
_c("div", { staticClass: "modal-content" }, [
3868838708
_vm._m(1),
3868938709
_vm._v(" "),
38690-
_c("div", { staticClass: "modal-body" }, [
38691-
_c("form", [
38710+
_c("form", [
38711+
_c("div", { staticClass: "modal-body" }, [
3869238712
_c("div", { staticClass: "form-group" }, [
3869338713
_c("label", { attrs: { for: "exampleInputEmail1" } }, [
3869438714
_vm._v("Name")
@@ -38771,26 +38791,35 @@ var render = function() {
3877138791
}
3877238792
}
3877338793
})
38774-
]),
38794+
])
38795+
]),
38796+
_vm._v(" "),
38797+
_c("div", { staticClass: "modal-footer" }, [
38798+
_c(
38799+
"button",
38800+
{
38801+
staticClass: "btn btn-secondary",
38802+
attrs: { type: "button", "data-dismiss": "modal" }
38803+
},
38804+
[_vm._v("\n Close\n ")]
38805+
),
3877538806
_vm._v(" "),
3877638807
_c(
3877738808
"button",
3877838809
{
38779-
staticClass: "btn btn-primary",
38780-
attrs: { type: "submit" },
38810+
staticClass: "btn btn-success",
38811+
attrs: { type: "button", "data-dismiss": "modal" },
3878138812
on: {
3878238813
click: function($event) {
3878338814
$event.preventDefault()
38784-
return _vm.saveStudent($event)
38815+
return _vm.updateStudent($event)
3878538816
}
3878638817
}
3878738818
},
38788-
[_vm._v("\n Submit\n ")]
38819+
[_vm._v("\n Save changes\n ")]
3878938820
)
3879038821
])
38791-
]),
38792-
_vm._v(" "),
38793-
_vm._m(2)
38822+
])
3879438823
])
3879538824
]
3879638825
)
@@ -38841,27 +38870,6 @@ var staticRenderFns = [
3884138870
[_c("span", { attrs: { "aria-hidden": "true" } }, [_vm._v("×")])]
3884238871
)
3884338872
])
38844-
},
38845-
function() {
38846-
var _vm = this
38847-
var _h = _vm.$createElement
38848-
var _c = _vm._self._c || _h
38849-
return _c("div", { staticClass: "modal-footer" }, [
38850-
_c(
38851-
"button",
38852-
{
38853-
staticClass: "btn btn-secondary",
38854-
attrs: { type: "button", "data-dismiss": "modal" }
38855-
},
38856-
[_vm._v("\n Close\n ")]
38857-
),
38858-
_vm._v(" "),
38859-
_c(
38860-
"button",
38861-
{ staticClass: "btn btn-primary", attrs: { type: "button" } },
38862-
[_vm._v("Save changes")]
38863-
)
38864-
])
3886538873
}
3886638874
]
3886738875
render._withStripped = true

resources/js/components/CreateComponent.vue

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@
121121
<span aria-hidden="true">&times;</span>
122122
</button>
123123
</div>
124-
<div class="modal-body">
125-
<form>
124+
<form>
125+
<div class="modal-body">
126126
<div class="form-group">
127127
<label for="exampleInputEmail1">Name</label>
128128
<input
@@ -150,26 +150,25 @@
150150
placeholder="Enter Phone"
151151
/>
152152
</div>
153-
153+
</div>
154+
<div class="modal-footer">
154155
<button
155-
type="submit"
156-
v-on:click.prevent="saveStudent"
157-
class="btn btn-primary"
156+
type="button"
157+
class="btn btn-secondary"
158+
data-dismiss="modal"
158159
>
159-
Submit
160+
Close
160161
</button>
161-
</form>
162-
</div>
163-
<div class="modal-footer">
164-
<button
165-
type="button"
166-
class="btn btn-secondary"
167-
data-dismiss="modal"
168-
>
169-
Close
170-
</button>
171-
<button type="button" class="btn btn-primary">Save changes</button>
172-
</div>
162+
<button
163+
type="button"
164+
data-dismiss="modal"
165+
v-on:click.prevent="updateStudent"
166+
class="btn btn-success"
167+
>
168+
Save changes
169+
</button>
170+
</div>
171+
</form>
173172
</div>
174173
</div>
175174
</div>
@@ -188,6 +187,8 @@ export default {
188187
edit_name: "",
189188
edit_email: "",
190189
edit_phone: "",
190+
191+
id: "",
191192
};
192193
},
193194
mounted() {
@@ -196,11 +197,15 @@ export default {
196197
},
197198
methods: {
198199
saveStudent() {
199-
axios.post("save_student", {
200-
name: this.name,
201-
email: this.email,
202-
phone: this.phone,
203-
});
200+
axios
201+
.post("save_student", {
202+
name: this.name,
203+
email: this.email,
204+
phone: this.phone,
205+
})
206+
.then((response) => {
207+
this.getResults(); //show list student after add
208+
});
204209
},
205210
206211
// Our method to GET results from a Laravel endpoint
@@ -214,11 +219,25 @@ export default {
214219
editStudent(id) {
215220
axios.get("edit_student/" + id).then((response) => {
216221
console.log(response.data);
222+
this.id = response.data.id;
217223
this.edit_name = response.data.name;
218224
this.edit_email = response.data.email;
219225
this.edit_phone = response.data.phone;
220226
});
221227
},
228+
updateStudent() {
229+
console.log(this.id);
230+
axios
231+
.put("update_student", {
232+
id: this.id,
233+
name: this.edit_name,
234+
email: this.edit_email,
235+
phone: this.edit_phone,
236+
})
237+
.then((response) => {
238+
this.getResults(); //show list student after update
239+
});
240+
},
222241
},
223242
};
224243
</script>

routes/web.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
Route::get('/home', 'HomeController@index')->name('home');
2121
Route::post('save_student', 'StudentController@save_student');
2222
Route::get('all_students', 'StudentController@all_students');
23-
Route::get('edit_student/{id}', 'StudentController@edit_student');
23+
Route::get('edit_student/{id}', 'StudentController@edit_student');
24+
Route::put('update_student', 'StudentController@update_student');

0 commit comments

Comments
 (0)