Php 将经过身份验证的用户对象传递给Laravel中的对象构造函数,表示未给出任何值
我有一个问题,我不能在拉雷维尔的头脑 我正在尝试使用构造函数中指定的用户对象创建对象的新实例。我正在将Auth::user()传递给它,但它似乎不起作用 以下是我的代码示例:Php 将经过身份验证的用户对象传递给Laravel中的对象构造函数,表示未给出任何值,php,oop,laravel,constructor,laravel-5,Php,Oop,Laravel,Constructor,Laravel 5,我有一个问题,我不能在拉雷维尔的头脑 我正在尝试使用构造函数中指定的用户对象创建对象的新实例。我正在将Auth::user()传递给它,但它似乎不起作用 以下是我的代码示例: public function requestStudentProject($id) { $student_request = new StudentRequest(Auth::user()); $student_request->requestProject($id); } 下面是
public function requestStudentProject($id)
{
$student_request = new StudentRequest(Auth::user());
$student_request->requestProject($id);
}
下面是我的课程片段:
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\User;
class StudentRequest extends Model{
protected $table = 'student_project_requests';
protected $user;
public function __construct(User $user)
{
$this->user=$user;
}
这给了我一个错误:
传递给App\StudentRequest::\uu construct()的参数1必须是App\User的实例,没有给定,在第986行的/home/vagrant/w3students/vendor/laravel/framework/src/light/Database/Eloquent/Model.php中调用并定义
有人能帮忙吗?几个小时以来,我一直在努力解决这个问题。谢谢 重写这样的模型可能很棘手,我更喜欢删除
public function\uu构造(..)
并将newstudentrequest(Auth::user())
改为newstudentrequest(Auth::user()->getAttributes())
-或StudentRequest::create(Auth::user()->getAttributes())
。因为这个模型实际上有自己的。假设StudentRequest上有相同的字段,并且用户表中有相同的字段。只要经过身份验证,Auth::user()就会返回用户对象。确保情况属实。是的,您可能没有经过身份验证。