@kokoshneta So I've done these changes:
In class SitePageImage:
<?php
namespace App\Models\Site;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
class SitePageImage extends Model implements HasMedia
{
// TODO: Renamte this to SitePageVariation
use HasFactory;
use InteractsWithMedia;
public function scopeFiltered($query, $theme, $device) {
$query->join('site_variations', 'site_variation_id', 'site_variations.id')
->where('site_variations.theme', $theme)
->where('site_variations.device', $device);
// dd($query);
return $query;
}
public function variation()
{
return $this->belongsTo(SiteVariation::class, 'site_variation_id');
}
public function hi()
{
return "hi";
}
public function registerMediaConversions(Media $media = null): void
{
$this
->addMediaConversion('preview')
->width(100)
->height(100);
}
}
In class SitePage:
<?php
namespace App\Models\Site;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Models\Site\Site;
class SitePage extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug'
];
protected $with = ['color', 'site', 'categoryPage'];
public function scopeFilter($query, array $filters) {
$query->when($filters['color'] ?? false, fn($query, $color) =>
$query->whereHas('site.color', function ($query) use ($color) {
$query->where('name', $color);
})
);
// TODO: Come up with better filter name?
$query->when($filters['pageName'] ?? false, fn($query, $page) =>
$query->whereHas('categoryPage', function ($query) use ($page) {
$query->where('name', $page);
})
);
// with('filteredImageVariations')
$theme = $filters['theme'] ?? 'light';
$device = $filters['device'] ?? 'desktop';
$query->with(['filteredImageVariations' => function($query) use ($theme, $device) {
$query->join('site_variations', 'site_variation_id', 'site_variations.id')
->where('site_variations.theme', $theme)
->where('site_variations.device', $device);
}
]);
return $query;
}
public function filteredImageVariations() {
return $this->imageVariations()->filtered(request('theme'), request('device'));
}
public function imageVariations()
{
return $this->hasMany(SitePageImage::class, 'site_page_id');
}
public function allImages()
{
return $this->hasMany(SitePageImage::class, 'site_page_id');
}
public function color()
{
return $this->belongsTo(SiteBrandColor::class,'site_brand_color_id');
}
public function site()
{
return $this->belongsTo(Site::class, 'site_id');
}
public function categoryPage()
{
return $this->belongsTo(SiteCategoryPage::class, 'site_category_page_id');
}
}
and its controller:
<?php
namespace App\Http\Controllers\Inspiration;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Site\SitePage;
class PageController extends Controller
{
public function index()
{
return view('web.inspiration.pages.index', [
// 'pages' => SitePage::latest()->filter(
// request(['color', 'page', 'device', 'theme'])
// )->paginate(10)->withQueryString()
'pages' => SitePage::with('filteredImageVariations')->latest()->paginate(10)
// $pageId = 514, // Or whatever, just some random page ID that exists
// $page1 = SitePage::filter()->find($pageId),
// $page2 = SitePage::with('imageVariations')->find($pageId),
// // 'pages' =>
// dd($page1, $page2),
// $pageId = 514,
// 'pages' => SitePage::filter()->find($pageId),
// dd($page)
// $pageId = 514,
// 'pages' => SitePage::latest()->filter(request(['device']))->paginate(10)->withQueryString(),
]);
}
}
I did add pagination and such because otherwise it wouldn't work just blank screen, now the image shows with the above code, but how would I filter it now? I did try to change this as it was before with the controller, and instead put this filteredImageVariations in the filter, but that doesn't seem it works.
The image shows with this code though, just the other parts are broken, which is expected based on the code, but then how would we add this back?
'pages' => SitePage::latest()->filter(
request(['color', 'page', 'device', 'theme'])
)->paginate(10)->withQueryString()
I tried to:
$query->with(['filteredImageVariations' => function($query) use ($theme, $device) {
$query->join('site_variations', 'site_variation_id', 'site_variations.id')
->where('site_variations.theme', $theme)
->where('site_variations.device', $device);
}
]);
Which actually, if I live the above code, and keep this in the controller:
'pages' => SitePage::with('filteredImageVariations')->latest()->paginate(10)
It doesn't even matter what I write here lol:
$query->with(['filtesfsdfsdredasdfsdjkfsdImageVariations' => function($query) use ($theme, $device) {
$query->join('site_variations', 'site_variation_id', 'site_variations.id')
->where('site_variations.theme', $theme)
->where('site_variations.device', $device);
}
]);
The image still shows.
I must have been misunderstanding something lol
Oh I see...
The controller reads this code:
public function filteredImageVariations() {
return $this->imageVariations()->filtered(request('theme'), request('device'));
}
Hence the above code with random name doesn't get executed. Makes sense now lol