我正在使用Laravel 5.1
我想在我的数据库中保存动态创建输入的输入值。
我基本上用这一行创建动态输入:
...
for(var i = 1; i<slider.value; i++) {
+'<div class=\"input-group\">'
+'<input name=\"input-chapter-name[]\" name="" type=\"text\">'
+'<input name=\"input-chapter-start[]\" name="" type=\"text\">'
+'<input name=\"input-chapter-end[]\" name="" type=\"text\">'
+'</div>'
}
我的两个模型看起来像这样:
<?php
class Chapters extends Model
{
protected $table = 'chapters';
protected $fillable = ['chapter_name' ,'input-chapter-start', 'input-chapter-end'];
public function product()
{
return $this->belongsTo('App\Product');
}
}
class Product extends Model
{
protected $table = 'products';
protected $fillable = ['email', 'title', 'filename', 'inputMpg', 'number_of_chapters'];
public static $rules = [
'email' => 'required|email|max:50',
'title' => 'required|max:50',
'filename' => 'required|max:50',
'input_mpg' => 'required',
'number_of_chapters' => 'required',
'input-chapter-name' => 'required',
'input-chapter-start' => 'required',
'input-chapter-end' => 'required'
];
public function Chapters()
{
return $this->hasMany('App\Chapters');
}
}
在我的控制器中,我保存(尝试保存)这样的数据:
$product->save();
$Chapters->chapters;
$Chapters->product()->associate($product);
$Chapters->save();
并收到以下错误:
Grammar.php第118行中的ErrorException:参数1传递给 Illuminate \ Database \ Grammar :: parameterize()必须是类型数组, 给定的整数,调用 C:\ XAMPP \ htdocs中\ lariApp \供应商\ laravel \框架的\ src \照亮\数据库\查询\语法\ Grammar.php 在第672行并定义
修改
我当前的控制器看起来像这样:
<?php
class ProductController extends Controller
{
protected $request;
public function request(Request $request)
{
$this->request = $request;
}
public function createProduct(Request $request, $productID)
{
$product = new Product;
$data = $request->only('email', 'title', 'filename', 'number_of_chapters');
# Validate Input
$validator = Validator::make($request->all(), Product::$rules);
# if validation fails return Erros
if($validator->fails())
{
return Redirect::back()->withInput()->withErrors($validator);
}
$product->fill($data);
if($product->save())
{
$product = Product::find($productID);
foreach ($request->input('chapters', []) as $chaptersData) {
$chapters = new Chapters($chaptersData);
$chapters->product()->associate($product);
$chapters->save();
}
return redirect()->route('root')->with('message', 'WIN')->withInput();
}
else
{
return redirect()->route('newProduct')->with('message', 'Error')->withInput();
}
}
}
我的路线:
Route::post('create/{productID}', ['as' => 'createProduct', 'uses' => 'ProductController@createProduct']);
答案 0 :(得分:0)
我认为控制器看起来像:
public function createProduct(Request $request)
{
$data = $request->only('email', 'title', 'filename', 'number_of_chapters');
# Validate Input
$validator = Validator::make($request->all(), Product::$rules);
# if validation fails return Erros
if($validator->fails())
{
return Redirect::back()->withInput()->withErrors($validator);
}
$product = new Product($data);
if($product->save())
{
foreach ($request->input('chapters', []) as $chaptersData) {
$chapters = new Chapters($chaptersData);
$chapters->product()->associate($product);
$chapters->save();
}
return redirect()->route('root')->with('message', 'WIN')->withInput();
}
else
{
return redirect()->route('newProduct')->with('message', 'Error')->withInput();
}
}
视图更像是:
...
for(var i = 0; i < slider.value; i++) {
+'<div class=\"input-group\">'
+'<input name=\"chapters['+i+'][chapter_name]\" type=\"text\">'
+'<input name=\"chapters['+i+'][input-chapter-start]\" type=\"text\">'
+'<input name=\"chapters['+i+'][input-chapter-end]\" type=\"text\">'
+'</div>'
}
这将有助于您创建动态多$章节并与产品模型相关联。
所有您需要的是在路径中传递产品的ID到此控制器操作(在定义链接时):
route('create-multi', $product->id)
或只是
<a href="create-multi/{{$product->id}}">
定义如下:
Route::post('create-multi/{productId}', 'Controller@createMultiChapters')