Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::withTimestamps()

Sol*_*ine 1 php laravel eloquent

I am trying to create a relationship in laravel with timestamps. I have this app that allows customers to request a job to a marketplace. When a freelancer on the marketplace finds a job their interested in they propose to complete the job and a new row is queried into the freelancer table.

Here is the code creating the relation:

$marketplace->freelancers()->create([
   'request_id' => $id,
   'user_id' => $user->id
]);
Run Code Online (Sandbox Code Playgroud)

Here is the Marketplace Model relationship code:

    /**
     * A request may have multiple freelancers
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function freelancers()
    {
        return $this->hasMany('App\Models\MainFreelancers')->withTimestamps();
    }
Run Code Online (Sandbox Code Playgroud)

Here is the Freelancer Model relationship code:

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function mainMarketplace()
{
    return $this->belongsTo('App\Models\MainMarketplace');
}
Run Code Online (Sandbox Code Playgroud)

When trying to run the very first code block I keep getting this Laravel Error: BadMethodCallException Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::withTimestamps()

In the short term, I just manually added a strtotime() but I'd prefer to utilize what Laravel offers. Does anyone have suggestions?

Note I have reference a previous stackoverflow question here: Timestamps are not updating while attaching data in pivot table But unfortunately it didn't help.

Ang*_*bey 5

withTimestamps(); 用于多对多关系中,您要在其中声明连接表中有 created_at 和 updated_at 列。

$withTimestamps 指示数据透视表上是否有时间戳可用。

正如异常正确指出的那样,Illuminate\Database\Eloquent\Relations\HasMany没有withTimestamps()方法。

https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Relations/HasMany.html