如何在Laravel中解决此检查数据库值/可变可用性问题

时间:2018-03-10 06:59:31

标签: php mysql database laravel

我正在创建一个Ticket Reservation System,我想检查数据库中的数据可用性。像往常一样,我使用HTML表单,当有人试图插入已存在于数据库中的数据时,我想向他们显示“已有数据”的消息。如果数据是唯一的,我想插入数据库。但是,当我单击“提交”按钮时,没有任何反应。 (座位号=项目) 在浏览器的控制台中显示这些错误 -

  

POST http://localhost/FinalProject/public/seatprocess 500(内部   服务器错误)XHR加载失败:POST   “http://localhost/FinalProject/public/seatprocess

在网络标签中 - >响应显示如下 -

{
    "message": "Object of class Illuminate\\Database\\Query\\Builder could not be converted to string",
    "exception": "ErrorException",
    "file": "D:\\wamp64\\www\\FinalProject\\app\\Http\\Controllers\\SeatsController.php",
    "line": 42,
    "trace": [
        {
            "file": "D:\\wamp64\\www\\FinalProject\\app\\Http\\Controllers\\SeatsController.php",
            "line": 42,
            "function": "handleError",
            "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
            "type": "->"
        },

这是继续进行的。可能是我的seatprocess功能错了。但是,我不知道如何修复它。

Form and Seat Structure Image

我该如何解决这个问题?

这是我的Seats.blade.php

<form class="form-horizontal" id="form1" method="POST" action="{{ route('seatsinsert') }}" enctype="multipart/form-data">    

    {{ csrf_field() }}    

        <div class="dt"> <br>

    <h4> <span id="success_message" class="text-success"></span> </h4>

    <div class="form-group row">
    <label for="example-date-input" class="col-2 col-form-label">Select Date :</label>
    <div class="col-10">
    <input class="form-control" type="date" name="date" placeholder="mm-dd-yyyy" id="example-date-input">
    </div>
    </div>

     <div class="form-group">
    <label for="exampleSelect1">Select Time :</label>
    <select name="st" class="form-control" id="exampleSelect1">
      <option>10.30 am</option>
    </select>
    </div>  

        </div>

      <h2 style="font-size:1.2em;font-family: Times New Roman;"> Choose seats by clicking below seats :</h2>

      <div id="holder"> 
    <ul id="place">
        </ul>    
      </div>

      <div style="width:600px;text-align:center;overflow:auto"> <br>

      <input type="submit" class="btn btn-primary" id="btnShowNew" value="Continue"> <br><br>

       <span id="availability"></span>          

    <script type="text/javascript">

        $(function () {

            $('#btnShowNew').click(function (e) {
                e.preventDefault();

                var items = [];
                $.each($('#place li.' + settings.selectingSeatCss + ' a'), function (index, value) {
                    items.push($(this).attr('title'));
                });

                    console.log(items);
                   // $(location).attr('href', 'Seats');

                   $.ajax({
                   url:'{{ route('seatprocess') }}',
                   type:"POST",
                   data:{ 
                   _token: "{{ csrf_token() }}", 
                   items: JSON.stringify(items), 
                   date: $('input[name=date]').val(), 
                   st: $('select[name=st]').val()},
                   success:function(data)
                   {
                    if(data !== '0')
                    {
                     $('#availability').html('<span class="text-danger">Seats not available</span>');
                     $('#btnShowNew').attr("disabled", true);
                    }
                    else
                    {
                     //$('#availability').html('<span class="text-success">Seats Available</span>');

                    $.ajax({ 
                    type: "post", 
                    url: "{{ route('seatsinsert') }}", 
                    data: { 
                    _token: "{{ csrf_token() }}", 
                    items: JSON.stringify(items), 
                    date: $('input[name=date]').val(), 
                    st: $('select[name=st]').val()}, 
                    success: function(data){ 
                    $("form").trigger("reset"); 
                    $('#success_message').fadeIn().html("Text"); 
                    } 
                    });

                    $('#btnShowNew').attr("disabled", false);
                    }
                   }
                  });

                }); //btnShowNew

         }); //Final 

    </script>

    </form>

这是我的SeatsController.php

public function seatsinsert(Request $request)
    {

        $date = $request->input('date');
        $st = $request->input('st');
        $items = $request->input('items'); 
        $items = str_replace(['[', ']', '"'], '', $items); 

        $user = new Seats(); 
        $user->date = $date; 
        $user->st = $st; 
        $user->item = $items; 

        $user->save();

    }  

    public function seatprocess(Request $request)
    {
    //dd($request->all());

    $items = $request->input('items');     

    $results = DB::table('seats')->where('item',$items);

    echo $results;

    }
 }

这是我的路线。

Route::post('seatsinsert',[
'uses'=> 'SeatsController@seatsinsert',
'as' => 'seatsinsert'
]);
Route::post('seatprocess',[
'uses'=> 'SeatsController@seatprocess',
'as' => 'seatprocess'
]);

1 个答案:

答案 0 :(得分:0)

在回显$result变量之前,您需要使用这样的方法检索响应函数中的数据。

这是您的控制台出错的原因。

你必须像这样更改一行:

$results = DB::table('seats')->where('item',$items)->get();