我想创建一个帖子并且能够添加不同的类别,所以我制作了这段代码
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');
}
模特邮报
public function categories(){return $this->hasMany(\App\Category::class, 'post_id');}
模型类别
public function something(){return $this->belongsTo(\App\Solicitud::class, 'post_id');}
HTML
<form class="form-horizontal" role="form" method="POST" action="{{ route('store_data') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
<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="{{ old('title') }}" />
@if ($errors->has('title'))
<span class="help-block">
<strong>{{ $errors->first('title') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('body') ? ' has-error' : '' }}">
<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="{{ old('body') }}" />
@if ($errors->has('body'))
<span class="help-block">
<strong>{{ $errors->first('body') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<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="{{ old('name') }}" /></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="{{ URL::previous() }}">back</a> <br>
</div>
</div>
</form>
Scritps:
<script>
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<tr id="row'+i+'"><td><input type="text" name="name[]" placeholder="Name" class="form-control name_list" /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">-</button></td></tr>');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
});
</script>
但是我收到了这个错误。当我提交表格时
类型错误:传递给Illuminate \ Database \ Grammar :: parameterize()的参数1必须是数组类型,给定整数,在C:\ xampp \ htdocs \ proyect \ vendor \ laravel \ framework \ src \ Illuminate中调用681行上的\ Database \ Query \ Grammars \ Grammar.php。
“类型错误:传递给Illuminate \ Database \ Grammar :: parameterize()的参数1必须是数组类型,给定整数,在C:\ xampp \ htdocs \ proyect \ vendor \▶”中调用“
如果在输入为空的情况下按提交,我会收到此错误
htmlspecialchars()期望参数1为字符串,给定数组(查看:C:\ xampp \ htdocs \ proyect \ resources \ views \ create.blade.php)
但是如果我编辑代码,就会以这种方式工作但只添加一个类别。
$categories = new \App\Category();
$categories->name = $request->get('name');
//dd($categories);
$post->categories()->save($categories);
我希望我已经解释得很好并且谢谢:)