Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

graeme14's avatar

Components on same Blade file

In something of an update to a thread I created last week, I found the problem wasn't my database readouts, but actually trying to call two blade components on the same page.

I have no idea what I'm doing wrong, but Laravel doesn't seem to like my attempts to display 'Jobs' and 'Skills' on one blade file. I have to have one or the other, but not both otherwise I end up with an "Undefined Variable" problem with either my 'skills' or 'jobs' components.

Here is the code:

About:Blade

	@include('components/skills')
	
</div>
	@include('components/jobs')

</div>

Jobs:blade

@foreach ($jobs as $job)

		<h4 class="is-size-4 has-text-weight-semibold my-2">
			<span class="icon  mr-4"><i class="{{ $job->icon }} "></i></span><br>
			{{ $job->jobTitle }}
		</h4>
		<p class="is-size-6 "> {{ $job->description }} </p>

@endforeach

Skills:blade

@foreach ($skills as $skill)
		
		<div class="progress-wrapper" x-data="{ value: ' ' }">
			
			<p> {{ $skill->name }} <span class="icon"><i class=" {{ $skill->icon }} "></i></span></p>
				
			<progress id="progress-bar" class="progress is-small is-info show-value" x-model=' value=" {{ $skill->value }} " ' max="100" ></progress>
				
			<p x-text="value" class="progress-value"></p>
			
		</div>

	@endforeach

JobsController:

namespace App\Http\Controllers;

use Illuminate\Http\Request; use App\Models\Jobs;

class JobsController extends Controller { // function showJobs() { $data = Jobs::all(); return view('pages.about', ['jobs'=>$data]); } }

SkillsController:

namespace App\Http\Controllers;

use Illuminate\Http\Request; use App\Models\Skills;

class SkillsController extends Controller { // function showSkills() { $details = Skills::all(); return view('pages.about', ['skills'=>$details]); } }

web.php

use App\Http\Controllers\JobsController; use App\Http\Controllers\SkillsController;

Route::get('/about', function() { return view('pages.about'); }); //jobs page

Route::get('about', [SkillsController::class,'showSkills']); // skills component

Route::get('about', [JobsController::class,'showJobs']); // jobs component

Looking for a solution, many thanks

0 likes
18 replies
vincent15000's avatar

You are defining three times the same route, it can't work.

Route::get('/about', function() { return view('pages.about'); }); //jobs page
Route::get('about', [SkillsController::class,'showSkills']); // skills component
Route::get('about', [JobsController::class,'showJobs']); // jobs component

Be aware of something important : one route / one controller / one action. You can't hope that one route will execute two different actions in two different controllers.

If you want to display the skills and the jobs on the same page, for example the about page, you should create a dedicated controller to display this page. And pass the skills and the jobs to the view.

class AboutController extends Controller
{
	public function get()
	{
		$skills = Skill::all();
		$jobs = Job::all();

		return view('pages.about', compact('skills', 'jobs'));
	}
}

Then the route would be like this.

Route::get('about', [AboutController::class, 'get']);

Furthermore you are using components like simple fragments of code. If you want to use components, you should also call the components like mentioned in the documentation.

<x-skills></x-skills>
<x-jobs></x-jobs>

https://laravel.com/docs/10.x/blade#rendering-components

You can also use blade fragments if it's that's you really need.

https://laravel.com/docs/10.x/blade#rendering-blade-fragments

But components and fragments are different approaches of using blade to add a portion of HTML code according to what you really need.

Seeing your code, I suggest you to follow some tutorial, for example Laravel from scratch.

https://laracasts.com/series/laravel-8-from-scratch

1 like
graeme14's avatar

@vincent15000 Hi again, thanks for getting in touch. I have a day job outside my coding so updates are coming in slow.

I applied your recommended changes last night, and Laravel is throwing: InvalidArgumentException

' Unable to locate a class or view for component [jobs] '

I tried testing it with one component, and comes up with ' Undefined variable $skills '

1 like
uchimaru8's avatar

Good idea to have reusable components, but stop when you are adding code to handle different use-cases in a single component. If you have a component that has two discreet states, it may be better to make one abstract partial and two page-specific implementations that live in a file that is organized in the folder for the page https://mobdro.bio/ .

1 like
vincent15000's avatar

@uchimaru8 It depends on where you need to display these partials. If he needs to display them in different pages, it's not a good idea to do so.

graeme14's avatar

@vincent15000 I've had a read through the laravel page you recommended and applied some changes. I also have a View/Components folder in the App folder which I didn't have before.

The updated code is as follows: resources/views/pages/about.blade.php

