1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Laravel 11 not saving data in to the database

Discussão em 'Outras Linguagens' iniciado por MUGWANEZA MANZI Audace, Outubro 30, 2024 às 18:22.

  1. I'm developing a Laravel 11 application that asks users to submit data, which is then saved to a database. My migrations have successfully created the necessary tables, but I'm having an issue. I can't seem to save data to the database. After submitting the form, there are no errors, but the database remains empty, and I am redirected to index.php as expected.

    I've included parts of my code below for reference:

    ProjectController.php


    <?php

    namespace App\Http\Controllers;

    use Nette\Schema\ValidationException;
    use App\Models\project;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;

    class ProjectController extends Controller
    {
    /**
    * Display a listing of the resource.
    */
    public function index()
    {
    $projects = project::query()->orderBy('created_at', 'desc')->paginate(5);
    return view('project.index', ['project' => $projects]);
    }

    /**
    * Show the form for creating a new resource.
    */
    public function create()
    {
    return view('project.create');
    }

    /**
    * Store a newly created resource in storage.
    */
    public function store(Request $request)
    {
    dd("Form submitted");
    try {
    $data = $request->validate([
    'title' => ['required', 'string'],
    'university' => ['required', 'string'],
    'category' => ['required', 'string'],
    'description' => ['required', 'string'],
    'yesno' => ['required', 'string'],
    'partner' => ['string'],
    'concept' => ['required', 'string'],
    'video' => ['required', 'string'],
    ]);
    $data['user_id'] = Auth::id();
    $project = project::create($data);
    return redirect()->route('project.index', $project)->with('success', 'Project created successfully!');

    } catch (ValidationException $e) {
    // Handle validation errors
    return back()->withErrors($e->validator)->withInput();
    }
    }
    }

    Project.php


    <?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class Project extends Model
    {
    protected $fillable = [
    'title', 'university', 'category', 'description',
    'yesno', 'concept', 'video', 'partner', 'user_id'
    ];
    }

    create.blade.php


    <x-app-layout>
    <x-slot name="dashboard">
    <h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
    {{ __('Dashboard') }}
    </h2>
    </x-slot>

    <div class="py-12">
    <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
    <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
    <div class="p-6 text-gray-900 dark:text-gray-100">
    <form action="{{ route('project.store') }}" class="space-y-4 p-4 bg-gray-100 rounded shadow-md" method="POST">
    @csrf
    <!-- Project Title -->
    <div class="flex items-center">
    <label class="w-1/4 font-semibold text-black">Project Title:</label>
    <input type="text" name="title" class="rounded border border-gray-300 p-2 w-full">
    </div>
    <!-- Other fields here... -->

    <!-- Submit Button -->
    <div class="flex justify-end">
    <input type="submit" name="sub" class="bg-blue-500 text-white rounded px-4 py-2 hover:bg-blue-600">
    </div>
    </form>
    </div>
    </div>
    </div>
    </div>
    </x-app-layout>

    CreateProjectsTable.php


    <?php

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

    return new class extends Migration
    {
    public function up(): void
    {
    Schema::create('projects', function (Blueprint $table) {
    $table->id();
    $table->string('title', 50);
    $table->string('university', 50);
    $table->string('category', 50);
    $table->longText('description');
    $table->string('yesno', 5);
    $table->text('partner');
    $table->string('concept', 500);
    $table->string('video', 100);
    $table->foreignId('user_id')->constrained('users');
    $table->timestamps();
    });
    }

    public function down(): void
    {
    Schema::dropIfExists('projects');
    }
    };

    .env


    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=student_showcase
    DB_USERNAME=root
    DB_PASSWORD=


    Could you help me understand why data isn't being saved to the database and why there are no error messages?

    Continue reading...

Compartilhe esta Página