$data is a reserved keyword/variable used internally for components. You would need to rename it to something else or pass in each array element as a separate property.
Laravel component is not receiving variables!
I'm facing an issue where my Laravel component is not receiving the $data variable properly.
Defined values (e.g., $title, $description) are passed correctly. Expressions or variables (e.g., $data) are not being passed. The $data variable is retrieved from the controller as an object, for example: $data = (object) [ 'code' => 10, 'name' => 'actor', ]; However, when I pass $data to my component, it does not seem to be available inside the Blade template.
Version: Laravel 11, PHP 8.2.x
My Code edit.blade.php
<div class="app-content">
<div class="container-fluid">
<x-form-input
route="{{ route('admins.roles.edit', ['id' => $data->id]) }}"
title="Chỉnh sửa"
description="Chỉnh sửa một role"
:data="$data"
/>
</div>
</div>
form-input.blade.php
<form class="form" novalidate method="POST" action="{{ $route }}">
@csrf
<div class="card-body">
<div class="row g-3">
<div class="col-md-6">
<label for="code" class="form-label">Mã role</label>
<input
name="code"
value="{{ old('code', $data->code ?? '') }}"
type="text"
class="form-control"
required
/>
</div>
<div class="col-md-6">
<label for="name" class="form-label">Tên role</label>
<input
type="text"
name="name"
value="{{ old('name', $data->name ?? '') }}"
class="form-control"
required
/>
</div>
<div class="col-md-12">
<label for="description" class="form-label">Mô tả</label>
<textarea
name="description"
class="form-control"
>{{ old('description', $data->description ?? '') }}</textarea>
</div>
</div>
</div>
<div class="card-footer text-center">
<button class="btn btn-primary" type="submit">{{ $title }}</button>
<a class="btn btn-secondary" href="{{ route('admins.roles.index') }}">Quay lại</a>
</div>
</form>
FormInput.php:
namespace Modules\Admin\View\Components;
use Illuminate\View\Component; use Illuminate\View\View;
class FormInput extends Component { public string $title; public string $description; public string $route; public $data;
public function __construct(string $title, string $description, string $route, $data = null)
{
$this->title = $title;
$this->description = $description;
$this->route = $route;
$this->data = $data;
}
public function render(): View|string
{
return view('admin::components.form-input');
}
}
Help me, Thank everyone❤️
Please or to participate in this conversation.