2017-02-28 58 views
0

介紹Laravel - 用戶外鍵導致試圖讓非對象

親愛的堆垛機的性能,

我目前工作的一個小項目,多數民衆贊成,而把我的時間一大筆。這是低效的生產,因此我想讓我的問題得到解決。所以有什麼問題?

我試圖訪問$小時變量中的屬性問題。此屬性是用戶模型的「名稱」字段(由Laravel的默認安裝提供給您的模型)。用戶和小時模型通過小時表內的「werknemer_id」鏈接在一起,它引用用戶表中的「id」字段。當我嘗試調用用戶時,它會返回「嘗試獲取非對象的屬性」錯誤。

數據庫關係 https://gyazo.com/5a39f76d7fe6e297551e74a46a8def7c

用戶模型

<?php 

namespace App; 

use Illuminate\Notifications\Notifiable; 
use Illuminate\Foundation\Auth\User as Authenticatable; 
use App\Hour; 

class User extends Authenticatable 
{ 
use Notifiable; 

/** 
* The attributes that are mass assignable. 
* 
* @var array 
*/ 
protected $fillable = [ 
    'name', 'email', 'password', 
]; 

public function Hours() { 
    return $this->hasMany(Hour::class); 
} 

/** 
* The attributes that should be hidden for arrays. 
* 
* @var array 
*/ 
protected $hidden = [ 
    'password', 'remember_token', 
]; 
} 

小時模型

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use App\User; 

class Hour extends Model 
{ 
protected $fillable = [ 
    'id', 'werknemer_id', 'weeknummer', 'dag', 'uren', 'toelichting', 
]; 

public function User() { 
    return $this->belongsTo(User::class); 
} 
} 

小時遷移標籤樂

<?php 

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateHoursTable extends Migration 
{ 
/** 
* Run the migrations. 
* 
* @return void 
*/ 
public function up() 
{ 
    Schema::create('hours', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('werknemer_id')->unsigned(); 
     $table->integer('weeknummer'); 
     $table->string('dag'); 
     $table->integer('uren'); 
     $table->string('toelichting'); 
     $table->rememberToken(); 
     $table->timestamps(); 
    }); 

    Schema::table('hours', function (Blueprint $table) 
    { 
     $table->foreign('werknemer_id')->references('id')->on('users'); 
    }); 
} 

/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    Schema::dropIfExists('hours'); 
} 
} 

視圖

<a href="{{ url('/uren/create') }}">Voeg je uren toe</a> 
<br/> 
<table class="display table table-bordered table-condensed table-responsive dynamic-table"> 
    <thead> 
     <tr> 
      <th>Werknemer</th> 
      <th>Week</th> 
      <th>Dag</th> 
      <th>Aantal uren</th> 
      <th>Toelichting</th> 
     </tr> 
    </thead> 

    <tbody> 


    enter code here 


     @foreach($hours as $hour) 
     <tr class="clickable-row" data-url="/uren/{{ $hour->id }}"> 
      {{dd($hour)}} 
      <td>{{ $hour->User->name}}</td> 
      <td>{{ $hour->weeknummer }}</td> 
      <td>{{ $hour->dag }}</td> 
      <td>{{ $hour->uren }}</td> 
      <td>{{ $hour->toelichting }}</td> 
      <td><a href="{{ url('/uren', array('id' => $hour->id)) }}">Laat zien</td> 
      <td><a href="{{ url('/uren/' . $hour->id . '/edit') }}">Bijwerken</a></td> 
      {!! Form::open(array('route' => array('uren.destroy', $hour->id), 'method' => 'delete')) !!} 
      <td><button class="btn btn-danger" data-toggle="confirmation" type="submit"><i class="fa fa-times"></i>Verwijder</button></td> 

      {!! Form::close() !!} 
      </tr> 
      @endforeach 

    </tbody> 

    </table> 
    <a href="/home">Keer terug naar het dashboard.</a> 

的視圖,$小時dd'd

Hour {#200 ▼ 
    #fillable: array:6 [▼ 
    0 => "id" 
    1 => "werknemer_id" 
    2 => "weeknummer" 
    3 => "dag" 
    4 => "uren" 
    5 => "toelichting" 
    ] 
    #connection: null 
    #table: null 
    #primaryKey: "id" 
    #keyType: "int" 
    #perPage: 15 
    +incrementing: true 
    +timestamps: true 
    #attributes: array:9 [▼ 
    "id" => 1 
    "werknemer_id" => 1 
    "weeknummer" => 5 
    "dag" => "Wednesday" 
    "uren" => 5 
    "toelichting" => "" 
    "remember_token" => null 
    "created_at" => "2017-02-01 08:56:10" 
    "updated_at" => "2017-02-01 08:56:10" 
    ] 
    #original: array:9 [▼ 
    "id" => 1 
    "werknemer_id" => 1 
    "weeknummer" => 5 
    "dag" => "Wednesday" 
    "uren" => 5 
    "toelichting" => "" 
    "remember_token" => null 
    "created_at" => "2017-02-01 08:56:10" 
    "updated_at" => "2017-02-01 08:56:10" 
    ] 
    #relations: [] 
    #hidden: [] 
    #visible: [] 
    #appends: [] 
    #guarded: array:1 [▼ 
    0 => "*" 
    ] 
    #dates: [] 
    #dateFormat: null 
    #casts: [] 
    #touches: [] 
    #observables: [] 
    #with: [] 
    +exists: true 
    +wasRecentlyCreated: false 
} 

的實際結果,如果DD被冷落

https://gyazo.com/73a83f411728084b5b09e806132ed76e?token=2a47870927ce239b03763c8dc24384a5

+0

份額的var_dump($小時); – Naincy

+0

https://gyazo.com/5fc0a0654cad9beedd23c6db6c03e100 - 不會呈現;只是拋出錯誤。 –

+0

do var_dump($ hours);出口; ....如果你分享20號線呢? – Naincy

回答

0

問題出在您的命名上。 Laravel有一個你期望遵循的命名約定,如果你不這樣做,你必須讓系統知道。

在你小時的模型,你有這樣的用戶功能,像這樣:

public function User() { 
     return $this->belongsTo(User::class); 
    } 

它希望你的外鍵被user_id說明不werknemer_id。爲了糾正這一點,添加id作爲第二ARG

public function User() { 
     return $this->belongsTo(User::class, 'werknemer_id'); 
    } 

public function werknemer() { 
     return $this->belongsTo(User::class); 
    } 
+0

https://gyazo.com/635cd1d8e489f92be3623513f0f47862 ---修復---非常感謝,法蒂瑪。 –