Search code examples
phplaraveleloquentaccessor

Laravel get custom model attribute not working


The exact same thing is working for other models in the application but not this one & I can't figure out why. When i try and access the clientname attribute it is blank.

Model

class Domain extends Model {

protected $guarded = [];

public function clients() {
    return $this->belongsTo('App\Client');
}

public function getClientNameAttribute() {
    return Client::where('id', $this->client_id)->value('name');
}

}

Blade

  <tbody>
            @foreach( $domains as $domain )
            {!! Form::open(array('class' => 'form-inline', 'method' => 'DELETE', 'route' => array('domains.index', $domain->id))) !!}
            <tr>
                <td><a href="{{ route('domains.show', $domain->id) }}">{{ $domain->id }}</a></td>
                <td>{{ $domain->name }}</td>
                <td>{{ $domain->clientname }}</td>
                <td>{{ $domain->expiry_date }}</td>
                <td>  {!! link_to_route('domains.edit', 'Edit', array($domain->id), array('class' => 'btn btn-info')) !!}
                    </td>
            </tr>
            {!! Form::close() !!}

            @endforeach
        </tbody>

edited to add client/domain schema

Client

    Schema::create('clients', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('kashflow_id')->unique();
        $table->string('name');
        $table->string('contact');
        $table->string('telephone');
        $table->string('mobile');
        $table->string('fax');
        $table->string('email');
        $table->string('address1');
        $table->string('address2');
        $table->string('address3');
        $table->string('address4');
        $table->string('postcode');
        $table->timestamps();
    });

Domain

            Schema::create('domains', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients');
        $table->string('name');
        $table->date('expiry_date');
        $table->timestamps();
    });

Solution

  • I deleted my table and made a new migration and everything is working now strangely, thanks to everyone that tried to help