			<section class="section">
				<div class="container">
	
					<x-skills></x-skills>

				</div>
			</section>

resources/views/components/skills.blade.php

			@foreach ($skills as $skill)
			<x-skills.about :skill="$skill" />
		
			<div class="progress-wrapper" x-data="{ value: ' ' }">
			
			<p> {{ $skill->name }} <span class="icon"><i class=" {{ $skill->icon }} "></i></span></p>
				
			</div>

		@endforeach

App/View/Components/Skills.php

class Skills extends Component { public $skill;

public function __construct($skill)
{
    $this->skill = $skill;
}

public function render(): View|Closure|string
{
    return view('components.skills.about');
}

I'm now getting " Unresolvable dependency resolving [Parameter #0 [ $skill ]] in class App\View\Components\Skills "

1 like
tangtang's avatar

@graeme14 maybe you tried it, but you code in wrong place.

maybe this help, but read and follow it step by step. I assume you already do a route and controller like @vincent15000 suggest, so it just little more modification in blade and component it will work as you want (maybe 🤣).

first look at this about blade

<section class="section">
	<div class="container">
	// <x-skills></x-skills> // this is your code before and you forgot to pass data to component skills

    <x-skills :skills="$skills"></skills> // this is the updated code

	</div>
</section>

and this resources view component skills blade

@foreach ($skills as $skill)
			// <x-skills.about :skill="$skill" /> // delete this code line, why you call the component inside it self component 😁
		
	<div class="progress-wrapper" x-data="{ value: ' ' }">
			
			<p> {{ $skill->name }} <span class="icon"><i class=" {{ $skill->icon }} "></i></span></p>
				
	</div>
@endforeach

and this app views compenent skills.php

class Skills extends Component {
public $skill;

public function __construct($skill)
{
    $this->skill = $skill;
}

//public function render(): View|Closure|string
public funtion render(): //use this insted
{
    // return view('components.skills.about'); // where this skills.about come from
    // just use this
    return view('components.skills');
}

it will same todo step for job component later, but make sure this skills runs with no error first.

hope this work. just follow step by step just as @vincent15000 , @snapey resposnse.

graeme14's avatar

To keep things specific, I'm creating a job profile page, which necessitates the need to display skills alongside the job description on the same page. Still getting familiar with the component side of laravel.

1 like
Snapey's avatar

@graeme14 Just remember, one route, one controller, one view

If you need to display two different sets of data in one view, then prepare both sets in the controller method and pass them to the view. You can then display them directly in the view or via blade components - this but is irrelevant to your problem.

If you like the idea of self-contained components that are responsible for getting their own data, then Livewire would be the way to go, but you need to start with the basics first.

1 like
graeme14's avatar

Hi again everybody. I've got back from my day job and had some time to develop a bit more understanding of using components. Still haven't quite got the "skills" and "jobs" to display but I think I'm making progress. I'll keep you updated as I go

1 like
Snapey's avatar

@graeme14 Sounds like you still have not got your head around it.

If you have a view that wants to display both skills and jobs then in ONE CONTROLLER method, you need to get both sets of data and pass them to the view.

If this statement still does not make sense, park your project and go and study some starter videos until you have the concept of MVC

1 like
tangtang's avatar

@vincent15000 relax, some of them jus a bit confused using this framework for the first time.

even with full explanation they will not just understand it completly. they will force it with their mind what they need like it is what it is 🤣 without undestanding step by step how to do it. they will rush with copy paste code 😁.

just like what @snapey says, @graeme14 must learn it from basic again before make a complex application.

and @graeme14 have a progress like what it says here but I think I'm making progress. I'll keep you updated as I go

so keep it in spirit @graeme14 , just a little step more, it will be done before you know 💪

1 like

Please or to participate in this conversation.