vendredi 23 février 2018

Laravel multiple insert in a table

I want to create a post and be able to add different categories, so I made this code

  public function store(Request $request)
{
  $this->validate($request,[
    'title' => 'required',
    'body' => 'required',
    'name'=>'required'
  ]);

  $post = new \App\Post([
      'title' => $request->get('title'),
      'body' => $request->get('body')
    ]);
     $post->save();

     foreach ($request->name as $data) {
       $categories = new \App\Category();
       $categories->name = $request->get('name');
       //dd($categories);
      $post->categories()->save($categories);
     }
      return redirect()->route('create');
}

model Post

public function categories(){return $this->hasMany(\App\Category::class, 'post_id');}

model Category

public function something(){return $this->belongsTo(\App\Solicitud::class, 'post_id');}

HTML

<form class="form-horizontal" role="form" method="POST" action="">
                    
                    <div class="form-group">
                        <label for="title" class="col-md-4 control-label">title:</label>
                        <div class="col-md-6">

                      <input type="text" name="title" placeholder="Cantidad" class="form-control"  value="" />

                            @if ($errors->has('title'))
                                <span class="help-block">
                                    <strong></strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="body" class="col-md-4 control-label">Body:</label>
                        <div class="col-md-6">

                          <input type="text" name="body" placeholder="Cantidad" class="form-control"  value="" />

                            @if ($errors->has('body'))
                                <span class="help-block">
                                    <strong></strong>
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="form-group">
                      <table class="table table-bordered" id="dynamic_field">
                        <thead>
                          <tr>
                          </tr>
                        </thead>
                        <tbody>
                          <tr>
                            <td><input type="text" name="name[]" placeholder="Name" class="form-control name_list"  value="" /></td>
                            <td><button type="button" name="add" id="add" class="btn btn-success"> + </button></td>
                          </tr>
                        </tbody>
                       </table>
                    </div>
                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                Create
                            </button>
                        </div>
                        <div class="col-md-6 col-md-offset-4">
                          <a class="btn btn-info" href="">back</a> <br>
                        </div>
                    </div>
                  </form>

but I get this error. when I submit the form

Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, integer given, called in C:\xampp\htdocs\proyect\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 681.

"Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, integer given, called in C:\xampp\htdocs\proyect\vendor\ ▶"

and if press submit with the input empty I get this error

htmlspecialchars() expects parameter 1 to be string, array given (View: C:\xampp\htdocs\proyect\resources\views\create.blade.php)

but if I edit the code, in this way it works but adds only one category.

$categories = new \App\Category();
       $categories->name = $request->get('name');
       //dd($categories);
      $post->categories()->save($categories);

I hope I have explained well and thanks :)



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

Aucun commentaire:

Enregistrer un commentaire