Php 未找到laravel列:1054未知列“image\u likes.gallery\u image\u id”
我得到以下错误: 未找到列:1054未知列“image\u likes.gallery\u image\u id” 在“where子句”SQL中:从图像中选择* 1中的image_likes.gallery_image_id和已删除的_at为null和 用户\u id=1且图像\u likes.deleted\u at为空 每当我添加$query函数时 下面的代码行获取数据,但我需要数据来获取与之对应的类 $images=GalleryImage::with'user'->get 这就是我目前所拥有的 ImageLike.php GalleryImage.phpPhp 未找到laravel列:1054未知列“image\u likes.gallery\u image\u id”,php,laravel,Php,Laravel,我得到以下错误: 未找到列:1054未知列“image\u likes.gallery\u image\u id” 在“where子句”SQL中:从图像中选择* 1中的image_likes.gallery_image_id和已删除的_at为null和 用户\u id=1且图像\u likes.deleted\u at为空 每当我添加$query函数时 下面的代码行获取数据,但我需要数据来获取与之对应的类 $images=GalleryImage::with'user'->get 这就是我目前所
在model GalleryImage中更改您的关系以获得喜爱 默认laravel将您的模型名称和_id设为外键,因此它在ImageLike模型中为您的GalleryImage模型查找GalleryImage id,但您有image id。因此,如果您没有默认名称,则在关系中指定它
return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
请在此处查看详细信息
在model GalleryImage中更改您的关系以获得喜爱 默认laravel将您的模型名称和_id设为外键,因此它在ImageLike模型中为您的GalleryImage模型查找GalleryImage id,但您有image id。因此,如果您没有默认名称,则在关系中指定它
return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
请在此处查看详细信息
这不是关于你的问题,但是,你可以用更有效的方式重新写你喜欢的东西
这不是关于你的问题,但是,你可以像这样更有效地重新编写你喜欢的内容你能发布一张从图片中选择*的屏幕截图吗\u likes limit 1;你能发布一张从图像中选择*的屏幕截图吗?@巨魔猫头鹰。。因为默认情况下,Laravel在模型关系上使用foregin键。。您可以向doc查询更多信息:@Troll owl。。因为默认情况下,Laravel在模型关系上使用foregin键。。有关更多信息,请参阅文档:
<?php
namespace App;
use App\User;
use App\GalleryImage;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class GalleryImage extends Authenticatable
{
protected $fillable = [
'image_title',
'user_id',
'file_name',
'created_at'
];
protected $table = 'images';
public function user()
{
return $this->belongsTo(User::class);
}
public function likes()
{
return $this->hasMany(ImageLike::class);
}
public function likedByMe()
{
foreach($this->likes as $like) {
if ($like->user_id == auth()->id()){
return true;
}
}
return false;
}
}
public function likes()
{
return $this->hasMany(ImageLike::class, 'image_id'); //here image_id is reference id of images table to likes table
}
return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
public function likedByMe()
{
return $this->likes()->whereUserId(auth()->id)->count()>0;
}