首页 文章

Laravel使用外键保存数据库中的动态输入

提问于
浏览
1

我正在使用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:传递给Illuminate \ Database \ Grammar :: parameterize()的参数1必须是类型数组,给定整数,在C:\ xampp \ htdocs \ lariApp \ vendor \ laravel \ framework \中调用第672行的src \ Illuminate \ Database \ Query \ Grammars \ Grammar.php并定义

Edit:

我当前的控制器看起来像这样:

<?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']);

1 回答

  • 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')

相关问题