Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1K
Views
Passing Php variable to Laravel Blade Component

I have made a laravel component using php artisan make:component testcomponent for which two files are created; one is blade and second one is php class file. here is the blade file:

<div>
    {{ $data }}
</div>

and here is the php file

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class testcomponent extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.testcomponent');
    }
}

And i called this component in a blade file using this way <x-testcomponent/>

But Now, how can i pass a variable coming form controller to this component?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

First of all go to the php component file and do this.(Declare a variable and assign a value from variable coming from constructor)

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class testcomponent extends Component
{
    public $data;
    public function __construct($data)
    {
        //
        $this->data=$data;
    }

  
    public function render()
    {
        return view('components.testcomponent');
    }
}

and then in the view file where you call blade component; you can call like this

<x-testcomponent :data=$data/>

where $data is the variable coming from controller

Thats solved!!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!