laravel - Laravel 模型没有从表中获得任何价值
问题描述
为什么我的模型在我尝试获取数据之前它工作正常之前没有给我任何数据$standart->name
,但现在它给了我null
。我不知道发生了什么。
试图死转储,这就是我得到的:
public function show(Standart $standart)
{
dd($standart);
return view('admin.datas.standart', compact('standart'));
}
App\Models\Standart {#1249 ▼
#guarded: []
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
标准型号:
class Standart extends Model
{
use HasFactory;
protected $guarded = [];
public function questions()
{
return $this->hasMany(Question::class);
}
}
路线
Route::get('/standarts/{standarts}', [App\Http\Controllers\StandartController::class, 'show']);
解决方案
您必须使用资源路由,例如在 web.php 文件中
使用 App\Http\Controllers\StandartController;
Route::resource('standarts', StandartController::class);
在这之后你也得到表名 null 然后你必须在模型中提到表名,比如,
受保护的 $table = 'your_table_name_here';
推荐阅读
- c# - Xamarin/XAML/C# - 构建一个计算 DateTime 数组中所有周末天数的方法
- matrix - #Fail2ban,矩阵(突触)的失败正则表达式请求
- flutter - Flutter 没有为“Object”类型定义运算符“[]”
- android - 源图像的文件名无效 - 无法更改而不丢失消息
- lighthouse - 需要帮助了解多个构建后生成的 Lighthouse 服务器仪表板
- android - 底部导航视图中的片段路由
- mysql - mySql 更新查询返回 ERROR 1242: 1242: 子查询返回多于 1 行
- r - R Shiny:从上传的数据运行动态 For 循环
- postgresql - jsonb 交叉连接未在 Postgres 中返回 N*M 行
- ios - 如何在 XCode 13 中运行 react native 0.63 应用程序?