gpt4 book ai didi

php - “SQLSTATE [42000] : Syntax error or access violation: 1103 Incorrect table name error

转载 作者:行者123 更新时间:2023-12-03 20:17:21 24 4
gpt4 key购买 nike

我正在尝试使用图像和标签的 ID 填充 image_tag 中间表,但是,我不断收到此错误,据我所知,表名不正确,即使它不是。它具有采用无符号整数的 image_id 和 tag_id 列。

“SQLSTATE[42000]:语法错误或访问冲突:1103 不正确的表名 'image_tag'(SQL:插入 image_tag(image_idtag_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');
}
}

最佳答案

你写的'image_tag'中有一个空格:|

return $this->belongsToMany('App\Tag', 'image_tag ','image_id','tag_id');

关于php - “SQLSTATE [42000] : Syntax error or access violation: 1103 Incorrect table name error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48857433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com