【发布时间】:2019-06-28 10:43:03
【问题描述】:
我正在尝试在数据库中存储多个图像并在视图中显示它们。我有相关的产品表和图像表,在图像表中我有一个名为(图像)的外键。到目前为止,我收到此错误"Array to string conversion"
这里是代码
控制器
public function store(Request $request)
{
$formInput=$request->all();
$image=array();
if($files=$request->file('image')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('images',$name);
$image[]=$name;
}
}
product::create(array_merge($formInput,
['user_id' => Auth::user()->id,
'image' => $image
]));
return redirect()->back();
刀片
<input type="file" name="image[]" multiple class="form-control">
产品.php
public function products()
{
return $this->belongsTo('App\Images', 'image');
}
Images.php
public function images()
{
return $this->hasMany(Product::class, 'image');
}
【问题讨论】:
-
您遇到了什么错误?
-
问题是它存储一个图像我想存储多个@SimonCpan>
-
好的,这就是您正在经历的行为,而预期的行为是不同的。有错误吗?异常,错误代码等?请求是什么样的? Request 对象是否与 HTTP 请求相同?我们需要更多信息。
-
这是
"Array to string conversion"@SimonC 时出现的错误 -
你能告诉你从哪里得到“数组到字符串转换”这个错误吗?
标签: php laravel laravel-5 eloquent