提问人:SmpLife 提问时间:11/11/2023 更新时间:11/11/2023 访问量:60
Laravel 中的空属性
Empty attributes in Laravel
问:
我正在尝试编辑我的数据库输出,但当我使用时,我得到空属性。dd($customerlist);
web.php (路由)
Route::get('/customer/{customer}/edit', 'App\Http\Controllers\CustomerListController@edit')->name('customer.edit');
CustomerListController.php (控制器)
public function edit(Customerlist $customerlist)
{
dd($customerlist);
}
customerlist.blade.php
<tbody class="divide-y divide-gray-200">
@foreach ($customers as $customer)
<tr>
<td class="px-6 py-4 whitespace-nowrap">{{ $customer->Naam }}</td>
<td class="px-6 py-4 whitespace-nowrap">{{ $customer['Bank_Account_Number'] }}</td>
<td class="px-6 py-4 whitespace-nowrap">{{ $customer['Social_Security_Number'] }}</td>
<td class="px-6 py-4 whitespace-nowrap">
<a href="{{ route('customer.edit', ['customer' => $customer]) }}" class="text-blue-500 hover:underline">Edit</a>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<form action="{{ route('customer.destroy', $customer['id']) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit" class="text-red-500 hover:text-red-700">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
CustomerList.php(模型)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CustomerList extends Model
{
use HasFactory;
protected $table = 'customerlists';
protected $fillable = ['Naam', 'Bank_Account_Number', 'Social_Security_Number'];
}
我的 web.php、CustomerListController 和 Blade 文件中还有更多代码,我只是展示了我眼中必要的内容,我的控制器中还有其他 100% 有效的 crud 操作。它们是“创建”和“删除”。我的数据库也在我的网站上输出。
我希望有人可以帮助我,如果您需要更多代码来帮助,请告诉我!
答:
3赞
Pippo
11/11/2023
#1
操作中的变量必须与路由中设置的变量匹配:
public function edit(Customerlist $customer)
该机制称为“隐式绑定”,此处为完整文档。
简而言之,路由变量名称和(类型提示的)操作变量名称必须匹配,以允许使用调用路由提供的密钥从数据库中检索模型,否则系统将只创建新模型的一个实例而不填充它
评论
0赞
Vyrn
11/11/2023
你是救命恩人,你能再解释一下到底应该匹配什么吗?这是我在Laravel上的第一个严肃的一周,所以我想尽可能多地了解。
1赞
Pippo
11/11/2023
我在回答中添加了一些解释
2赞
Pippo
11/11/2023
@SmpLife,如果需要,可以关闭问题,接受答案
评论