我对编码和Laravel(使用5.7)非常陌生。我了解了如何创建一个基本的单页表单来填充数据库中的表(包括冲突和身份验证),但是我很难到达下一个级别。 我要设计的应用程序将具有多个表单和表格,我不知道如何将从第一个表单收集的信息链接到下一个表单。
让我们考虑一个具有以下内容的数据库: -客户表(通过表格A填充),例如:一个字段是client_id字段。 -产品表(通过表格B填充) -(最终会更多)
当用户(例如,分析客户行为的公司员工)填写表单页面A(URL:/ clients,GET方法:Clientcontroller @ create,查看clients.create.blade.php)时,他/她单击下一步。
我的想法是,“下一步”按钮应: -将信息提交到Client表(POST方法:Clientscontroller @ store) -并将用户重定向到表单的页面B,并保留URL中的client_id(URL:/ products / {client_id} / create,GET方法:Productscontroller @ create,查看products.create.blade.php)。 / p>
在“产品”表中,我有一个client_id列,并且在“客户和产品”模型之间存在一对多的关联。
提交表单B后,我想从URL检索{client_id}来填写Product表的client_id列,但是我被困在这里。我将不胜感激。为了简化学习过程,我认为客户只能购买一种产品。
主要问题是: -如何从products.create.blade.php视图的URL中检索{client_id}参数,以将其注入到客户端视图中(已经尝试了很多解决Stackoverflow中类似问题的方法) 也: -我使用的方法正确吗?有什么建议/建议吗?
其他问题有点超出范围: -关于如何实施产品添加/删除字段的任何提示?
Web路由文件:
> <?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');
}
答案 0 :(得分:0)
我们的解决方案将从寻找此路线开始:
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');
现在,我们只需使用
就可以访问存储功能上的值$client_id = $request->input('client_id')
答案 1 :(得分:0)
这是一种可行的替代解决方案:
路线:
Route::get('/products/{client_id}/create', 'ProductsController@create')-name('products.create');
Route::post('/products', 'ProductsController@store')->name('products.store');
产品控制器的创建功能:
public function create() //keep as in the original question
{
// all your code ...
return view ('products.create')->with("data",$datas)->with("client_id",$request->client_id);
}
在视图中:
<form>
<!-- other inputs .... -->
<input type="hidden" value="{{Request::segment(2)}}" name="client_id" /> //access the URL segmment 2 which correspond to the client_id
</form>
在产品控制器存储功能中:
$client_id = $request->input('client_id')
像这样直接在存储函数中使用Request :: segment(2)会很好,但是这会触发“非静态方法Illuminate \ Http \ Request :: segment()我无法解决的“静态调用”错误。我不需要这个答案,但是无论如何,还是会得到提示的。如果这个问题尚未解决,我可能会发布一个新问题。