[英]I have use correct table but show the error SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name in Laravel 5.1
[英]"SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name error
我正在尝试使用图像和标签的 ID 填充 image_tag 中间表,但是,我不断收到此错误,据我所知,表名不正确,即使它不是。 它具有采用无符号整数的 image_id 和 tag_id 列。
“SQLSTATE[42000]:语法错误或访问冲突:1103 不正确的表名 'image_tag'(SQL:插入image_tag
( image_id
, tag_id
)值( tag_id
))”
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');
}
}
你写的'image_tag'中有一个空格:|
return $this->belongsToMany('App\Tag', 'image_tag ','image_id','tag_id');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.