Show name of relationship Laravel 5.1


How can I show the name of the client with whom it is related?

Migration projects

Schema::create('projects', function (Blueprint $table) {
            $table->string('nombre', 60);
            //para relacionar con cliente

Migration clients

Schema::create('clients', function (Blueprint $table) {
            $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) 
            <th scope="row">{{$project->id}}</th> 
            <td>{{$project->clients->nombre}}</td> //AQUI
            <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'])!!}

asked by ztvmark 01.05.2016 в 05:41

2 answers


To maintain consistency with the way Laravel works and the belongsTo method, I suggest that you use the singular when you declare the relation in Project and / or that you add the foreign key as the second parameter:

class Project extends Model
    public function client(){
        //Este proyecto pertenece a un cliente
        return $this->belongsTo('App\Client');

Here is Laravel's reference (5.2):

 * Define an inverse one-to-one or many relationship.
 * @param  string  $related
 * @param  string  $foreignKey
 * @param  string  $otherKey
 * @param  string  $relation
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
    // If no relation name was given, we will use this debug backtrace to extract
    // the calling method's name and use that as the relationship name as most
    // of the time this will be what we desire to use for the relationships.
    if (is_null($relation)) {
        list($current, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

        $relation = $caller['function'];

    // If no foreign key was supplied, we can use a backtrace to guess the proper
    // foreign key name by using the name of the relationship function, which
    // when combined with an "_id" should conventionally match the columns.
    if (is_null($foreignKey)) {
        $foreignKey = Str::snake($relation).'_id';

    $instance = new $related;

    // Once we have the foreign key names, we'll just create a new Eloquent query
    // for the related models and returns the relationship instance which will
    // actually be responsible for retrieving and hydrating every relations.
    $query = $instance->newQuery();

    $otherKey = $otherKey ?: $instance->getKeyName();

    return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
answered by 01.05.2016 в 05:58

I already found the error link It was in client_id and must be clients_id In other words, it must be called the same as the table plus _id // to relate to client $ table- > integer ('clients_id') - > unsigned ();             $ table- > foreign ('clients_id')                 - > references ('id') - > on ('clients')                 -> onDelete ('cascade');                 - > references ('id') - > on ('clients')                 -> onDelete ('cascade');

answered by 02.05.2016 в 03:45