Laravel have no output and database is empty as well after subbmiting


i am trying to create a todo-list by Laravel where individual users has their own todo, but when i click on the submit button, it display nothing, why?
Database is empty after subbmit the buttom and no output .
TodoController:

Tell me if here is the problem

    <?php

namespace AppHttpControllers;

use AppModelsTodo;
use IlluminateHttpRequest;
use AppModelsUser;
use IlluminateSupportFacadesAuth;

class TodoController extends Controller
{
   

    public function create()
    {
    

    return view('todos.todo');
    }


    public function index()
    {
        $user = Auth::user();

        return view('todos.todo', [
            'todos'=> Todo::orderBy('created_at', 'asc')->where('user_id', $user->id)->get(),
            
        ]);
    }

  
  
    public function store(Request $request)
    {   
        $this->validate($request, 
        ['description' => 'required|max:60']);
        $user = Auth::user();
        $todos = $request->all();
        $todos['user_id'] = $user->id;
        Todo::create($todos);

        return redirect('/todos')->with('success', 'Todo created');
    }
}

Todo.php

Tell me if here is the problem

    <?php

namespace AppModels;

use AppModelsUser;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentFactoriesHasFactory;

class Todo extends Model
{
    use HasFactory;

    protected $guarded = [];

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

User.php

Tell me if here is the problem

<?php

namespace AppModels;

use AppModelsTodo;
use LaravelSanctumHasApiTokens;
use AppHttpControllersTodoController;
use IlluminateNotificationsNotifiable;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $guarded = [];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function setPasswordAttribute($password)
    {
        $this->attributes['password'] = bcrypt($password);
    }

    public function todos()
    {
        return $this->hasMany(Todo::class);
    }

}

todos(folder)-todo.blade.php

Tell me if here is the problem

@extends('layouts.layout')
@section('content')
<div class="todo">
    <form action="/todos" method="POST">
        @csrf
<h1>Add ToDo</h1>
<label for="task">Create and manage your daily tasks: </label>
<input type="text" name="task" id="task">
<input type="submit" id="submit-btn2" value="Add">
@foreach ($todos as $todo)
 {{ $todos->todos }}
 @endforeach
</form>
</div>
@endsection

web.php

Tell me if here is the problem

Route::get('/', function () {
    return view('welcome');
});

Route::get('/register', [RegisterController::class, 'create'])->middleware('guest');
Route::post('/register', [RegisterController::class, 'store'])->middleware('guest');

Route::get('/login', [LoginController::class, 'create'])->name('login')->middleware('guest');
Route::post('/login', [LoginController::class, 'store'])->name('login')->middleware('guest');

Route::post('/logout', [LogoutController::class, 'destroy'])->middleware('auth');

Route::get('/todos', [TodoController::class, 'create'])->middleware('auth');
Route::get('/todos', [TodoController::class, 'index'])->middleware('auth');

Route::post('/todos', [TodoController::class, 'store'])->middleware('auth');

And the migration as well

Schema::create('todos', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('description');
            $table->timestamps();

Source: CSS – Stack Overflow

November 22, 2021
Category : News
Tags: css | html | laravel | mysql | php

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.