简体   繁体   English

在null Laravel 5.2上调用成员函数

[英]call to a member function on null Laravel 5.2

I am creating something more like a facebook post , i get a post from user and store it to database for now. 我正在创建类似Facebook帖子的内容,我从用户那里获得了一篇帖子,现在将其存储到数据库中。 I have created two models User and Post 我创建了两个模型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();

    }}

The error is indicating that $request->user() returned null , which means that the user is not logged in to the application – ie is a guest. 该错误表明$request->user()返回null ,这意味着该用户尚未登录到该应用程序,即是访客。 The method $request->user() will only return a user, if a user is authenticated. 如果用户通过了身份验证,则方法$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. 解决方案是将您的代码包装在如下所示的auth检查中,或者使用中间件来阻止访问此路由,除非对用户进行身份验证。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 laravel 5.2 | 上传文件-在null上调用成员函数getClientOriginalName() - laravel 5.2 | upload file - Call to a member function getClientOriginalName() on null 致命错误:在null上调用成员函数query()(Laravel 5.2) - Fatal error: Call to a member function query() on null (Laravel 5.2) 在数组Laravel 5.2上调用成员函数getClientOriginalName() - Call to a member function getClientOriginalName() on array Laravel 5.2 route.php第113行中的FatalErrorException:在Laravel 5.2中,在null上调用成员函数delete() - FatalErrorException in routes.php line 113: Call to a member function delete() on null in Laravel 5.2 Laravel 5.2密码重置返回致命错误:“在null上调用成员函数getEmailForPasswordReset()” - Laravel 5.2 password reset returns the fatal error: “Call to a member function getEmailForPasswordReset() on null” Laravel在null上调用成员函数 - Laravel call to member function on null Laravel 5.2:dimsav在非对象上调用成员函数translateOrNew() - Laravel 5.2: dimsav Call to a member function translateOrNew() on a non-object 在非对象上调用成员函数where()吗? Laravel 5.2 - Call to a member function where() on a non-object ? Laravel 5.2 致电成员 function update() on null Laravel ZC1C425268E68385D1AB5074F17A - Call to a member function update() on null Laravel function 在 null laravel 7 上调用成员函数 store() - Call to a member function store() on null laravel 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM