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

414
Views
Calling function from (Laravel 8) Blade template file

I have a blade component called OrderBox.php in app/View/Components. It has several public attributes I can access from the orderbox.blade.php template file.

            <x-orderbox
                    :id="$id"
                    ....
            ></x-orderbox>

However, there is a public function getShortTextByStatus($statusCode) in the OrderBox.php component file and when I call this function inside orderbox.blade.php template

{{ $getShortTextByStatus('EDIT') }}

I get the Undefined variable: shortTextByStatus error

I followed this: https://laravel.com/docs/8.x/blade#component-methods but everything looks the same, what am I missing?

EDIT:

If I remove the $ I get the Call to undefined function getShortTextByStatus() error

{{ getShortTextByStatus('EDIT') }}

OrderBox.php

class OrderBox extends Component
{

    public $id;
    ....

    public function __construct()
    {
    //
    }

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

    public function getShortTextByStatus($statusCode)
    {
       return 'TEST';
    }

}

EDIT 2:

I noticed that x-orderbox actually should be x-order-box

<x-order-box
    :id="$id"
     ....
></x-order-box>

With this, the {{ $getShortTextByStatus("EDIT") }} works !

B U T

does not pass attributes like :id="$id" or

something="static" 

So the public $id; or public $something will be empty....

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

So I made a couple of mistakes here

First of all, if the component name is OrderBox than the

tag should be

<x-order-box></x-order-box>

And then, if you want to pass data to the component (OrdeBox) you need to add the attributes to the constructor - special thanks to @gert-b (Gert B) from the comment section

class OrderBox extends Component
{

    public $id;
    public $otherId;
    ....

    public function __construct($id, $otherId)
    {
        $this->id = $id;
        $this->otherId;
    }

    ...
    ...
}

For some reason, I thought only mandatory attributes go to the constructor.

And don't forget, the attributes in the component class are camelCase like public $otherId; but in the template, it is kebab case: other-id

<x-order-box
    id="1"
    :other-id="$someVariable"
></x-order-box>
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!