2016-06-07 5 views
0

Ich benutze PHP, Laravel 5 und MySQL. Ich versuche, einen Patienten basierend auf dem authentifizierten Benutzer abzurufen, bekomme aber einen falschen Wert des Primärschlüssels.Falscher Abruf des Primärschlüssels im Laravel-Modell

Im PatientController Ich laufe dies:

$ Patient = Patient :: wo ('userId', '=', $ request-> Benutzer() -> id) -> get() -> zuerst();

Der Wert der patientId in der Klinge unterscheidet sich von der gespeicherten.

zum Beispiel, habe ich 3127

während der ursprüngliche Wert 3127fc14-368e-4b06-90f2-a524986e627e

ich mit den übrigen Spalten das gleiche Problem nicht konfrontiert ist.

Vielen Dank für Ihre Zeit und Mühe.

User.php

class User extends Authenticatable 
{ 
    protected $fillable = [ 
     'id','name', 'email', 'password', 
    ]; 

    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 

    public function patient() 
    { 
     return $this->belongsTo('App\Patient', 'patientId','id'); 
    } 
} 

UserController.php

class UserController extends Controller 
{ 
    /** 
    * @param Request $request 
    * @return \Illuminate\Http\RedirectResponse 
    */ 

    public function postSignUp(Request $request) 
    { 
     $this->validate($request,[ 
      'email' => 'required|email|unique:users', 
      'name' => 'required|max:100', 
      'password' => 'required|min:6' 
     ]); 

     $user = new User(); 
     $user->email = $request['email']; 
     $user->name = $request['name']; 
     $user->password = bcrypt($request['password']); 

     Auth::login($user); 

     return redirect()->route('dashboard'); 
    } 

    /** 
    * @param Request $request 
    * @return \Illuminate\Http\RedirectResponse 
    */ 

    public function postSignIn(Request $request) 
    { 
     $this->validate($request,[ 
      'email' => 'required', 
      'password' => 'required' 
     ]); 
     if (Auth::attempt([ 'email' => $request['email'], 'password' => $request['password']])){ 

      return redirect()->route('dashboard'); 
     } 
     return redirect()->back(); 
    } 
} 

Patient.php

class Patient extends Model 
{ 
    protected $primaryKey='patientId'; 
    public $incrementing = 'false'; 
    public $timestamps = true; 

    protected $fillable = [ 
     'patientId','contactId','guardianId','userId','patientNationalId','patientFirstName','patientSurName','patientDob','patientInsuranceNumber','patientGender' 
    ]; 

    public function user() 
    { 
     return $this->hasOne('App\User','id','userId'); 
    } 
} 

PatientController.php

class PatientController extends Controller 
{ 
    public function getDashboard(Request $request) 
    { 
     $patient = Patient::where('userId', '=', $request->user()->id)->get()->first(); 
     return view('dashboard', [ 
      'patient' => $patient 
     ]); 
    } 
} 

Migrations

Schema::create('users', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->string('name'); 
    $table->string('email')->unique(); 
    $table->string('password', 60); 
    $table->rememberToken(); 
    $table->timestamps(); 
}); 

Schema::create('patients', function (Blueprint $table) { 
    $table->string('patientId')->primary(); 
    $table->string('contactId')->index()->nullable(); 
    $table->string('guardianId')->index()->nullable(); 
    $table->integer('userId')->unsigned()->nullable(); 
    $table->string('patientNationalId')->nullable(); 
    $table->string('patientFirstName')->nullable(); 
    $table->string('patientSurName')->nullable(); 
    $table->string('patientDob')->nullable(); 
    $table->string('patientGender')->nullable(); 
    $table->string('patientInsuranceNumber')->nullable(); 
    $table->timestamps(); 
    # Foreign Keys 
    $table->foreign('userId')->references('id')->on('users'); 
    $table->foreign('guardianId')->references('guardianId')->on('guardians'); 
    $table->foreign('contactId')->references('contactId')->on('contacts'); 
}); 

Antwort

1

Sie haben userId als Fremdschlüssel, die ID in Benutzer verweist, die eine ganze Zahl ist. Wenn Sie sagen, der ursprüngliche Wert ist 3127fc14-368e-4b06-90f2-a524986e627e, wie erstellen Sie das id?

Ihr Schema beschreibt den Typ für die Spalte eine ganze Zahl so 3127fc14-368e-4b06-90f2-a524986e627e nur zu sein in einem integer Wert von 3127 führen

+0

Ich drucke Patienten_ID, die Typ-String ist. Was Sie sagen, ist sinnvoll, aber ich kann den Fehler nicht finden ... wenn ich {{$ patient}} in das Blatt eintippe, bekomme ich folgendes json: '{ " patientId ": 3127, " contactId ": "0da44870-2b6a-11e6-9717-d70507957d0d" "guardianId":" be3d1ad0-2b67-11e6-9f83-5d955f25c5f6" "userId": 2, "patientNationalId": "968.359.452.532" ,. ..} ' – dimitrisdan