我有下面的控制器,它加载了一个包含组件的视图沿着从数据库中获取的数据,视图正确地显示了$data,我遇到的问题是我从视图传递到组件的数据没有显示给我错误“未定义的变量$data”。请任何人都可以给我一个解决方案。
TransactionController
class TransactionController extends Controller
{
public function index(){
$data = TransactionType::all();
return view('addtransaction', ['data' => $data]);
}
}
AddTransaction.blade.php(这是视图)
<!DOCTYPE html>
<html data-theme="light">
<head>
<title>Transactions</title>
<meta name="description" content="Our first page" />
<meta name="keywords" content="html tutorial template" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css" />
<link rel="stylesheet" href="{{ URL::asset('css/app.css') }}" />
</head>
<body>
<main class="container">
{{$data}}
<x-user-nav></x-user-nav>
<x-add-transaction :data="$data"></x-add-transaction>
</main>
</body>
</html>
add-transaction.blade.php(这是组件)
<div style="box-shadow: rgba(0, 0, 0, 0.05) 0px 6px 24px 0px, rgba(0, 0, 0, 0.08) 0px 0px 0px 1px; padding: 30px;">
<div class="grid">
<form action="/handleTransaction" method="POST">
@csrf
{{$data}}
<h3>Add Transacton</h3>
</div>
<div class="grid">
<div>
<label for="date">
Date
<input type="date" id="date" name="date" />
</label>
</div>
<div>
<label for="Type">Type</label>
<select name="type" required>
<option value="" selected>Select a type</option>
</select>
</div>
<div>
<label for="From">From</label>
<select name="from" required>
<option value="" selected>Select an Account</option>
<option>Income</option>
<option>Expense</option>
<option>Transfer</option>
</select>
</div>
<div>
<label for="To">To</label>
<select name="to" required>
<option value="" selected>Select an Account</option>
<option>Income</option>
<option>Expense</option>
<option>Transfer</option>
</select>
</div>
</div>
<div class="grid">
<div class="col-4">
<label for="Description">
Description
<input type="text" id="desc" name="desc" placeholder="Description" required />
</label>
</div>
<div>
<label for="Reference">
Reference
<input type="text" id="ref" name="ref" placeholder="Reference" />
</label>
</div>
<div>
<label for="Amount">
Amount
<input type="text" id="amount" name="amount" placeholder="Amount" />
</label>
</div>
<div>
<br />
<input type="submit" value="Add" />
</div>
</form>
</div>
</div>
我的AddTransaction组件类
<?php
namespace App\View\Components;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
class AddTransaction extends Component
{
/**
* Create a new component instance.
*/
public $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.add-transaction');
}
}
2条答案
按热度按时间643ylb081#
我发现的解决方案是“数据是一个保留关键字,这就是为什么这对你不起作用。”作者:driesvints(https://github.com/driesvints)
hts6caw32#
当使用
php artisan make:component AddTransaction
创建组件时,它将在app/View/Components
中创建刀片文件和组件类,如下所示:为了能够将数据传递到组件中,您必须将其添加到
__construct()
函数中:确保调整变量的类型。如果你不确定是哪种类型,你可以把它去掉。
希望这能帮上忙。:)
来源:https://laravel.com/docs/10.x/blade#passing-data-to-components