samedi 31 mars 2018

Could not get user id from Laravel controller

When I want to get the logged in user id using Auth::user()->id from the controller then, it gives this error.

HTTP500: SERVER ERROR - The server encountered an unexpected condition that prevented it from fulfilling the request. (XHR)POST - http://leadmanager.dev/api/treatment/add

If I type the user id manually it works and gets saved to the database, also Auth::user()->id works for GET requests but not for post requests. All relations between the tables are completed. Has anyone faced this issue?

My code

<?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){
        $treatment = new Treatment;
        $treatment->name = $request->treatmentName;
        $treatment->user_id = Auth::user()->id;
        $treatment->company_id = Auth::user()->company->id;
        $treatment->save();
      }

}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire