John Does Legacy John Does Legacy - 7 months ago 286
SQL Question

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


Edit:

my current controller looks like this:

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

}

}


And my route:

Route::post('create/{productID}', ['as' => 'createProduct', 'uses' => 'ProductController@createProduct']);

Answer

I think that controller should looks like:

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();
    }

}

where view is more like that:

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

This will help You create dynamiclly multi $chapters and associate with product model's.

All You need is to pass product's id in in the route to this controller action (while defining the link):

route('create-multi', $product->id)

or just

<a href="create-multi/{{$product->id}}">

which is defined like:

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