親愛的堆垛機的性能,
我目前工作的一個小項目,多數民衆贊成,而把我的時間一大筆。這是低效的生產,因此我想讓我的問題得到解決。所以有什麼問題?
我試圖訪問$小時變量中的屬性問題。此屬性是用戶模型的「名稱」字段(由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
份額的var_dump($小時); – Naincy
https://gyazo.com/5fc0a0654cad9beedd23c6db6c03e100 - 不會呈現;只是拋出錯誤。 –
do var_dump($ hours);出口; ....如果你分享20號線呢? – Naincy