【问题标题】:Laravel save dynamic input in database with foreign keyLaravel 使用外键将动态输入保存在数据库中
【发布时间】:2016-08-22 00:43:07
【问题描述】:

我正在使用 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\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\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']);

【问题讨论】:

  • 118 行是$chapters-&gt;product()-&gt;associate($product); 行吗?
  • 控制器中的 4 行没有意义。这肯定是个问题,不知道你想做什么。
  • 不,第 118 行不是那行

标签: mysql laravel dynamic input laravel-5


【解决方案1】:

我认为控制器应该是这样的:

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-&gt;id)

或者只是

&lt;a href="create-multi/{{$product-&gt;id}}"&gt;

定义如下:

Route::post('create-multi/{productId}', 'Controller@createMultiChapters')

【讨论】:

  • 顺便说一句,我认为您不需要保存产品,因为您需要手动分配chapter_id。先关联产品,然后只保存章节(会自动保存关联的产品)。
  • 是的,我稍微改变了一下。认为您应该保存$chapter,因为它不会自动保存关联(它只会在模型中设置外部而不保存它)。
  • 这只是一个例子:) 你需要以某种方式传递$id 变量。过去你的整个控制器方法。
  • 嗯,好的,你能告诉我你是怎么做的吗,我没那么有经验
  • 好的,我已经更新了条目,所以你可以检查一下你是否更好地理解它。
猜你喜欢
  • 2016-05-05
  • 1970-01-01
  • 2021-09-18
  • 2021-09-18
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-31
相关资源
最近更新 更多