Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带附加场的laravel质量分配_Php_Laravel_Eloquent - Fatal编程技术网

Php 带附加场的laravel质量分配

Php 带附加场的laravel质量分配,php,laravel,eloquent,Php,Laravel,Eloquent,这是我在Post Controller中的存储方法 通过masassignment,有两个查询。是否有其他方法可以只在一个查询中执行操作?使用反向关系 $post = Post::create(['author_id' => auth()->id] + $request->all()); Auth::user->posts->create$request->all 在用户类上,需要对关系进行反向定义 public function posts() { return $

这是我在Post Controller中的存储方法


通过masassignment,有两个查询。是否有其他方法可以只在一个查询中执行操作?

使用反向关系

$post = Post::create(['author_id' => auth()->id] + $request->all());
Auth::user->posts->create$request->all

在用户类上,需要对关系进行反向定义

public function posts() {
    return $this->hasMany(Post::class);
}

所以我应该在filleble中输入作者id。是吗?通过这样做,用户可以在update requestyes中插入作者id,将此字段添加到。Javad Ali对,将$request->all+['author\u id'=>auth->id]更改为['author\u id'=>auth->id]+$request->all如果有两个额外字段怎么办?author\u id,blog\u id位于查询字符串中,您不能在fillable中添加它们。但是,您可能希望考虑不通过查询字符串传递,因为它非常不安全,或者检查用户是否具有访问权限/是否允许访问等。在控制器中,答案之一是否解决了您的问题?那就请吧。这会向其他用户显示问题已解决。
public function posts() {
    return $this->hasMany(Post::class);
}