How can I show the name of the client with whom it is related?
Migration projects
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('nombre', 60);
$table->string('descripcion');
//para relacionar con cliente
$table->integer('client_id')->unsigned();
$table->foreign('client_id')
->references('id')->on('clients')
->onDelete('cascade');
Migration clients
Schema::create('clients', function (Blueprint $table) {
$table->increments('id');
$table->string('nombre', 60);
Projects controller
use App\Project;
use App\Client;
public function index()
{
$projects = Project::all();
return view('admin/projects.index',compact('projects'));
}
Model project
class Project extends Model
{
public function clients(){
//Este proyecto pertenece a un cliente
return $this->belongsTo('App\Client');
}
}
Model Client
class Client extends Model
{
public function projects(){
//Este cliente tiene muchos proyectos
return $this->hasMany('App\Project');
}
}
How can I show the name of the client it relates to in the view? I get Trying to get property of non-object
@foreach($projects as $project)
<tbody>
<tr>
<th scope="row">{{$project->id}}</th>
<td>{{$project->nombre}}</td>
//AQUI//AQUI//AQUI//AQUI
<td>{{$project->clients->nombre}}</td> //AQUI
<td>{{$project->descripcion}}</td>
<td class="form-inline">
<a href="{{ route('admin.projects.edit', $project->id) }}" class="btn btn-primary">Editar</a>
{!!Form::open(['route'=>['admin.projects.destroy', $project->id], 'method' => 'DELETE'])!!}
{!!Form::submit('Eliminar',['class'=>'btn btn-danger'])!!}
{!!Form::close()!!}
</td>
</tr>
</tbody>
@endforeach