我正在尝试使用图像和标记的ID填充image_tag中间表,但是,我不断收到此错误,据我所知,表示表名不正确,即使它不是。它有列image_id和tag_id,它们采用无符号整数。
“SQLSTATE [42000]:语法错误或访问冲突:1103表名称'image_tag'不正确(SQL:插入image_tag
(image_id
,tag_id
)值(17,12) ))“
public function uploadImage(Request $request){
$this->validate($request, [
'name' => 'required|max:120',
'description' => 'max:120|nullable',
'image' => 'required|max:1999'
]);
$name = $request['name'];
$description = $request['description'];
$tag = $request['tags'];
$userId = auth()->user()->id;
$file = $request->file('image')->getClientOriginalName();
$fileName = pathinfo($file, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
$fileNameToStore = str_replace(' ', '', $fileNameToStore);
$path = $request->file('image')->storeAs('public/images/uploaded', $fileNameToStore);
$image = new Image();
$image->name = $name;
$image->description = $description;
$image->user_id = $userId;
$image->file_name = $fileNameToStore;
$image->save();
$image->tags()->attach($tag);
return redirect()->route('home');
}
图片模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function tags(){
return $this->belongsToMany('App\Tag', 'image_tag ','image_id','tag_id');
}
}
标记模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
public function images(){
return $this->belongsToMany('App\Image', 'image_tag','tag_id','image_id');
}
}
答案 0 :(得分:1)
&#39; image_tag&#39;你写的:|
return $this->belongsToMany('App\Tag', 'image_tag ','image_id','tag_id');