试图获得非对象的属性。 laravel错误

时间:2016-05-21 06:53:41

标签: laravel laravel-5.2

我是laravel的新人,需要你的帮助。

我有这个IR模型:

class IR extends Model
{
    //
    protected $fillable=['irnum','date','subject','cause','facts','as','at','rec','user_id'];
    protected $casts=['user_id'=>'int'];
    public function user()
    {
        return $this->belongsTo(user::class);
    }
    public static $rules =array (
        'date'=>'required',
        'status'=>'required|min:10',
        'cause' => 'required|min:10',
        'facts' => 'required|min:10',
        'ir-as' => 'required|min:10',
        'rec' => 'required|min:10',
        'ir-at' => 'required|min:10',
    );
}

和路线:

Route::group(['middleware' => ['web']], function () {
 Route::get('/', function () {
        return view('welcome');
 })->middleware('guest');
 Route::resource('tasks','TaskController');
 Route::get('ir',function ()
  {
        return View::make('tasks/ir');
  });
    Route::resource('irs','IRController');
 Route::auth();
});

这是我的控制者:

class IRController extends Controller
{
/**
 * The task repository instance.
 *
 * @var TaskRepository
 */
 protected $irs;
/**
 * Create a new controller instance.
 *
 * @param  TaskRepository  $tasks
 * @return void
 */
 public function __construct(IRRepository $irs)
 {
     $this->middleware('auth');
        $this->irs = $irs;
 }
/**
 * Display a list of all of the user's task.
 *
 * @param  Request  $request
 * @return Response
 */

    public function index(Request $request)
 {
     return view('tasks.ir',[
          'irs' => $this->irs->forUser($request->user()),
      ]);
 }
/**
 * Create a new task.
 *
 * @param  Request  $request
 * @return Response
 */

 public function create()
  {
     return View::make('irs.create');
 }  
 public function store(Request $request)
    {

        $request->user_id=Auth::user()->id;
     $input =$request->all();
     $validation=Validator::make($input, IR::$rules);
     if($validation->passes())
      {

          IR::create($input);
          return Redirect::route('tasks.ir');
     }
        return Redirect::route('tasks.ir')
           ->withInput()
            ->withErrors($validation)
         ->with('message','There were validation errors.');

 }
/**
 * Destroy the given task.
 *
 * @param  Request  $request
 * @param  Task  $task
 * @return Response
 */
  public function destroy(Request $request, IR $irs)
  {

  }
}

我真的不知道是什么原因引发了这个错误。 添加事件报告时出现错误。 请帮忙。 laravel的新作

1 个答案:

答案 0 :(得分:0)

当您尝试添加事件报告或IR时,您说您收到错误,因此我认为问题出在store()行动中。

我只能在store()操作中看到此错误的一个潜在候选人:

Auth::user()->id;

在此子句之前添加dd(Auth::user());,如果它将输出null,请使用check()方法检查是否有用户通过身份验证:

if (Auth::check()) {
    Auth::user->id;
}