kriszpchicken's avatar

Adding creating by and updated by to TO-DO list

I am working on a to-do application, and I figured out how to add the logged in user's id to a new data they created, so you can see who created the task, but I would like to also see who updated it. My basic idea would be, to refer to the user id in the migrations both in the case of created_by and updated_by, but then when you create a new task, I get an error message that of course I didn't put any value for my updated_by. This is the first time for me doing this, so I'm kinda confused.

Migrations:


<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTasksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        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->bigInteger("updated_by")->unsigned();
            $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;

class TaskController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tasks = Task::all();
        $user = Auth::user();

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('tasks/create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|max:255',
            'description' => 'required|max:350'
        ]);
        $task = new Task();
        $task->title = $request->title;
    	$task->description = $request->description;
    	$task->created_by = Auth::id();
    	$task->save();
   
        return redirect('/tasks')->with('success', 'Aufgabe wurde erfolgreich hinzugefügt!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $task = Task::findOrFail($id);

        return view('tasks/show', compact('task'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $task = Task::findOrFail($id);

     return view('tasks/edit', compact('task'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Task $task)
    {
        $this->validate($request, [
            'title' => 'required|max:255',
            'description' => 'required|max:350'
        ]);
        $task->title = $request->title;
        $task->description = $request->description;
        $task->updated_by = Auth::id();
    	$task->save();
   

        return redirect('/tasks')->with('success', 'Aufgabe wurde erfolgreich bearbeitet!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $task = Task::findOrFail($id);
        $task->delete();

        return redirect('/tasks')->with('success', 'Aufgabe wurde erfolgreich gelöscht!');
    }
}
0 likes
5 replies
andrea_ortu's avatar
Level 4

Try to change $table->bigInteger("updated_by")->unsigned(); in $table->unsignedBigInteger("updated_by")->nullable();

1 like
kriszpchicken's avatar

Thank you it worked! Would you also be able to tell me how to show the names of the users who created and updated the task in the index page as well? I can't figure it out because both reference the same id.

andrea_ortu's avatar

Go to your task model and add

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

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

then show name

$task = Task::first(); //example
$task->createdBy->name
$task->updatedBy->name
1 like
kriszpchicken's avatar

Thanks! However, if I do that, I get the this error message:

Trying to get property 'name' of non-object

kriszpchicken's avatar

I figured it out!

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

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

Thank you for showing me the right way! :)

1 like

Please or to participate in this conversation.