php 调用空 Laravel 5.2 上的成员函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37563681/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:17:45  来源:igfitidea点击:

call to a member function on null Laravel 5.2

phplaravellaravel-5.2

提问by Naveen Kumar

I am creating something more like a facebook post , i get a post from user and store it to database for now. I have created two models User and Post

我正在创建更像 Facebook 帖子的内容,我从用户那里收到一个帖子并将其存储到数据库中。我创建了两个模型User 和 Post

here is the code of User model

这是用户模型的代码

    <?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;

class User extends Eloquent implements Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    public function posts()
    {
        return $this->hasMany('App\Post');
    }
}

Here is the code of Post model

这是 Post 模型的代码

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function user()

    {

        return $this->belongsTo('App\User');
    }
}

and I have a postController and here is the code of it

我有一个 postController,这是它的代码

    use App\Post;

use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use Illuminate\Contracts\Auth\User;
use App\Http\Controllers\Controller;

class PostController extends Controller
{

    public function createPost(Request $request)
    {

        $post = new Post();

        $post->userpost = $request['userpost'];
        $request->user()->posts()->save($post);  -> **Error here Call to a member function posts() on null**
        return redirect()->back();

    }}

回答by MrCode

The error is indicating that $request->user()returned null, which means that the user is not logged in to the application – i.e is a guest. The method $request->user()will only return a user, if a user is authenticated.

错误表明$request->user()返回null,这意味着用户未登录到应用程序 - 即是访客。$request->user()如果用户经过身份验证,该方法将仅返回用户。

From the documentation:

文档

once a user is authenticated, you may access the authenticated user via an Illuminate\Http\Request instance

用户通过身份验证后,您可以通过 Illuminate\Http\Request 实例访问经过身份验证的用户

The solution is to wrap your code in an auth check like below, or use middleware to prevent access to this route unless the user is authenticated.

解决方案是将您的代码包装在如下所示的身份验证检查中,或者使用中间件来阻止访问此路由,除非用户通过身份验证。

if(\Auth::check()){
    $post = new Post();

    $post->userpost = $request['userpost'];
    $request->user()->posts()->save($post);
} else {
    // not logged in - do something
}

回答by Prashant G Patil

use App\Post;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use Illuminate\Contracts\Auth\User;
use App\Http\Controllers\Controller;

class PostController extends Controller
{

  public function createPost(Request $request)
    {

    $post = new Post();
    $post->userpost = $request['userpost'];
    $post->user_id = LOGGED_IN_USER_ID; 
    //$request->user()->posts()->save($post);
    $post->save();
    return redirect()->back();
    }
}