调用未定义的方法 App\Models\Car::setCreatedAt() - Laravel - 5.8.16

Posted

技术标签:

【中文标题】调用未定义的方法 App\\Models\\Car::setCreatedAt() - Laravel - 5.8.16【英文标题】:Call to undefined method App\Models\Car::setCreatedAt() - Laravel - 5.8.16调用未定义的方法 App\Models\Car::setCreatedAt() - Laravel - 5.8.16 【发布时间】:2019-10-14 02:57:58 【问题描述】:

我正在使用最新的 Laravel。我有一个模型Car - id | name | * timestamp columns:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Car extends Model
    protected $fillable = [
        'name'
    ];

汽车迁移:

public function up()

    Schema::create('cars', function (Blueprint $table) 
        $table->bigIncrements('id');
        $table->integer('name')->nullable();
        $table->timestamps();
    );

我正在尝试在控制器的cars 表中保存新行:

    $car = new Car();
    $car->name = 'example';
    $car->save();

,但对于 $car->save(),我收到此错误:

调用未定义的方法 App\Models\Car::setCreatedAt()

即使,如果我创建新的迁移/模型create_table_test(具有相同列的表),我也会遇到同样的错误。

使用堆栈跟踪编辑:

/mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php 中的 65 个 BadMethodCallException:50

64 Illuminate\Database\Eloquent\Model:throwBadMethodCallException in /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:36

63 Illuminate\Database\Eloquent\Model:forwardCallTo in /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1614

62 Illuminate\Database\Eloquent\Model:__call in /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php:47

61 Illuminate\Database\Eloquent\Model:updateTimestamps in /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:791

60 Illuminate\Database\Eloquent\Model:performInsert in /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:663

59 Illuminate\Database\Eloquent\Model:保存在 /mnt/e/xampp/htdocs/room2/project/app/Http/Controllers/Admin/CarsController.php:33

58 App\Http\Controllers\Admin\CarsController:importCars/mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54

57 call_user_func_array 在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54

56 Illuminate\Routing\Controller:callAction 在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45

55 Illuminate\Routing\ControllerDispatcher:dispatch in /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/Route.php:219

54 Illuminate\Routing\Route:runController 在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/Route.php:176

53 Illuminate\Routing\Route:在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:680 中运行

52 Illuminate\Routing\Router:Illuminate\Routingclosure 在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30

51 Illuminate\Routing\Pipeline:Illuminate\Routingclosure 在 /mnt/e/xampp/htdocs/room2/project/vendor/barryvdh/laravel-stack-middleware/src/ClosureHttpKernel.php:30

50 Barryvdh\StackMiddleware\ClosureHttpKernel:/mnt/e/xampp/htdocs/room2/project/vendor/helthe/turbolinks/StackTurbolinks.php:51 中的句柄

49 Helthe\Component\Turbolinks\StackTurbolinks:handle in /mnt/e/xampp/htdocs/room2/project/vendor/barryvdh/laravel-stack-middleware/src/ClosureMiddleware.php:36

48 Barryvdh\StackMiddleware\ClosureMiddleware:处理在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163

47 Illuminate\Pipeline\Pipeline:Illuminate\Pipelineclosure 在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53

46 Illuminate\Routing\Pipeline:Illuminate\Routingclosure 在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41

45 Illuminate\Routing\Middleware\SubstituteBindings:/mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163 中的句柄

44 Illuminate\Pipeline\Pipeline:Illuminate\Pipelineclosure 在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53

43 Illuminate\Routing\Pipeline:Illuminate\Routingclosure 在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php:43

42 Illuminate\Auth\Middleware\Authenticate:handle in /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163

41 Illuminate\Pipeline\Pipeline:Illuminate\Pipelineclosure 在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53

40 Illuminate\Routing\Pipeline:Illuminate\Routingclosure 在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php 中: 75

39 Illuminate\Foundation\Http\Middleware\VerifyCsrfToken:handle in /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163

38 Illuminate\Pipeline\Pipeline:Illuminate\Pipelineclosure 在 /mnt/e/xampp/htdocs/room2/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53 ......

【问题讨论】:

请发布堆栈跟踪。这是整个Car 模型吗? 谢谢,我已经更新了问题。是的,这都是关于模型Car 你使用的是什么版本的 Laravel?堆栈跟踪与 laravel-5.2 标记不匹配。 Laravel: 5.8.16 看看vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php:有没有setCreatedAt()方法? 【参考方案1】:

我只是重新安装Laravel

    删除vendor文件夹; 删除composer.lockcomposer update 在控制台中。

感谢@Jonas Staudenmeir 的帮助。

【讨论】:

以上是关于调用未定义的方法 App\Models\Car::setCreatedAt() - Laravel - 5.8.16的主要内容,如果未能解决你的问题,请参考以下文章

调用未定义的方法 App\Models\Catering::search() [关闭]

调用未定义的方法 BelongsTo::attach()

未调用自定义 UIButton 的 setIsSelected 方法

调用未定义的方法 Cake\ORM\Entity::query() CakePhp

致命错误:未捕获的错误:调用未定义的方法 stdClass::option();

调用类方法时出现“调用未定义函数”错误