我无法在 Laravel 中向数据库添加数据

问题描述 投票:0回答:1
public function projects(Request $request)
{
    if ($request->isMethod('post')) {
        $validatedData = $request->validate([
            'title' => 'required',
            'description' => 'required',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
        ]);

        
        $imagePath = $request->file('image')->store('public/images');

        $project = new Projects();
        $project->title = $validatedData['title'];
        $project->description = $validatedData['description'];
        $project->image = $imagePath; 
        $project->save();
    }

    $projects = Projects::all();

    return view('home/pages/projects', [
        'projects' => $projects
    ]);
}

HomeController.php

Route::post('/projeler', [HomeController::class, 'projects'])->name('projects');

Web.php

    <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Projects extends Model
{
    protected $fillable = [
        'title',
        'image',
        'description',
        'slug',
        'created_at',
        'updated_at',
    ];
}

Projects.php 作为模型。

    <div class="d-flex justify-content-center mt-5">
    <button onclick=showDialog() class="btn btn-primary">Add Project</button>
</div>

<div id="myModal" class="modal">
    <div class="modal-content">
        <span class="close d-flex justify-content-end"  onclick="closeDialog()">&times;</span>
        <h2>Add Project</h2>
        <form action="{{ route('projects') }}" method="POST">
           <input type="hidden" name="_token" value="{{ csrf_token() }}">
          
            <div class="form-group">
                <label for="projectName">Proje Adı:</label>
                <input type="text" class="form-control" id="projectName" name="title">
            </div>
            <div class="form-group">
                <label for="projectDescription">Proje Açıklaması:</label>
                <textarea class="form-control" id="projectDescription" name="description"></textarea>
            </div>
            <div class="form-group">
                <label for="projectImage">Proje Görseli:</label>
                <input type="file" class="form-control-file" id="projectImage" name="image">
            </div>
            <button type="submit" class="btn btn-primary">Add</button>
        </form>
    </div>
</div>

<script>
function showDialog() {
    var modal = document.getElementById("myModal");
    modal.style.display = "block";
}

function closeDialog() {
    var modal = document.getElementById("myModal");
    modal.style.display = "none";
}
</script>

Projects.twig 文件。

我试图做的是,当用户按下名为“添加项目”的按钮时,会打开一个弹出窗口。这里有标题、描述和图片上传字段。当用户填写它们并单击“添加”按钮时,我尝试返回

projects.twig
页面并使用
Projects::all()
方法检索并显示数据库中的所有项目。我手动将其添加到数据库中,显示数据没有问题,或者当我按添加按钮时没有收到任何错误。但它不会将数据添加到数据库中。

sql laravel twig crud
1个回答
0
投票

如果您尝试提交表单,表单中应该有

enctype="multipart/form-data"
属性。 检查这个例子 https://www.w3schools.com/tags/att_form_enctype.asp

© www.soinside.com 2019 - 2024. All rights reserved.