Laravel 5:如何使用按钮?

时间:2015-12-08 02:32:18

标签: laravel-5

我是laravel 5中的新手..我不知道如何使用按钮功能..我需要将数据保存在数据库中,但我不知道该怎么做。谢谢你的帮助!

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Create</title>
    <link rel="stylesheet" href="">
</head>
<body>
    <div><input type="text" placeholder="Enter Name here" name="name"></div>
    <div><input type="text" placeholder="Author" name="author"></div>
    <div><input type="hidden" name="action_create"></div>
    <div><textarea name="content" id="" cols="30" rows="10"></textarea</div>
    <div><button type="button">Button</button></div>

</body>
</html>
这是我的路线     

$router->get('create',['uses' => "TestController@create"]);
$router->post('create',['uses' => "TestController@store"]);
$router->get('edit/{blog_id?}',['uses' => "TestController@edit"]);
$router->post('edit/{blog_id?}',['uses' => "TestController@update"]);
$router->get('view',['uses' => "TestController@view"]);
$router->get('/',['uses' => "TestController@index"]);

public function store(InputRequest $input){
    // return 'lkjlasd';
    $new_blog = new Blog;

    $new_blog->fill($input->all());

    if($new_blog->save()){
        echo "save success.";
    }else{
        echo "db communication error";
    }

}

2 个答案:

答案 0 :(得分:0)

在没有创建表单的情况下开始将无法在您的表单上发送任何内容 方法是你不发送任何东西

<强>索引

<form method="POST" action="backend" accept-charset="UTF-8" name="InsertData" role="form">
      <div><input type="text" placeholder="Enter Name here" name="name"></div>
      <div><input type="text" placeholder="Author" name="author"></div>
      <div><input type="submit" value="Insert"></div>     
</form>

在你的路线中:

在这里,您将调用您的表单操作,例如&#34; backend&#34;

Route::get('backend','Controller@InsertData');

在您的控制器中

控制器使用eloquent来保存数据库中的内容

    public function updateSocial() { 
        $new_blog = Input::except('_token'); 
        $validation = Validator::make($new_blog, Blog::$new_blog); 
        if ($validation->passes()) {
            $new_blog = new Blog;
                $new_blog->name = Input::get('name');
                $new_blog->author= Input::get('author');
                $new_blog->save();
                Session::flash('insert', 'success'); 
                return Redirect::to('backend'); 
        } else { 
            return Redirect::to('backend')->withInput()->withErrors($validation); 
        } 
    }

在您的模型中

将绘制变量,这是他们的验证

public static $new_blog = array(
        'name' =>  'required',
        'author' =>  'required',
        );  

我希望能帮到你

答案 1 :(得分:0)

在@Jose Cerejo的回答中扩展anwser:

{{ csrf_field() }}添加到表单。

您对路线的处理与以下内容非常相似:

$router->resource('/', TestController::class);

查看文档here

修改

您可以使用Laravel Collective执行以下操作:

{!! Form::open(['action' => 'TestController@store']) !!}
<div>
    {!! Form::text('name', null, [ 'placeholder' => 'Enter Name here' ]) !!}
</div>
<div>
    {!! Form::text('author', null, [ 'placeholder' => 'Author' ]) !!}
</div>
<div>
    {!! Form::submit('Insert') !!}
</div>
{!! Form::close() !!}

它为您创建带有必要隐藏字段的表单(如crsf标记和更新PUT请求的_method字段)