【问题标题】:Controller returns page instead of pushing content in the database Laravel控制器返回页面而不是在数据库 Laravel 中推送内容
【发布时间】:2018-11-21 21:48:46
【问题描述】:

我想使用 Laravel 将插入字段中的数据放入 MySQL 数据库。 但是当我发送表单数据时遇到了问题。唯一发生的事情是输入视图正在返回。

我已经创建了一个显示控制器

namespace pianoCRM\Http\Controllers\Shop;

use DB;
use pianoCRM\Http\Controllers\Controller;

class ShopsController extends Controller
{
    // overview alle webwinkels
    public function overview()
    {

        $webshops = DB::table('webshops')->get();

        return view('Shops/ShopsOverview', ['webshops' => $webshops]);

    }
    // Laat input boxen zien
    public function inputview()
    {

        return view('Shops/Shopsinput');

    }

    // Maak Shop aan
    public function create()
    {

        $Shop = new Shop;
        $Shop->Shopname = Input::get('Shopname');
        $Shop->ShopOwner = Input::get('ShopOwner');
        $Shop->Shopmail = Input::get('Shopmail');
        $Shop->Shopkey = Input::get('Shopkey');
        $Shop->Shopsecret = Input::get('Shopsecret');

        $Shop->save();
        exit();

    }
}

用于从我的输入视图中获取数据

<form methode="post" action="/piano_crm/public/shops/input" >
    <!-- Webwinkel naam -->
    <div class="form-group row">
        <label for="Shopname" class="col-sm-2 col-form-label">Webwinkel naam</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" placeholder="Webwinkel Naam">
        </div>
    </div>
    <!-- Webwinkel eigenaar -->
    <div class="form-group row">
        <label for="ShopOwner" class="col-sm-2 col-form-label">Webwinkel eigenaar</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" placeholder="Webwinkel eigenaar">
        </div>
    </div>
    <!-- Webwinkel email -->
    <div class="form-group row">
        <label for="Shopmail" class="col-sm-2 col-form-label">Webwinkel email</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" placeholder="Webwinkel email">
        </div>
    </div>
    <!-- Shop comsumer key -->
    <div class="form-group row">
        <label for="Shopkey" class="col-sm-2 col-form-label">Woocommerce Comsumer key</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" placeholder="Woocommerce Comsumer key">
        </div>
    </div>
    <!-- Shop comsumer secret -->
    <div class="form-group row">
        <label for="Shopsecret" class="col-sm-2 col-form-label">Woocommerce Comsumer secret</label>
        <div class="col-sm-10">
            <input type="password" class="form-control" placeholder="Woocommerce Comsumer secret">
        </div>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

在我的 web.php 中,我创建了一个应该到 post 控制器的 post 路由

Route::get('shops/input', 'Shop\ShopsController@inputview')->middleware('auth');
// Een overzicht van alle rapporten die uit gedraaid kunnen worden
Route::post('shops/input', array('uses'=>'Shop\ShopsController@create'))->middleware('auth');

【问题讨论】:

  • 删除控制器代码中的exit();函数并在post方法中添加@csrf令牌
  • 缺少 CSRF 令牌?在您的表单中添加@csrf。您可能还想了解 resource controllers 的基本 CRUD 方法。

标签: php laravel


【解决方案1】:

在你调用的函数 create() 上

$Shop = new Shop;

但在文件顶部,您错过了导入放置在“app/models/{ClassName}”文件夹中的模型类。

您需要在文件夹“app/models/”上创建一个名为 Shop 的文件 并通过此代码。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Shop extends Model
{

    protected $table = '{your table name if is name of table on database is different from the name of class plus "s" if the table name is shops you can remove this variable}';
    protected $id = '{your id field if name is different from id if you have id field you can remove this variable}';
    protected $primaryKey = 'your primaryKey if name is different from id if primary key is id you can remove this variable';


}

在 ShopsController 文件的顶部之后

use App\Models\Shop ;

【讨论】:

  • 你需要调用什么型号?
【解决方案2】:

使用辅助函数在表单中添加 csrf 令牌

{!! csrf_field() !!}

否则在verifyCsrfToken.php文件中添加url并在控制器中删除exit()函数

在控制器上导入模型

use App\Shop;

【讨论】:

  • 我已经这样做了,但结果是一样的。 &lt;form methode="POST" action="/piano_crm/public/shops/input" &gt; &lt;input type="hidden" name="_token" value="zfPCsYdwnNp5JVDxoPRFl8nBQc103HNtyCa6GfVs"&gt;
  • 我将它作为 _GET 而不是 POST 取回。它正在返回public/shops/input?_token=zfPCsYdwnNp5JVDxoPRFl8nBQc103HNtyCa6GfVs
猜你喜欢
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 2021-06-02
  • 2012-01-16
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
相关资源
最近更新 更多