验证不在laravel中工作

时间:2018-03-31 07:47:02

标签: laravel laravel-5 laravel-4 laravel-5.3

我想将治疗方法存储到数据库中,并附加了用户ID。当我运行此代码时,它甚至在用户登录后也会触发else条件。我还注意到我可以从GET请求返回用户ID但是对于POST请求,它不会。

我的代码



<Private>False</Private>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您的用户是否已登录? Auth :: user()返回NULL而不登录。试试这个

<?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\Treatment;
    use Auth;

    class TreatmentsController extends Controller
    {
        //Post request 
        //Add Treatment
        public function add(Request $request){
            if (Auth::check())
            {
              $user = Auth::user();
              $treatment = new Treatment;
              $treatment->name = $request->treatmentName;
              $treatment->user_id = $user->id;
              $treatment->company_id = $user->company->id;
              $treatment->save();
            }else{
              //functionality for not logged users
            }
          }

    }