laravel-5.7 - Laravel - 多个表单/表格 - 在 URL 中携带变量以填充表格列
问题描述
我对编码和 Laravel(使用 5.7)很陌生。我了解如何创建一个基本的单页表单来填充数据库中的表,包括 relashionship 和身份验证,但我很难达到下一个层次。 我要设计的应用程序将有多个表单和表格,我无法弄清楚如何将从第一个表单收集的信息链接到下一个表单。
让我们考虑一个数据库: - 一个客户表(通过表单页面 A 填充)例如:一个字段是 client_id 字段。- 产品表(通过表单页面 B 填充) - (最终它们会更多)
当用户(例如分析客户行为的公司的员工)完成填写表单页面 A(URL:/clients,GET 方法:Clientcontroller@create,查看 clients.create.blade.php)时,他/她单击下一步。
我的想法是 Next 按钮应该: - 将信息提交到 Client 表(POST 方法:Clientscontroller@store) - 并将用户重定向到表单的页面 B,在 URL 中携带 client_id(URL:/products/ {client_id}/create,GET 方法:Productscontroller@create,查看 products.create.blade.php)。
在 Product 表中,我有一个 client_id 列,并且我在 Clients 和 Products 模型之间有一对多的关系。
提交表单 B 后,我想从 URL 中检索 {client_id} 以填充 Product 表的 client_id 列,但我被困在这里。我将不胜感激指导和建议。为了简化学习过程,我认为客户只购买一种产品。
主要问题是: - 如何从 products.create.blade.php 视图的 URL 中检索 {client_id} 参数以将其注入客户端视图(已经尝试了很多事情,从回答 stackoverflow 中的类似问题)也: - 我使用正确的方法吗?有什么建议/建议吗?
额外的问题有点超出范围: - 关于如何为产品实施添加/删除字段的任何提示?
网络路由文件:
> <?php
> Route::get('/', 'PagesController@welcome')->name('welcome');
> Auth::routes();
> //ROUTES FOR CLIENT
> Route::resource('clients','ClientsController');
> //ROUTES FOR PRODUCT (please note the {client_id} parameter)
> Route::get('/products/{client_id}/create', 'ProductsController@create')->name('products.create');
> Route::post('/products/{client_id}', 'ProductsController@store')->name('products.store');
> //not sure if it should be Route::post('/products', 'ProductsController@store')->name('products.store');
客户端控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Client; //THIS IS THE CLIENT MODEL
use App\User; ////THIS IS THE USER MODEL
class ClientsController extends Controller
{
AUTH
public function __construct()
{
$this->middleware('auth');
}
//INDEX (CLIENT)
public function index()
{
//my code here
}
//CREATE (CLIENT)
public function create()
{
$datas=[
'controllermethod' => 'ClientsController@store',
'client_id_field' => [
'input_type_l' => 'textinput',
'input_name_l' => 'client_id', //this what i am carrying over in URL
'input_label_l' => 'Client ID :',
'input_placeholder_l' => 'XXXXX',
'input_default_l' => ''
],
'client_info_1_field' => [
'input_type_l' => 'textinput',
'input_name_l' => 'client_info_1'
'input_label_l' => 'Client Info 1 :',
'input_placeholder_l' => 'XXXXX',
'input_default_l' => ''
],
'client_info_2_field' => [
'input_type_l' => 'textinput',
'input_name_l' => 'client_info_2'
'input_label_l' => 'Client Info 2 :',
'input_placeholder_l' => 'XXXXX',
'input_default_l' => ''
]
],
return view ('clients.create')->with($datas);
}
//STORE (CLIENT)
public function store(Request $request)
{
$this->validate($request, [
'client_id' => 'required',
]);
$client = new Client;
$client->client_id = $request->input('client_id');
$client->client_info_1 = $request->input('client_info_1');
$client->client_info_2 = $request->input('client_info_2');
$client->user_id = auth()->user()->id; //one to many relashionship betw user/client models
$client->save();
//
//this is how I inject the {client_id} parameter onto the URL
//this works, the products.create view is displayed, the URL contain the client_id from formA entered by the user
$client_id = $request->client_id;
return redirect("/products/$client_id/create")->with('client_id', $client_id)
->with('success','New client record created');
//SHOW/DESTROY/EDIT/UPDATE FUNCTIONS....all this work
产品控制器 = 那是我被卡住了 + 不确定这是否是正确的方法
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//note sure if I need that
//use Illuminate\Routing\Route;
use App\Product;
use App\Client;
use App\User;
class ProductsController extends Controller
{
//AUTH
public function __construct()
{
$this->middleware('auth');
}
//INDEX (PRODUCT)
public function index()
{
//no code so far
}
//INDEX (PRODUCT)
public function create()
{
$datas=[
'controllermethod' => 'ProductsController@store',
//planning of having dynamic add/remove products fields but let's keep it simple (1 client -> 1 product)
'product_id_field' => [
'input_type_l' => 'textinput',
'input_name_l' => 'product_id',
'input_label_l' => 'Date of cancer diagnosis :',
'input_default_l' => ''
],
'product_info_1_field' => [
'input_type_l' => 'textinput',
'input_name_l' => 'product_info_1'
'input_label_l' => 'Product Info 1 :',
'input_placeholder_l' => 'XXXXX',
'input_default_l' => ''
],
'product_info_2_field' => [
'input_type_l' => 'textinput',
'input_name_l' => 'product_info_2'
'input_label_l' => 'Product Info 2 :',
'input_placeholder_l' => 'XXXXX',
'input_default_l' => ''
]
],
//Below, I am not sure I should do that
return view ('products.create')->with($datas);
}
// STORE (PRODUCT) = THAT's WHERE I AM STUCK
// everything works except that the client_id column in the products table stays empty or filled with crap
public function store(Request $request)
{
//NOT SURE WHAT TO DO HERE TO RETRIEVE THE {client_id} from the "/products/$client_id/create" ...
//TO FURTHER INJECT IT AS A VALUE IN THE client_id COLUMN OF THE PRODUCT TABLE
//I KNOW IT's CRAP, but I TRIED THINGS ALONG THOSE LINES:
//return $client_id;
//$client_id = request()->route()->paremeters('client_id');
//$client_id = request()->route('client_id');
//$client_id = $request->client_id;
//$client_id = url('client_id');
$product = new Diagnosis;
$product->product_id = $request->input('product_id');
$product->product_info_1 = $request->input('product_info_1');
$product->product_info_2 = $request->input('product_info_2');
$product->user_id = auth()->user()->id;
//I KNOW IT's CRAP, but I TRIED THINGS ALONG THOSE LINES:
//$diagnosis->client_id = $client_id; //NB: if I write $diagnosis->client_id = 'whatever'; it works
$diagnosis->save();
//redirect to client.index view
return redirect('/clients')->with('success','New patient diagnosis (-es) created');
}
解决方案
我们的解决方案将从查看这条路线开始:
Route::get('/products/{client_id}/create','ProductsController@create')->name('products.create');
这是包含我们要检索的您的 client_id 的路由,并且由于该值在链接上,我们可以使用 Request 对象来访问它!
就像我说的,$request 对象已经包含 client_id 值,所以我们需要检索它并使用 with 函数将其作为参数发送到视图,所以基本上你的 ProductsController@create 将是这样的:
public function create(Request $request)
{
// all your code ...
return view ('products.create')->with("data",$datas)->with("client_id",$request->client_id);
}
所以现在,我们从我们的视图中访问这个值对吗?这个想法是将此值添加为产品表单上的隐藏输入!(像这样的东西)
<form>
<!-- other inputs .... -->
<input type="hidden" value="{{$client_id}}" name="client_id" />
</form>
提交表单后,将调用此路由:
Route::post('/products/{client_id}', 'ProductsController@store')->name('products.store');
由于 client_id 将通过表单发送而不使用链接,因此最好将路由更改为
Route::post('/products', 'ProductsController@store')->name('products.store');
现在,我们可以通过使用来访问我们在 store 函数中的值
$client_id = $request->input('client_id')
推荐阅读
- python - 对于列表 L 中的每个项目,在数据框中找到所有相应的项目
- javascript - 使用带像素的背景位置的中心图像
- python - 将 TFlearn 模型转换为 Keras - Python 3.7
- webstorm - WebStorm 与 Visual Studio 中的调试
- sql-server - 使用 LEFT() 和 SUBSTRING() 从字符串中只提取这么多字符
- javascript - 添加新项目已加载,指示器图像在项目从 SharePoint 中的文件夹加载到网页后
- node.js - TypeError:无法读取猫鼬上未定义的属性“查找”
- node.js - 找不到模块'sqlite3'
- php - Laravel 中的 Slack 通知 - to() 函数什么都不做?
- python - 如何使用 discord.py 获取服务器 ID?