Tuesday, February 28, 2017

Laravel - User foreign key resulting in trying to get property of non-object

Intro

Dear Stackers,

I am currently working on an small project thats rather taking me a large sum of time. This is inefficient production, and thus I want to get my problem solved. So, what's the problem?

The problem

I am trying to access a property inside the $hour variable. This property is the "name" field of the User model (The model provided to you by Laravel's Default Install). The User and Hour models are linked together through "werknemer_id" inside the Hour table which references to the "id" field inside the User table. When I try to call upon the user however, it returns me a "Trying to get property of non-object" error.

Database relationship http://ift.tt/2m6CQet

User Model

<?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',
];
}

Hour Model

<?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);
}
}

Hour Migration Table

<?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');
}
}

The view

<a href="">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/">
            
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td><a href="">Laat zien</td>
            <td><a href="">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>

The view, with $hour 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
}

The actual result if the dd is left out

http://ift.tt/2ltV7Ph




via nitsuJ mexoB

Advertisement