doudao7511 2018-06-24 04:33
浏览 58
已采纳

未找到laravel列:1054未知列'image_likes.gallery_image_id'

i'm getting the following error:

Column not found: 1054 Unknown column 'image_likes.gallery_image_id' in 'where clause' (SQL: select * from image_likes where image_likes.gallery_image_id in (1) and deleted_at is null and user_id = 1 and image_likes.deleted_at is null)

whenever i add the function ($query).

the line of code below gets the data, but i need the data to get the likes that corresponds with it.

$images = GalleryImage::with('user')->get()

this is what i have so far.

ImageLike.php

<?php

namespace App;

use App\GalleryImage;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ImageLike extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'user_id',
        'image_id'
    ];

}

ImageController.php

public function getImages()
{

    $images = GalleryImage::with('user')
                        ->with(['likes' => function ($query) {
                                    $query->whereNull('deleted_at');
                                    $query->where('user_id', auth()->user()->id);

                        }])->get(); 


    return response()->json($images); 
}

GalleryImage.php

<?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;
    }


}
  • 写回答

2条回答 默认 最新

  • duanqian2368 2018-06-24 04:54
    关注

    Change your relationship in model GalleryImage for likes

    public function likes()
    {
       return $this->hasMany(ImageLike::class, 'image_id'); //here image_id is reference id of images table to likes table
    }
    

    default laravel assume your model name and _id as foreign key so it was looking for gallery_image_id for your GalleryImage model in ImageLike model but you have image_id. So if you have other than default then specific it in relationship.

    return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
    

    check details here

    https://laravel.com/docs/5.6/eloquent-relationships#one-to-many

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法