kriszpchicken's avatar

Display name of user instead of id

I am working on a task application, and I would like to display the name of the user, who created or updated the task. However, I can only display the id of the user.

Migrations:

Schema::create('tasks', function (Blueprint $table) {
            $table->id();
            $table->string("title");
            $table->string("description");
            $table->bigInteger("created_by")->unsigned();
            $table->foreign("created_by")->references("id")->on("users")->onDelete("cascade");
            $table->unsignedBigInteger("updated_by")->nullable();
            $table->foreign("updated_by")->references("id")->on("users")->onDelete("cascade");
            $table->timestamps();

TaskController:


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Task;
use App\User;

class TaskController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tasks = Task::with(["user"])->get();
        $user = User::all();
        

     return view('tasks/index', compact('tasks','user'));
    }

index.blade.php:


@foreach($tasks as $task)
            <tr>
                <td>{{$task->id}}</td>
                <td>{{$task->title}}</td>
                <td>{{$task->description}}</td>
                <td>{{$task->created_at}}</td>
                <td>{{$task->user->created_by}}</td>

0 likes
5 replies
bito's avatar

Hello,

Change the index.blade.php

<td>{{$task->created_by->name}}</td>

and in the Task model add this

public function created_by()
    {
        return $this->belongsTo('App\User');
    }
    public function updated_by()
    {
        return $this->belongsTo('App\User');
    }

In task controller you don't need to pass the user

kriszpchicken's avatar

Hello!

Thank you for your suggestion. However, if I do that I get the following error message:

Facade\Ignition\Exceptions\ViewException Trying to get property 'name' of non-object

brentxscholl's avatar

in Task.php (task model) add this relationship

public function creator()
        {
            return $this->belongsTo('App\User', 'user_id'); // be sure to set 'user_id` since this relationship is called 'creator' and not 'user'. if you do not do this, it will look for 'creator_id` in the tasks table and you will get 'object not found'
        }

You can do the reverse in your User.php (user model)

public function tasks()
        {
            return $this->hasMany('App\Task');
        }

Then in your view

{{ $task->creator->name }} // that is if your users table has a field for 'name'
kriszpchicken's avatar

Thanks for the tip! I tried it but I'm still getting the error message

Facade\Ignition\Exceptions\ViewException Trying to get property 'name' of non-object

kriszpchicken's avatar
kriszpchicken
OP
Best Answer
Level 1

Ok, so apparently this is the right answer:

public function createdBy()
{
    return $this->belongsTo(User::class, "created_by");
}

public function updatedBy()
{
    return $this->belongsTo(User::class, "updated_by");
}

Thank you for your help in figuring it out! :)

Please or to participate in this conversation.