It does not find it?
$main = Category::find(5);
dd($main); //is this null?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I can't figure out where the error is, please help. My BaseController:
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Service;
use App\Models\Slider;
class BaseController extends Controller
{
public function getIndex() {
$main = Category::find(5);
if (empty($main->title)) $title = "{$main->name} - Hello world";
else $title = $main->title;
if (empty($main->description)) $description = "";
else $description = $main->description;
$sliders = Slider::all('img');
$sr1 = Service::where('display', 1)->orderby('order')->take(4)->get();
$sr2 = Service::where('display', 1)->orderby('order')->offset(4)->take(4)->get();
$sr3 = Service::where('display', 1)->orderby('order')->offset(8)->take(4)->get();
return view('welcome', compact('sliders', 'sr1', 'sr2', 'sr3', 'title', 'description'));
}
}
It does not find it?
$main = Category::find(5);
dd($main); //is this null?
null // app/Http/Controllers/BaseController.php:14
@angerfist so no records in the categories table with id 5
@Sinnbeck how to fix ?
@angerfist fill your database table of Category with id 5
@angerfist Insert any dummy data into your Category table and gives its Id : 5 and the rest anything fill it and try
@angerfist what do you mean? You are trying to get a row with id 5.. It does not exist. So add it or change your code to not get id 5. Or make the code fail if it isn't found
$main = Category::findOrFail(5);
@Sinnbeck white screen )
@angerfist 404 page I would assume. That's what the code does
@angerfist set your id to 1 remove 5
@Sinnbeck yes
@angerfist had it worked?
@angerfist maybe explain why you chose 5? I assume you are just trying things?
@Shivamyadav no
@angerfist just put your find id to a dynamic id,
public function getIndex($id) {
$main = Category::find($id);
and set your routes code like this
Route::get('/yourpath/{id}', your contoller as you used);
@angerfist so the table is completely empty?
@angerfist watch this video and do the same
https://www.youtube.com/watch?v=8ZgrP_mJ0cM
@Shivamyadav Too few arguments to function App\Http\Controllers\BaseController::getIndex(), 0 passed in /home/sites/marsik_doctor/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 and exactly 1 expected
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Service;
use App\Models\Slider;
class BaseController extends Controller
{
public function getIndex($id) {
$main = Category::find($id);
if (empty($main->title)) $title = "{ $main->name } - blabla";
else $title = $main->title;
if (empty($main->description)) $description = "";
else $description = $main->description;
$sliders = Slider::all('img');
$sr1 = Service::where('display', 1)->orderby('order')->take(4)->get();
$sr2 = Service::where('display', 1)->orderby('order')->offset(4)->take(4)->get();
$sr3 = Service::where('display', 1)->orderby('order')->offset(8)->take(4)->get();
return view('welcome', compact('sliders', 'sr1', 'sr2', 'sr3', 'title', 'description'));
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BaseController;
use App\Http\Controllers\CallbackFormController;
use App\Http\Controllers\OnlineFormController;
use App\Http\Controllers\ContactFormController;
use App\Http\Controllers\ServiceAreaController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\AboutCenterController;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\OnlineController;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Admin\MainCategoryController;
use App\Http\Controllers\Admin\ServiceController;
use App\Http\Controllers\Admin\SliderController;
use App\Http\Controllers\Admin\GalleryController;
use App\Http\Controllers\Admin\PriceCategoryController;
use App\Http\Controllers\Admin\PriceController;
use App\Http\Controllers\Admin\AdminRedactorController;
//
Route::post('/callback/form', [CallbackFormController::class,'postIndex']);
Route::post('/thanks-page', [OnlineFormController::class, 'postIndex']);
Route::post('/contact/form', [ContactFormController::class, 'postIndex']);
Route::get('/', [BaseController::class, 'getIndex']);
Route::prefix('uslugi')->group(function(){
Route::get('/', [ServiceController::class, 'getIndex']);
Route::get('/{slug}', [ServiceAreaController::class, 'getIndex']);
});
Route::get('/o-tsentre', [AboutCenterController::class, 'getIndex']);
Route::get('/tseny', [PriceController::class, 'getIndex']);
Route::get('/kontakty', [ContactController::class, 'getIndex']);
Route::get('/onlain-zapis', [OnlineController::class, 'getIndex']);
// Авторизация
//Auth::routes();
Route::get('login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('login', [LoginController::class, 'login']);
Route::post('logout', [LoginController::class, 'logout'])->name('logout');
// Админка
Route::prefix('adm')->middleware(['auth', 'auth'])->group(function() {
Route::get('/', [AdminController::class, 'index'])->name('admin');
Route::get('/main-category/choice-category', [MainCategoryController::class, 'mainCategoryList'])->name('main-category-choice');
Route::get('/main-category/edit-form/{id}', [MainCategoryController::class, 'mainCategoryEditForm'])->name('category-edit-form');
Route::post('/main-category/edit/{id}', [MainCategoryController::class, 'mainCategoryEdit'])->name('category-edit');
Route::get('/service/choice-service', [ServiceController::class, 'choiceService'])->name('service-choice');
Route::post('/service/edit-icon/{id}', [ServiceController::class, 'editIcon'])->name('service-edit-icon');
Route::get('/service/edit-form/{id}', [ServiceController::class, 'serviceEditForm'])->name('service-edit-form');
Route::post('/service/edit/{id}', [ServiceController::class, 'editService'])->name('service-edit');
Route::get('/slider', [SliderController::class, 'viewSlides'])->name('slides');
Route::post('/slider/add', [SliderController::class, 'addSlide'])->name('slide-add');
Route::post('/slider-edit/{id}', [SliderController::class, 'editSlide'])->name('slide-edit');
Route::get('/slide-del/{id}', [SliderController::class, 'deleteSlideElement'])->name('slide-delete');
Route::get('/gallery', [GalleryController::class, 'viewGallery'])->name('gallery');
Route::post('/gallery/add', [GalleryController::class, 'addGallery'])->name('gallery-add');
Route::post('/gallery-edit/{id}', [GalleryController::class, 'editGallery'])->name('gallery-edit');
Route::get('/gallery-del/{id}', [GalleryController::class, 'deleteGalleryElement'])->name('gallery-delete');
Route::get('/price-category/list', [PriceCategoryController::class, 'priceCategoryList'])->name('price-category-list');
Route::get('/price-category/edit-form/{id}', [PriceCategoryController::class, 'priceCategoryEditForm'])->name('price-category-edit-form');
Route::post('/price-category/edit/{id}', [PriceCategoryController::class, 'priceCategoryEdit'])->name('price-category-edit');
Route::get('/price-category/add-form', [PriceCategoryController::class, 'priceCategoryAddForm'])->name('price-category-add-form');
Route::post('/price-category/add', [PriceCategoryController::class, 'priceCategoryAdd'])->name('price-category-add');
Route::get('/price-category/del/{id}', [PriceCategoryController::class, 'priceCategoryDelete'])->name('price-category-delete');
Route::get('/price-choice', [PriceController::class, 'choiceService'])->name('price-choice');
Route::get('/price-edit/{id}', [PriceController::class, 'viewPrices'])->name('price-view');
Route::post('/price-edit/edit/{id}', [PriceController::class, 'editPrice'])->name('price-edit');
Route::get('/price-add', [PriceController::class, 'priceAddForm'])->name('price-add-form');
Route::post('/price-add/add', [PriceController::class, 'addPrice'])->name('price-add');
Route::get('/price-del/{id}', [PriceController::class, 'deletePriceElement'])->name('price-delete');
Route::get('/admin/add-form', [AdminRedactorController::class, 'adminAddForm'])->name('admin-add-form');
Route::post('/admin/add', [AdminRedactorController::class, 'adminAdd'])->name('admin-add');
Route::get('/admin/list', [AdminRedactorController::class, 'adminList'])->name('admin-list');
Route::get('/admin/edit-form/{id}',[AdminRedactorController::class, 'adminEditForm'])->name('admin-edit-form');
Route::post('/admin/edit/{id}', [AdminRedactorController::class, 'adminEdit'])->name('admin-edit');
Route::get('/admin/del/{id}', [AdminRedactorController::class, 'adminDelete'])->name('admin-delete');
});
@angerfist Hey Buddy : Sorry for late reply :) , You have missed to pass a data from the routes change this line in your web.php file
Route::get('/', [BaseController::class, 'getIndex']);
to this
Route::get('/{id}', [BaseController::class, 'getIndex']);
@Shivamyadav no work, 404 not found
@angerfist show your blade code where you have used this routes to click and open the link
@angerfist try this code typing in your url after your server start name /1 and enter then what it gives you
@Shivamyadav Be aware that if the table is empty, this wont work (finding id 1 failed, so I bet its empty)
@Shivamyadav .blade.php
@extends('layouts.base')
@section('content')
@include ('includes.slider')
@include ('includes.services_slider')
<section class="main-page_our-advantage text-area">
<div class="container">
<h2>1111</h2>
<div class="main-page_our-advantage-bl">
<div class="main-page_our-advantage-element">
<div class="main-page_our-advantage_img">
<img src="{{ asset('img/our_vibor.png') }}">
</div>
<div class="main-page_our-advantage_title">
1111
</div>
</div>
<div class="main-page_our-advantage-element">
<div class="main-page_our-advantage_img">
<img src="{{ asset('img/our_oborud.png') }}">
</div>
<div class="main-page_our-advantage_title">
1111
</div>
</div>
<div class="main-page_our-advantage-element">
<div class="main-page_our-advantage_img">
<img src="{{ asset('img/our_personal.png') }}">
</div>
<div class="main-page_our-advantage_title">
111
</div>
</div>
<div class="main-page_our-advantage-element">
<div class="main-page_our-advantage_img">
<img src="{{ asset('img/our_price.png') }}">
</div>
<div class="main-page_our-advantage_title">
111
</div>
</div>
<div class="main-page_our-advantage-element">
<div class="main-page_our-advantage_img">
<img src="{{ asset('img/our_raspol.png') }}">
</div>
<div class="main-page_our-advantage_title">
111
</div>
</div>
<div class="main-page_our-advantage-element">
<div class="main-page_our-advantage_img">
<img src="{{ asset('img/our_rezhim.png') }}">
</div>
<div class="main-page_our-advantage_title">
1111
</div>
</div>
</div>
</div>
</section>
<section class="main-page_map text-area">
<div class="container">
<h2>kjhjkhkj</h2>
<p><strong>blabla </p>
</div>
</section>
@endsection
@section('css')
@parent
<link href="{{ asset('css/rslides.css') }}" rel="stylesheet">
@endsection
@section('scripts')
@parent
<script src="{{ asset('js/responsiveslides.js') }}"></script>
<script>
$(function() {
$(".rslides").responsiveSlides({
auto: true, // Анимация автоматически, true или false
speed: 2000, // Скорость смены
timeout: 5000, // Время между переходами
pager: false, // Показывать нумерацию слайдов
nav: true, // Показывать навигацию, true или false
random: false, // Случайный показ слайдов true или false
pause: false, // Пауза при наведении true или false
pauseControls: true, // Пауза при наведении на кнопки навигации
prevText: "<img src='/img/slider_left.png'>", // Текст кнопки "Назад"
nextText: "<img src='/img/slider_right.png'>", // Текст кнопки "Следующий"
maxwidth: "", // Максимальная ширина слайдера
navContainer: "", // Контейнер слайдера, по умолчанию 'ul'
manualControls: "", // Точки навигации
namespace: "links", // класс слайдера
before: function(){}, // Function: Before callback
after: function(){} // Function: After callback
});
});
</script>
<script src="{{ asset('js/jquery.bxslider.js') }}"></script>
<script type="text/javascript">
$(document).ready(function(){$('.bxslider').bxSlider();});
</script>
@endsection
@Shivamyadav Attempt to read property "name" on null
@angerfist thats the thing you do not have any data in your table watch this video and do as tell in the video and then try to run the route you will never get error ...
th video link https://www.youtube.com/watch?v=8ZgrP_mJ0cM
What @sinnbeck said, and watch this free series https://laracasts.com/series/laravel-8-from-scratch
Please or to participate in this conversation.