Try to change $table->bigInteger("updated_by")->unsigned(); in $table->unsignedBigInteger("updated_by")->nullable();
Apr 4, 2020
5
Level 1
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!');
}
}
Level 4
1 like
Please or to participate in this conversation.