【发布时间】:2021-02-17 15:04:54
【问题描述】:
我创建了三个路线:
当我打电话给/cars/1 时,一切都正确并且连接了 mysql:
App\Models\Car {#1543 ▼
#fillable: array:12 [▶]
**#connection: "mysql"**
#table: "cars"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:15 [▶]
#original: array:15 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
当我调用/salaries/22 时,没有数据库连接:
App\Models\Salarie {#358 ▼
#fillable: array:8 [▶]
**#connection: null**
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
我的模型和控制器没有区别。我已经清除了路由缓存,但我不知道为什么会以不同的方式起作用。你有什么想法吗?
【问题讨论】:
-
您实际上可以在模型中定义连接。也许那件事发生了? > 受保护的 $connection;
-
您的薪水表中有 id 22 吗?我认为您的模型需要是薪水而不是薪水才能工作,而无需在模型类中定义表
-
您能向我们展示您的控制器中的 show 方法或您使用模型绑定的任何方法吗?
标签: laravel eloquent laravel-8