I'm using Laravel 10 and I have made a table named pages
which stores the all static pages field data of website.
Here is the table:
Now in order to retrieve this data, at the Controller I added this:
use App\Repositories\PageRepository;
use App\Models\Page;
class MainPagesController extends Controller
{
protected $pageRepository;
public function __construct(PageRepository $pageRepository)
{
$this->pageRepository = $pageRepository;
}
public function home()
{
$homeInfo = $this->pageRepository->getPageByType('home');
return view('admin.pages.main.home', compact('homeInfo'));
}
}
So I made a respository named PageRepository.php
which goes like this:
namespace App\Repositories;
use App\Models\Page;
class PageRepository
{
public function getPageByType(string $pageType)
{
return Page::where('pag_type', $pageType)->first();
}
public function getPageByName(string $pageName)
{
return Page::where('pag_name', $pageName)->first();
}
}
So it looks nice and perfect but at the Blade, when I do this:
@dd($homeInfo->where('pag_name', 'hero_image')->first()->pag_value)
I get this error:
Attempt to read property "pag_value" on null
However, the pag_name
with hero_image
has already a value named layer.png
and this error occurs for other pag_name
s as well!
So what's going wrong here? How can I solve this issue?