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

问题描述

我在为大学做一个项目,我有一个问题......有谁知道为什么我不想在通过模态方法添加后将数据输入到数据库中。它也没有更新。它不会抛出一个错误。即,我想在同一页面上进行 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',])}}
                        </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',]) !!}

                    {{csrf_field()}}

                    <div class="modal-body">

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

                        <div class="form-group">
                            {{Form::label('lastname',$client->lastname,'placeholder' ])}}
                        </div>

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

                        <div class="form-group">
                            {{Form::label('email','Email')}}
                            {{Form::text('email',$client->email,'placeholder' => 'Unesite email roditelja'])}}
                        </div>
                        <div class="form-group">
                            {{Form::label('telephone','Kontakt broj:')}}
                            {{Form::text('telephone',$client->telephone,'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',$client->in_therapy)}} Da <br>
                                {{Form::checkBox('in_therapy','')}} Ne<br>
                            @elseif($client->in_therapy == "Ne")
                                {{Form::checkBox('in_therapy',)}} Da<br>
                                {{Form::checkBox('in_therapy',$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,'readonly '])}}  <br>

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

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

                        @endif
                        <div class="form-group">
                            {{Form::label('diagnosis','Dijagnoza?')}}<br/>
                            {{Form::textarea('diagnosis',$client->diagnosis,'rows' => 5,'cols' => 170])}}
                        </div>
                        <div class="form-group">
                            {{Form::label('comments','Komentari')}}<br/>
                            {{Form::textarea('comments',$client->comments,'placeholder' => 'Unesite popratne komentare'])}}
                        </div>
                        <div class="form-group">
                            {{Form::label('therapists_id','logoped')}}
                            {{Form::select('therapists_id',$client->therapists_id,'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','action']);

});

谢谢

解决方法

检查您使用的 Modals 是否具有包含您要插入的值的 $fillable 属性,如下所示:

class User extends Model {

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

}

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

,

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

use App\Http\Controllers\ClientsController;

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

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

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