mercredi 27 avril 2016

Laravel save dynamic input in database with foreign key

I am using Laravel 5.1

I want to save inputs value of dynamic created inputs in my database.

I basically create the dynamic inputs with this line:

...
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>'
}

My two models look like this:

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

}

And in my Controller I save (try to save) the data like this:

 $product->save();
 $Chapters->chapters;
 $Chapters->product()->associate($product);
 $Chapters->save();

And get the following error:

ErrorException in Grammar.php line 118: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, integer given, called in C:\xampp\htdocs\lariApp\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 672 and defined



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1ri4vIk
via IFTTT

Aucun commentaire:

Enregistrer un commentaire