[英]Laravel: Error trying to get unique users to create posts
Right so im trying to make a website where a registered user can create a post. 正确,因此我试图建立一个注册用户可以创建帖子的网站。 The issue i am having rigt now is getting the post to be added to the the database.
我现在遇到的问题是将帖子添加到数据库中。 It should work but im getting an error.
它应该可以工作,但是即时通讯出现错误。
The error: 错误:
Call to a member function posts() on null
The error points to my post controller class 错误指向我的后控制器类
<?php
use App\Post;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class postController extends Controller
{
public function postCreatePost(Request $request){
$post = new Post();
$post->body = $request['body'];
$request->user()->posts($post); //points here
return redirect()->route('dashboard');
}
}
This is my post migration up method: 这是我的后期迁移方法:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->text('body');
$table->integer('user_id');
});
}
Post model: 帖子模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user(){
return $this->belongsTo['App\User'];
}
}
User model: 用户模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts(){
return $this->hasMany('App\Post');
}
}
The section the user types in: 用户键入的部分:
<section class="row new-post">
<div class="col-md-6 col-md-offset-3">
<form action="{{ route('postcreate') }}" method="post">
<div class="form-group">
<textarea class="form-control" name="body" rows="5" placeholder="your post"></textarea>
</div>
<button type="submit" class="btn btn-primary">Create post</button>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
</section>
The problem is that on your controller you do: 问题是您在控制器上执行以下操作:
$request->user()->posts($post); //points here
You are considering that user()
will always return something. 您正在考虑
user()
将始终返回某些内容。
If your route isn't guarded by the auth
middleware, $request->user()
may return null
if there's no authenticated user. 如果您的路由不受
auth
中间件保护,则如果没有经过身份验证的用户,则$request->user()
可能返回null
。
So you have two options: Or you add the auth
middleware, or you put an if on your code: 因此,您有两个选择:或者添加
auth
中间件,或者在代码中添加if:
if ($request->user()) {
$request->user()->posts($post);
}
However, this will fix the error but it won't create a post 但是,这将修复错误,但不会创建帖子
public function postCreatePost(Request $request){
$post = new Post();
$post->body = $request['body'];
$request->user()->posts($post); // This call isn't valid.
return redirect()->route('dashboard');
}
The right way to do is: 正确的方法是:
public function postCreatePost(Request $request){
$request->user()->posts()->create([
'body' => $request->body
// Here I'm assumming that the only field is the `body`,
// but you may need other fields if they exists.
// the `user_id` field will be automatically filled.
];
return redirect()->route('dashboard');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.