skip to Main Content

Error even though the column exists in the migration table.

SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘name’ in
‘field list’ (SQL: insert into tasks (name, description,
statuse_id, updated_at, created_at) values (Padaryti pietus,
fsd, 2, 2021-07-07 09:45:47, 2021-07-07 09:45:47))

Migration

Schema::create('tasks', function (Blueprint $table) {
    $table->id();
    $table->string('task_name', 128);
    $table->text('task_description');
    $table->unsignedBigInteger('statuse_id');
    $table->foreign('statuse_id')->references('id')->on('statuses');
    $table->timestamps();
});

Task Controller

public function store(Request $request)
{
   $validator = Validator::make($request->all(),
   [
       'task_name' => ['required', 'min:3', 'max:128'],
       'task_description' => ['required'],
       'statuse_id' => ['required', 'integer', 'min:1'],
   ]
   );
   if ($validator->fails()) {
       $request->flash();
       return redirect()->back()->withErrors($validator);
   }
   
   $task = new Task;
   $task->name = $request->task_name;
   $task->description = $request->task_description;
   $task->statuse_id = $request->statuse_id;
   $task->save();

   return redirect()->route('task.index')
       ->with('success_message', 'New task created successfuly');
 }

Task create.blade.html

<form method="POST" action="{{ route('task.store') }}">
    <div class="form-group">
        <label>Name:</label>
        <input type="text" name="task_name" class="form-control" 
               value="{{old('task_name')}}">
        <small class="form-text text-muted">Add a task name</small>
    </div>
    <label>Description:</label>
    <textarea name="task_description" id="summernote">
        {!!old('task_description')!!}
    </textarea>
    <small class="form-text text-muted">Add task description</small>
    <div class="form-group">
        <select name="statuse_id">
            @foreach ($statuses as $statuse)
            <option value="{{$statuse->id}}">{{$statuse->name}}</option>
            @endforeach
        </select>
        <small class="form-text text-muted">Select a status</small>
    </div>
    @csrf
    <button type="submit" class="btn">ADD</button>
</form>

I see task_name in my DB table in PhpMyAdmin. What is the solution to this error in this situation?

3

Answers


  1. You are trying to insert in the database data in the column name while it doesn’t exist.

    Change this line:

       $task->name = $request->task_name;
    

    into

       $task->task_name = $request->task_name;
    

    And it will work.

    Login or Signup to reply.
  2. Replace this line

    $task->name = $request->task_name;
    

    with

    $task->task_name = $request->task_name;
    
    Login or Signup to reply.
  3. So, for repeating it once again but just to explain a bit more, in your migration, you have set it as task_name, what you have to do in your TaskController is just name, so, changing it to task_name is required to do the task.

    Also, this is the reason for receiving the error of column not found.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search