Laravel - 存储在变量中但在数据库中变为空的数据

时间:2017-02-10 20:05:14

标签: laravel model-view-controller laravel-5 controller

我的功能问题是将数据保存到数据库中,

基本上用户将名称放入表单中,当我执行console.log时,我确实得到了该值,然后我在控制器中调用我的函数,一切看起来都很成功 - 没有错误但是当我检入mysql name = NULL时,为什么?< / p>

我的表格:

<div class="modal fade" id="myModalHorizontal" tabindex="-1" role="dialog" 
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <button type="button" class="close" 
                   data-dismiss="modal">
                       <span aria-hidden="true">&times;</span>
                       <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    Website Name
                </h4>
</div>
           <div class="modal-body">
                <form action="{{ action('BuilderController@postDB') }}"
                class="form-horizontal" role="form" method = "POST">
                  <div class="form-group">
                  <input type="hidden" name="_token" value="{{ csrf_token() }}">
                  <input type="hidden" id="code" name="newCode" value="">
                    <label  class="col-sm-2 control-label"
                              for="website_name">Name</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" 
                        id="website_name" placeholder="Website Name"/>
            </div>
        </div>
    </div>  

            <!-- Modal Footer -->
            <div class="modal-footer">
                <button type="button" class="form-control margin btn btn-danger"
                        data-dismiss="modal">
                            Close
                </button>
                <button onClick=" updateDatabase(this);" type="submit" class="form-control margin btn btn-success" id="getRequest changes">
                    Save Website
                </button>
            </div>
        </div>
    </div></form>
</div>

JS:

var web_name;
function updateDatabase()
{
    code2 = document.getElementById("content-link2").innerHTML;
    var newCode = document.getElementById('code').value = code2;
    web_name = ($('#website_name').val());
        console.log(web_name);
        console.log(newCode);

控制器:

public function postDB(Request $request) {
    $newName = $request->input('web_name');
    $newLat = $request->input('newCode');
    $websites = new Website();
    $websites->name = $newName;
    $websites->html = $newLat;
    $websites->save();

    // Now we go to our other function
    return $this->website($newName);
}
public function website($newName)
{
    // Return our "website" object
    $html = Website::where('name', $newName)->first();

    // Pass the contents of the "html" property to the view
    return view('layouts/website', [
        'html' => $html->html
    ]);
}

1 个答案:

答案 0 :(得分:0)

您需要将name属性添加到input元素:

<input name="website_name" ...