laravel 调用未定义的方法 Illuminate\Database\Eloquent\Relations\HasMany::withTimestamps()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53135219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::withTimestamps()
提问by Solomon Antoine
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.
我正在尝试在 laravel 中创建带有时间戳的关系。我有这个应用程序,允许客户向市场请求工作。当市场上的自由职业者找到他们感兴趣的工作时,他们建议完成该工作,并且在自由职业者表中查询新行。
Here is the code creating the relation:
这是创建关系的代码:
$marketplace->freelancers()->create([
'request_id' => $id,
'user_id' => $user->id
]);
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();
}
Here is the Freelancer Model relationship code:
这是自由职业者模型关系代码:
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function mainMarketplace()
{
return $this->belongsTo('App\Models\MainMarketplace');
}
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()
尝试运行第一个代码块时,我不断收到此 Laravel 错误:
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?
在短期内,我只是手动添加了一个,strtotime()
但我更愿意使用 Laravel 提供的功能。有没有人有建议?
Note I have reference a previous stackoverflow question here: Timestamps are not updating while attaching data in pivot tableBut unfortunately it didn't help.
注意我在这里参考了以前的计算器溢出问题: 在数据透视表中附加数据时时间戳没有更新但不幸的是它没有帮助。
回答by Angad Dubey
withTimestamps();
is used in Many to Many relationships where you want to declare that there are created_at and updated_at columns in the join table.
withTimestamps();
用于多对多关系中,您要在其中声明连接表中有 created_at 和 updated_at 列。
$withTimestamps Indicates if timestamps are available on the pivot table.
$withTimestamps 指示数据透视表上是否有时间戳可用。
As the exception rightly points out, Illuminate\Database\Eloquent\Relations\HasMany
does not have a withTimestamps()
method.
正如异常正确指出的那样,Illuminate\Database\Eloquent\Relations\HasMany
没有withTimestamps()
方法。
https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Relations/HasMany.html
https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Relations/HasMany.html
回答by Elisha Senoo
If you want your pivot table to have automatically maintained created_at and updated_at timestamps, use the withTimestamps method on the relationship definition:
return $this->belongsToMany('App\Role')->withTimestamps();
如果您希望数据透视表自动维护 created_at 和 updated_at 时间戳,请在关系定义上使用 withTimestamps 方法:
return $this->belongsToMany('App\Role')->withTimestamps();
In the case of a belongs to manyrelationship, withTimestamps()
manages your timestamps for you automatically. But you are implementing a has manyrelationship, so you have direct access to the timestamps.
在属于多关系的情况下,withTimestamps()
自动为您管理您的时间戳。但是您正在实现一个has many关系,因此您可以直接访问时间戳。
Int this case, you can use an accessor in your model to format the date:
在这种情况下,您可以在模型中使用访问器来格式化日期:
public function getCreatedAtAttribute()
{
return date("l jS \of F Y h:i:s A", strtotime($this->attributes['created_at']));
}