 |
[Laravel,SQLserv] หลังจากกด Save Laravel ไม่บันทึกลง Database ไม่ redirect ไปหน้า user |
|
 |
|
|
 |
 |
|
หลังจากกด Save Laravel ไม่บันทึกลง Database ไม่ redirect ไปหน้า user รบกวนช่วยดูด้วยค่ะ
หน้าแรก
Code (PHP)
http://localhost:8000/user
Home page
Add
//Route
Code (PHP)
Route::resource('user','UserController');
//Controller
Code (PHP)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
/**
* แสดงผล
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view ('user.index');
}
/**
* เพิ่มข้อมูล
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view ('user.create');
}
/**
* บันทึกข้อมูล
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,['fname_sname'=>'required','user_email'=>'required','status'=>'required']);
$user = new User(
[
'fname_sname'=>$request->get('fname_sname'),
'user_email'=>$request->get('user_email'),
'status'=>$request->get('status')
]);
$user->save();
return redirect()->route('user.index')->with('success','Save Success');
}
}
//Model
Code (PHP)
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $filable=['fname_sname','user_email','status'];
}
//Database
Code (PHP)
SELECT TOP (1000) [emp_id]
,[fname_sname]
,[user_email]
,[status]
,[create_user]
,[update_user]
,[created_at]
,[updated_at]
FROM [db_laravel].[dbo].[users]
Tag : PHP, HTML5, Laravel Framework, Apache
|
ประวัติการแก้ไข 2020-03-13 13:27:40 2020-03-13 13:28:15
|
 |
 |
 |
 |
Date :
2020-03-13 13:27:01 |
By :
nottpoo |
View :
1793 |
Reply :
9 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอา error มาครับ
|
 |
 |
 |
 |
Date :
2020-03-13 16:57:20 |
By :
mongkon.k |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใน Controller function store
ลองพิมพ์ dd($request->all());
แล้วลองดู ข้อมูลเข้า method รึเปล่า
ดึกๆเดี๋ยวมาเขียนแบบ tinker ให้ดูมันสะดวกกว่า
|
ประวัติการแก้ไข 2020-03-13 18:58:47
 |
 |
 |
 |
Date :
2020-03-13 18:57:22 |
By :
Genesis™ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
'fname_sname'=>$request->fname_sname',
'user_email'=>$request->user_email,
'status'=>$request->status
Store CRUD ไม่ได้รับเป็น Get แต่รับเป็น Post
หน้า form ใส่ method post ด้วย แล้วก็อย่าลืมใส่ csrf
|
 |
 |
 |
 |
Date :
2020-03-13 19:10:00 |
By :
Genesis™ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดูพื้นฐาน HTML ก่อนไหมว่า ลิงค์ action ตอน submit มันไปที่ไหน
<form method="post" action="{{url('user')}}">
กดแล้ว ไม่ ลง database ไม่ ลิงค์ ไม่โชว์ คือมันไปไหน ต้องรู้ตรงนี้ให้ได้
ที่ง่ายที่สุด เป็น view source บน browser ดูก่อนว่า มัน generate ได้ตามต้องการไหม
|
 |
 |
 |
 |
Date :
2020-03-17 11:08:44 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 9 เขียนโดย : nottpoo เมื่อวันที่ 2020-03-17 14:38:48
รายละเอียดของการตอบ ::
พยายามเปลี่ยนการใช้ route group resource มาเป็น route as มันจะดูง่ายกว่า และมันจะตรงกับ Verbs RESTful API คุณจะได้ไม่งง เช่น
web.php
// index
Route::get('/informations',[
'as' => 'informations.index',
'uses' => 'InformationsController@index',
]);
// create
Route::get('/informations',[
'as' => 'informations.create',
'uses' => 'InformationsController@create',
]);
// store
Route::post('/informations',[
'as' => 'informations.store',
'uses' => 'InformationsController@store',
]);
// show
Route::get('/informations/{id}',[
'as' => 'informations.show',
'uses' => 'InformationsController@show',
]);
// edit
Route::get('/informations/{id}/edit',[
'as' => 'informations.edit',
'uses' => 'InformationsController@edit',
]);
// update
Route::put('/informations/{id}',[
'as' => 'informations.update',
'uses' => 'InformationsController@update',
]);
//destroy
Route::delete('/informations/{id}',[
'as' => 'informations.destroy',
'uses' => 'InformationsController@destroy',
]);
ถ้าคุณเขียนในลักษณะ resource รวมในอนาคตถ้ามี Route เยอะๆ เดี๋วคุณจะแยกลำบากครับ Fight !!
|
ประวัติการแก้ไข 2020-03-17 15:56:57
 |
 |
 |
 |
Date :
2020-03-17 15:55:25 |
By :
Genesis™ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|