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
whereimage_likes
.gallery_image_id
in (1) anddeleted_at
is null anduser_id
= 1 andimage_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;
}
}