Reputation: 2893
I seem to be having a problem. I have my index controller which does this:
public function index()
{
$projects = Project::dateDescending()->get();
return view('projects.index', compact('projects'));
}
If I dump $projects
, I can see I have two objects. One of the attributes of a project is
"clientId" => 1
A Project
has one Client
and a Client
belongs to a Project
, this is all set up. In my index view, I am doing this:
@foreach( $projects as $project )
<tr>
{!! Form::open(array('class' => 'form-inline', 'method' => 'DELETE', 'route' => array('projects.destroy', $project->id))) !!}
<td>{{ $project->id }}</td>
<td>{{ $project->clientId->clientName }}</td>
<td>{{ $project->clientStatus }}</td>
<td>{{ $project->projectName }}</td>
<td>{!! link_to_route('projects.show', 'Show', array($project->id), array('class' => 'btn btn-info')) !!}</td>
<td>{!! link_to_route('projects.edit', 'Edit', array($project->id), array('class' => 'btn btn-info')) !!}</td>
<td>{!! Form::submit('Delete', array('class' => 'btn btn-danger')) !!}</td>
{!! Form::close() !!}
</tr>
@endforeach
For some reason, I am getting an error because of this line
<td>{{ $project->clientId->clientName }}</td>
But shouldn't I have access to the clientName
because of the relationships that are set up?
This is the weird part. If I only have one project in the database, the client name displays fine. It's only when I add a second project I get this error.
What could be causing this?
UPDATE
My migrations are like so:
Schema::create('clients', function (Blueprint $table) {
$table->increments('id');
$table->string('clientName')->default('');
$table->string('contactEmail')->default('');
$table->string('slug')->default('');
$table->timestamps();
});
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->integer('clientId')->unsigned()->default(0);
$table->foreign('clientId')->references('id')->on('clients')->onDelete('cascade');
$table->string('contactName')->default('');
$table->string('projectName')->default('');
$table->timestamps();
});
My Client model:
class Client extends Model
{
protected $table = 'clients';
protected $guarded = [];
public function project()
{
return $this->belongsTo('App\Project', 'clientId');
}
}
My Project model:
class Project extends Model
{
protected $table = 'projects';
protected $guarded = [];
public function scopeDateDescending($query)
{
return $query->orderBy('created_at','DESC');
}
public function client()
{
return $this->hasOne('App\Client', 'id');
}
}
Upvotes: 1
Views: 1380
Reputation: 62228
It doesn't sound like you have your relationships setup correctly. Since your projects table contains the foreign key to the clients table (projects.clientId), that means that project belongs to client and client has one or many projects.
Your models should look something like:
class Project extends Model
{
public function client()
{
return $this->belongsTo('App\Client', 'clientId');
}
}
class Client extends Model
{
public function project()
{
return $this->hasOne('App\Project', 'clientId');
}
}
Now, from your Project, you can access the Client information through the client
relationship:
<td>{{ $project->client->clientName }}</td>
Upvotes: 1
Reputation: 2780
have u added the client-project relationships in your models. Please refer http://laravel.com/docs/5.1/eloquent-relationships#one-to-one
Also if you have added it, are you using it in the correct way. I think, it should be more like
$project->client->clientName
It would be helpful if you could show the code for model here.
Upvotes: 1