IntroLaravel - Benutzerfremdschlüssel was zu versuchen, Eigentum von Nicht-Objekt
Lieber Stacker,
ich zur Zeit auf einem kleinen Projekt, das ist Arbeit mich eher nehmen eine große Summe Zeit bin zu bekommen. Dies ist eine ineffiziente Produktion und daher möchte ich mein Problem lösen. Also, was ist das Problem?
Das Problem
ich innerhalb des $ Stunde Variable auf eine Eigenschaft zuzugreifen versuchen. Diese Eigenschaft ist das Feld "Name" des Benutzermodells (das Modell, das Ihnen von der Standardinstallation von Laravel zur Verfügung gestellt wird). Die Benutzer- und Stundenmodelle sind über "werknemer_id" in der Hour-Tabelle miteinander verknüpft, die auf das "id" -Feld in der User-Tabelle verweist. Wenn ich jedoch versuche, den Benutzer anzurufen, gibt es mir einen Fehler "Versuch, Eigenschaft von Nicht-Objekt zu erhalten".
Datenbank Beziehung https://gyazo.com/5a39f76d7fe6e297551e74a46a8def7c
Benutzermodell
<?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',
];
}
Stunden-Modell
<?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);
}
}
Stunden Migration Tab le
<?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');
}
}
Der Blick
<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>
Die Aussicht, mit $ Stunde 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
}
Das tatsächliche Ergebnis, wenn die dd links ist aus
https://gyazo.com/73a83f411728084b5b09e806132ed76e?token=2a47870927ce239b03763c8dc24384a5
Aktie var_dump ($ Stunden); – Naincy
https://gyazo.com/5fc0a0654cad9beedd23c6db6c03e100 - Doesin't rendert; wirft nur den Fehler. –
do var_dump ($ Stunden); Ausfahrt; .... auch wenn Sie teilen, was in Zeile 20 ist? – Naincy