使用 modal 的 Laravel 8 crud 不向数据库添加数据

Posted

技术标签:

【中文标题】使用 modal 的 Laravel 8 crud 不向数据库添加数据【英文标题】:The Laravel 8 crud that uses the modal does not add data to the database 【发布时间】:2021-08-16 16:49:44 【问题描述】:

我在为一个大学项目工作,但我有一个问题...有谁知道为什么我不想在通过模态方法添加后将数据输入数据库。它也没有更新。它不会抛出一个错误。即,我想在同一页面上进行crud操作,即点击编辑时不要切换到另一个url,添加......只是添加删除按钮是这三个中唯一的一个......

index.blade.php

<?php
use App\Models\User;
use App\Models\Client;
use App\Models\Event;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap CRUD Data Table for Database with Modal Form</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<body>
    <div class="container">
        <div class="table-wrapper">
            <div class="table-title">
                <div class="row">
                    <div class="col-sm-6">
                        <h2>Uredi <b>klijente</b></h2>
                    </div>
                    <div class="col-sm-6">
                        <a href="#addEmployeeModal" class="btn btn-success" data-toggle="modal" style="float:right"><i class="material-icons">&#xE147;</i> <span>Dodaj novog klijenta</span></a>

                    </div>
                </div>
            </div>


            <table class="table table-striped table-hover">
                <thead>
                    <tr>

                        <th>Ime</th>
                        <th>Prezime</th>
                        <th>Datum rođenja</th>
                        <th>Email roditelja</th>
                        <th>Tel.roditelja</th>
                        <th>U terapiji?</th>
                        <th>Dijagnoza</th>
                        <th>Komentar</th>
                        <th>Logoped</th>
                        <th>Akcije</th>

                    </tr>
                </thead>
                <tbody>
                    @foreach($clients as $client)

                    <tr>
                        <td>$client->name</td>
                        <td>$client->lastname</td>
                        <td>$client->date_of_birth</td>
                        <td>$client->email</td>
                        <td>$client->telephone</td>
                        <td>$client->in_therapy</td>
                        <td>$client->diagnosis</td>
                        <td>$client->comments</td>
                        <td>$client->therapists_id</td>
                        <td>
                            <a href="#editEmployeeModal" class="edit" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Edit">&#xE254;</i></a>
                            <a href="#deleteEmployeeModal" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete">&#xE872;</i></a>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>

    </div>
    <!-- add Modal HTML -->
      <div id="addEmployeeModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <form>
                    <div class="modal-header">
                        <h4 class="modal-title">Dodaj klijenta</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    </div>
                    !! Form::open(['action' =>['ClientsController@store',''],'method'=>'POST','style'=>'width:50%',]) !!
                    csrf_field()

                    <div class="modal-body">

                        <div class="form-group">
                            Form::label('name','Ime')
                            Form::text('name','',['class'=>'form-control'])
                        </div>

                        <div class="form-group">
                            Form::label('lastname','Prezime')
                            Form::text('lastname','',['class'=>'form-control'])
                        </div>

                        <div class="form-group">
                            Form::label('date_of_birth','Datum rođenja')
                            Form::date('date_of_birth','',['class'=>'form-control'])
                        </div>

                        <div class="form-group">
                            Form::label('email','Email roditelja')
                            Form::text('email','',['class'=>'form-control'])
                        </div>
                        <div class="form-group">
                            Form::label('telephone','Tel. roditelja')
                            Form::text('telephone','',['class'=>'form-control'])
                        </div>
                        <div class="form-group">
                            Form::label('in_therapy','U terapiji?')<br/>
                            Form::checkbox('in_therapy','Da',['class'=>'form-control'])Da<br/>
                            Form::checkbox('in_therapy','Ne',['class'=>'form-control'])Ne<br/>
                        </div>
                        <div class="form-group">
                            Form::label('diagnosis','Dijagnoza')
                            Form::textarea('diagnosis','',['class'=>'form-control','rows' => 3, 'cols' => 170,])
                        </div>
                        <div class="form-group">
                            Form::label('comments','Komentari')
                            Form::textarea('comments','',['class'=>'form-control','rows' => 3, 'cols' => 170,])
                        </div>
                        <div class="form-group">
                            Form::label('user_id', 'Logoped')<br/>
                            Form::select('user_id', $sp_therapist, null, ['class' => 'form-control','placeholder' => 'Izaberite logopeda'])
                        </div>
                    </div>
                    <div class="modal-footer">
                        <input type="button" class="btn btn-default" data-dismiss="modal" value="Odustani">
                        <input type="submit" class="btn btn-success" value="Dodaj">
                    </div>
                    !!Form::close()!!
            </div>
        </div>
    </div>
    <!-- Edit Modal HTML -->

    <div id="editEmployeeModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <form>
                    <div class="modal-header">
                        <h4 class="modal-title">Edit Employee</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    </div>
                    !! Form::open(['action' =>['ClientsController@update',''],'method'=>'POST','style'=>'width:50%',]) !!

                    csrf_field()

                    <div class="modal-body">

                        <div class="form-group">
                            Form::label('name', 'Ime')
                            Form::text('name', $client->name, ['class' => 'form-control'])
                        </div>

                        <div class="form-group">
                            Form::label('lastname', 'Prezime')
                            Form::text('lastname', $client->lastname, ['class' => 'form-control', 'placeholder' ])
                        </div>

                        <div class="form-group">
                            Form::label('date_of_birth', 'Datum rođenja')
                            Form::date('date_of_birth', $client->date_of_birth, ['class' => 'form-control', 'placeholder' => 'Unesite datum rođenja djeteta'])
                        </div>

                        <div class="form-group">
                            Form::label('email', 'Email')
                            Form::text('email', $client->email, ['class' => 'form-control', 'placeholder' => 'Unesite email roditelja'])
                        </div>
                        <div class="form-group">
                            Form::label('telephone', 'Kontakt broj:')
                            Form::text('telephone', $client->telephone, ['class' => 'form-control', 'placeholder' => 'Unesite kontakt broj'])
                        </div>
                        <div class="form-group">
                            @if($client->in_therapy != 1)

                            <div class="form-group">
                                Form::label('in_therapy', 'U terapiji')<br>

                            @if($client->in_therapy == "Da")
                                Form::checkbox('in_therapy', 'Da',$client->in_therapy) Da <br>
                                Form::checkbox('in_therapy', 'Ne','') Ne<br>
                            @elseif($client->in_therapy == "Ne")
                                Form::checkbox('in_therapy', 'Da',) Da<br>
                                Form::checkbox('in_therapy', 'Ne',$client->in_therapy) Ne <br>
                            @endif

                            </div>
                        </div>
                        @else

                        <div class="form-group">
                            Form::label('in_therapy', 'U terapiji?')<br>
                        @if($client->in_therapy == "Da")
                            Form::text('in_therapy',$client->in_therapy,['class'=>'form-control','readonly '])  <br>

                        @elseif($client->in_therapy == "Ne")
                            Form::text('in_therapy',$client->in_therapy,['class'=>'form-control','readonly ']) <br>
                        </div>
                        @endif
                            <div class="form-group">
                                Form::label('in_therapy', 'U terapiji?')<br>
                            @if($client->in_therapy == "Da")
                                Form::text('in_therapy',$client->in_therapy,['class'=>'form-control','readonly '])  <br>

                            @elseif($client->in_therapy == "Ne")
                                Form::text('in_therapy',$client->in_therapy,['class'=>'form-control','readonly ']) <br>
                            </div>
                            @endif

                        @endif
                        <div class="form-group">
                            Form::label('diagnosis', 'Dijagnoza?')<br/>
                            Form::textarea('diagnosis', $client->diagnosis, ['class' => 'form-control', 'rows' => 5, 'cols' => 170])
                        </div>
                        <div class="form-group">
                            Form::label('comments', 'Komentari')<br/>
                            Form::textarea('comments',  $client->comments, ['class' => 'form-control', 'rows' => 5, 'cols' => 170, 'placeholder' => 'Unesite popratne komentare'])
                        </div>
                        <div class="form-group">
                            Form::label('therapists_id', 'Logoped')
                            Form::select('therapists_id', $sp_therapist, $client->therapists_id,['class' => 'form-control', 'placeholder' => 'Izaberite logopeda'])
                        </div>
                    </div>
                    <div class="modal-footer">
                        <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                        <input type="submit" class="btn btn-info" value="Save">
                    </div>
                    !!Form::close()!!
            </div>
        </div>
    </div>
    <!-- Delete Modal HTML -->
    <div id="deleteEmployeeModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                !!Form::open(['action' =>['ClientsController@destroy', $client->id], 'method' => 'POST'])!!
                    <div class="modal-header">
                        <h4 class="modal-title">Izbriši klijenta</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    </div>
                    <div class="modal-body">
                        <p>Da li ste sigurni da želite izbrisati ovog klijenta?</p>
                        <p class="text-warning"><small style="color: #000">Ova akcija se ne može poništiti</small></p>
                    </div>
                    <div class="modal-footer">
                        @method('DELETE')
                        <input type="button" class="btn btn-default" data-dismiss="modal" value="Odustani">
                        <input type="submit" class="btn btn-danger" value="Izbriši">
                    </div>
                    !!Form::close()!!
            </div>
        </div>
    </div>
