如何在Laravel中解决此检查数据库值/变量可用性 [英] How To Solve This Check Database Value / Variable Availability In Laravel

查看:68
本文介绍了如何在Laravel中解决此检查数据库值/变量可用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个票务预订系统,我想从数据库中检查数据可用性.像往常一样,我使用HTML表单,当有人尝试插入数据库中已经存在的数据时,我想向他们显示一条消息数据已经拥有".如果数据是唯一的,我想插入数据库.但是,当我单击提交"按钮时,没有任何反应. (座位号=物品) 在浏览器的控制台中显示这些错误-

I am creating a Ticket Reservation System and I want to check data availability from the Database. As usual, I used HTML form and when someone tries to insert data which is already in the database , I want to show them a message as "Data Already Have". And If data is unique , I want to insert into the database. But , when I click Submit button, nothing happens. ( Seat Number = Items ) In the console of the browser shows these errors -

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

POST http://localhost/FinalProject/public/seatprocess 500 (Internal Server Error) XHR failed loading: POST "http://localhost/FinalProject/public/seatprocess"

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

In Network tab -> Response shows like this -

{
    "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函数是错误的.但是,我不知道如何解决它.

And this is continuesly going. May be my seatprocess function is wrong. But , I have no idea how to Fix it.

形状和座椅结构图像

如何解决此问题?

这是我的Seats.blade.php

Here is my 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

Here is my 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'
]);

推荐答案

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

You need to retrieve data in your response function with get like this before echo the $result variable.

这是导致控制台错误的原因.

This is the reason behind error in your console.

您必须像这样更改行:

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

这篇关于如何在Laravel中解决此检查数据库值/变量可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