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

rafidAhsan's avatar

How to pass data on Laravel Component to a controller.

I am doing some research on laravel controller and want to make a dynamic controller . Somehow I can not pass data. I donnno what I am doing wrong.

Controller

<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\Role;
use Illuminate\Http\Request;

class RoleController extends Controller
{
    private $name = 'roles';
    private $headers = array(
        'Name',
        'Created By'
    );

    public function index() {
        return view('backend.roles.index', [
            'name' => $this->name,
            'headers' => $this->headers,
            'data' => Role::paginate(10),
        ]);
    }
}

Blade file

<x-backend-layout>
    <x-table-layout :name="$name" :headers="$headers" :data="$data"/>
</x-backend-layout>

Component file

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class TableLayout extends Component
{
    protected $name, $headers, $data;
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct($name, $headers, $data)
    {
        $this->name = $name;
        $this->headers = $headers;
        $this->data = $data;
    }

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

component blade file

<div class="flex flex-col">
    <div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
      <div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
        <div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
          <table class="min-w-full divide-y divide-blue-200">
            <thead class="bg-gray-50">
              <tr>
                  {{dd($name)}}
                  {{dd($headers)}}
                  @foreach ($headers as $header)
                    <th scope="col" class="px-6 py-3 text-left text-xs font-black text-blue-400 uppercase tracking-wider">
                        {{ $header }}
                    </th>
                  @endforeach

                <th scope="col" class="relative px-6 py-3">
                    <span class="sr-only">Edit</span>
                </th>
              </tr>
            </thead>
            <tbody class="bg-white divide-y divide-gray-200">

            </tbody>
          </table>
        </div>
      </div>
    </div>
</div>

I am getting this error

Undefined variable: name (View: /home/rafid/project/laravel/starter/resources/views/components/table-layout.blade.php)
0 likes
3 replies
tomaslucena's avatar

Hey, have you ever sorted this issue?

I am trying to send data directly from the class to the components but it does not seems to work

1 like
mkrebseberth's avatar

Hey! Finally solved it! This did the trick:

  1. Component has to be made with UpperCamelCase, e.g. : php artisan make component Jetstream/UserTable
  2. In /app/View/Components/Jetstream/UserTable.php the variable has to be declared as public property: public $users ;
  3. Then in the constructor: public function __construct() { $this->users = User::paginate(10); }

Please or to participate in this conversation.