</body>
</html>
@endsection

这是用于存储和编辑的控制器。

public function store(Request $request)
    
        $this->validate($request,[
            'name' => 'required',
            'lastname' => 'required',
            'date_of_birth' => 'required',
            'telephone' => 'required'
            ]);

            $client = new Client;
            $client->name = $request->input('name');
            $client->lastname = $request->input('lastname');
            $client->date_of_birth = $request->input('date_of_birth');
            $client->email = $request->input('email');
            $client->telephone = $request->input('telephone');
            $client->in_therapy = $request->input('in_therapy');
            $client->diagnosis = $request->input('diagnosis');
            $client->comments = $request->input('comments');
            $client->therapists_id = $request->input('user_id');
            $client->save();

            return redirect('/clients')->with('success', 'Klijent je unesen');

    

public function edit($id)
    
        $sp_therapist = DB::table('users')->pluck('name', 'id');
        $client = Client::find($id);
        return view('clients.edit')->with('client',$client)->with('sp_therapist', $sp_therapist);

    

这是 web.php


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FullCalenderController;
use App\Http\Controllers\Auth\RegisterController;


Auth::routes();
Route::group(['middleware' => 'auth'], function () 


Route::get('/','PagesController@index');

Route::get('/dashboard', 'DashboardController@index')->name('dashboard');

Route::get('/register', 'Auth\RegisterController@create')->name('register');

Route::resource('clients','ClientsController');

//CALENDAR
Route::get('schedule', [FullCalenderController::class, 'index']);

Route::post('schedule/action', [FullCalenderController::class, 'action']);

);

