例如,如果有一個創建客戶的表單,當您填寫表單并提交頁面時,頁面應該重定向并顯示通過特定個人 ID 插入該表單的數據。在我的例子中,它重定向到一個頁面并查看數據庫中的所有數據。這是我的Web.phpRoute::get('customers','CustomersController@index');Route::get('customers/create','CustomersController@create');Route::post('customers','CustomersController@store');Route::get('customers/{customer}','CustomersController@show');這是我的控制器<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\Customer;use App\Company;class CustomersController extends Controller{ public function index(){ $customers = Customer::all(); return view('customers.index',compact('customers')); } public function create(){ $companies = Company::all(); return view ('customers.create',compact('companies')); } public function store() { $data = request()->validate([ 'name'=>'required | min:3', 'email'=>'required | email', 'active'=>'required ', 'company_id'=>'required', ]); Customer::create($data); return redirect('customers'); } // * Route Model binding public function show(Customer $customer){ return view('customers.show',compact('customer')); }}
1 回答

aluckdog
TA貢獻1847條經驗 獲得超7個贊
重定向到show路由而不是客戶index路由:
$customer = Customer::create($data);
return redirect('customers/' . $customer->id);
- 1 回答
- 0 關注
- 100 瀏覽
添加回答
舉報
0/150
提交
取消