Php 调用未定义的方法Illumb\Database\Eloquent\Relations\HasMany::withTimestamps()

Php 调用未定义的方法Illumb\Database\Eloquent\Relations\HasMany::withTimestamps(),php,laravel,eloquent,Php,Laravel,Eloquent,我正试图在laravel中创建一个带有时间戳的关系。我有这个应用程序,允许客户向市场申请工作。当市场上的自由职业者找到他们感兴趣的工作时,他们建议完成该工作,并在自由职业者表中查询新行 以下是创建关系的代码: $marketplace->freelancers()->create([ 'request_id' => $id, 'user_id' => $user->id ]); 以下是市场模型关系代码: /** * A request

我正试图在laravel中创建一个带有时间戳的关系。我有这个应用程序,允许客户向市场申请工作。当市场上的自由职业者找到他们感兴趣的工作时,他们建议完成该工作,并在自由职业者表中查询新行

以下是创建关系的代码:

$marketplace->freelancers()->create([
   'request_id' => $id,
   'user_id' => $user->id
]);
以下是市场模型关系代码:

    /**
     * A request may have multiple freelancers
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function freelancers()
    {
        return $this->hasMany('App\Models\MainFreelancers')->withTimestamps();
    }
/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function mainMarketplace()
{
    return $this->belongsTo('App\Models\MainMarketplace');
}
以下是自由职业者模型关系代码:

    /**
     * A request may have multiple freelancers
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function freelancers()
    {
        return $this->hasMany('App\Models\MainFreelancers')->withTimestamps();
    }
/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function mainMarketplace()
{
    return $this->belongsTo('App\Models\MainMarketplace');
}
在尝试运行第一个代码块时,我不断遇到以下Laravel错误:
BadMethodCallException
调用undefined方法Illumb\Database\Eloquent\Relations\HasMany::withTimestamps()

短期内,我只是手动添加了一个
strotime()
,但我更愿意利用Laravel提供的功能。有人有什么建议吗

注:我在此引用了之前的stackoverflow问题: 但不幸的是,这没有帮助

带时间戳()
用于多对多关系中,您希望在其中声明联接表中有created_at和updated_at列

$withTimestamps表示透视表上是否有可用的时间戳

正如例外情况正确指出的那样,
illighte\Database\Eloquent\Relations\hassmany
没有
withTimestamps()方法

带时间戳()
用于多对多关系中,您希望在其中声明联接表中有created_at和updated_at列

$withTimestamps表示透视表上是否有可用的时间戳

正如例外情况正确指出的那样,
illighte\Database\Eloquent\Relations\hassmany
没有
withTimestamps()方法

如果希望透视表自动维护 在时间戳处创建和更新,请在上使用withTimestamps方法 关系定义:

return$this->belongtomany('App\Role')->withTimestamps()

如果属于多个关系,
withTimestamps()
会自动为您管理时间戳。但是您正在实现的具有许多关系,因此您可以直接访问时间戳

在这种情况下,您可以在模型中使用访问器来格式化日期:

public function getCreatedAtAttribute()
    {
        return date("l jS \of F Y h:i:s A", strtotime($this->attributes['created_at']));
    } 
如果希望透视表自动维护 在时间戳处创建和更新,请在上使用withTimestamps方法 关系定义:

return$this->belongtomany('App\Role')->withTimestamps()

如果属于多个关系,
withTimestamps()
会自动为您管理时间戳。但是您正在实现的具有许多关系,因此您可以直接访问时间戳

在这种情况下,您可以在模型中使用访问器来格式化日期:

public function getCreatedAtAttribute()
    {
        return date("l jS \of F Y h:i:s A", strtotime($this->attributes['created_at']));
    } 

啊,那太不幸了。所以我应该继续使用
strotime()
手动定义创建的\u at&updated\u at列吗?@SolomonAntoine你能发布你的迁移文件和模型文件吗。啊,这太不幸了。因此,我应该继续使用
strotime()
手动定义创建的_at&updated_at列吗?@SolomonAntoine您可以发布迁移文件和模型文件。如果使用
one-to-many
关系,则不需要附加timestamp()方法。您不需要附加timestamp()方法,如果您使用的是一对多关系。