谢谢

【问题讨论】:

【参考方案1】:

检查您正在使用的模态是否具有带有您要插入的值的 $fillable 属性,如下所示:

class User extends Model 

protected $fillable = ['name', 'lastname', 'email']


否则 Eloquent 不允许您注入输入。

【讨论】:

我觉得没关系:protected $fillable = [ 'name', 'lastname', 'date_of_birth', 'email', 'telephone', 'in_therapy', 'diagnosis', 'comments', 'therapists_id', ]; 所以问题可能出在表格中,检查文档我看到该方法在 Laravel 4.2 中使用,但如果您使用的是 Laravel 8,则不确定它是否有效。【参考方案2】:

首先,如果你使用 laravel 8,请在你的 web.php 上使用它

use App\Http\Controllers\ClientsController;

Route::resource('clients',[ClientsController::class]); 

并在您的索引文件中将表单操作设置为 route('clients.store') ,使用路由名称

在您的商店控制器中尝试 dd($request) 以查看响应,如果您仍有任何问题,请告诉我们

【讨论】:

以上是关于使用 modal 的 Laravel 8 crud 不向数据库添加数据的主要内容,如果未能解决你的问题,请参考以下文章

如何为 Laravel 模块架构应用 CRUD 生成器?

Laravel 5.2 & AJAX - 重定向后显示成功消息

从另一个页面 Laravel 打开 Modal

我的视图不包括刀片模板的 CSS - laravel 8

Laravel 标记 CRUD

在 Laravel 中通过 Modal Box 查看和更新​​数据