php - 找不到方法商店和帖子 [LARAVEL]
问题描述
我在后端问题上被困了一段时间。PHPStorm 告诉我posts 和store 方法不存在,不知道用什么方法来解决这个问题...
文件 PostController.php:
public function store(){
$data = request()->validate([
'caption' => ['required', 'string'],
'image' => ['required', 'image']
]);
$imagePath = request('image')->store('uploads','public');
auth()->user()->posts()->create([
'caption' => $data['caption'],
'image' => $imagePath
]);
return redirect()->route('profiles.show', ['user' => auth()->user()]);
}
文件用户.php:
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'username', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function getRouteKeyName()
{
return 'username';
}
public function profile(){
return $this->hasOne('App\Profile');
}
}
遇到的错误:
解决方案
我没有在我的 User.php 中创建关系帖子。
所以我以这种方式添加了关系:
public function posts(){
return $this->hasMany('App\Post');
}
推荐阅读
- swift - 使用 xcode 和 swift 向您的应用程序添加年龄限制的最佳方法是什么
- algorithm - 朴素贝叶斯分类器:数据集样本数问题
- php - 如何从价格背面删除 ,00 或 .00?
- javascript - 每次单击按钮时如何创建带有圆圈的画布
- python - Numpy相等的dtype
- asp.net - 附加类型为“X”的实体失败,因为同一类型的另一个实体已经具有相同的主键值。错误
- vue.js - 让 Chartkick 在渲染图表之前等待从 Firebase 填充数据
- tensorflow - 实现 tensorflow 高级 api 时出错
- powerbi - power bi 瀑布图首先显示较旧的日期
- r - 从具有 H、M、S 的字符串中拆分时间值 - R 编程