小编Tam*_*mam的帖子

Maatwebsite Excel 3.1:导入时如何跳过重复数据?

我在 Laravel Maat 网站 Excel 3.1 上使用 Excel 导入数据。但是当数据库中有相同的数据(主键)时,就会出现错误消息

完整性约束违规:1062 密钥“PRIMARY”的重复条目“188281”

我试图理解 Maat 网站的文档,但仍然失败

public function storeData(Request $request)
        {
            //VALIDASI
            $this->validate($request, [
                'file' => 'required|mimes:xls,xlsx'
            ]);
        if ($request->hasFile('file')) {
            $file = $request->file('file');

            // dd($file); //GET FILE;
            Excel::import(new MahasiswaImport, $file); //IMPORT FILE
            return redirect('/mahasiswa')->with(['status' => 'Upload success']);
        }
        return redirect('/mahasiswa')->with(['error' => 'Please choose file before']);
    }



<?php

namespace App\Imports;

use App\Mahasiswa;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\Importable;

class MahasiswaImport implements ToModel, WithHeadingRow, WithChunkReading, ShouldQueue

{

  use Importable; …
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-5 laravel-excel maatwebsite-excel

5
推荐指数
1
解决办法
1万
查看次数

如何在 Gin Gonic (Golang) 中渲染 HTML 模板?

我正在尝试使用 Gin Gonic 在 Golang 上创建 HTML 模板。但是在渲染我制作的用于生成 Web 视图的模板时出现问题(结果为空白)。我的代码有问题吗?我尝试阅读 gin gonic 文档,但它无法解决我的问题。

/workspace
|- main.go
|-web
  |-assets
  |-base
     | - header.html
     | - footer.html

  |-pages
    |-about.html
Run Code Online (Sandbox Code Playgroud)

这是示例主文件

import (
    "log"
    "net/http"

    "github.com/getsentry/sentry-go"
    "github.com/gin-gonic/gin"
    "html/template"
)


func main() {
    router := gin.Default()
    html := template.Must(template.ParseFiles("web/base/footer.html", "web/base/header.html"))
    router.SetHTMLTemplate(html)
    router.LoadHTMLGlob("web/pages/*")

    router.GET("/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "about.html", gin.H{
            "title": "Main website",
        })
    })
    router.Run(":8000")

}
Run Code Online (Sandbox Code Playgroud)

这是我的 header.html 文件

{{ define "Header" }}
<head>
    <title>Example</title>
</head>
{{end}}
Run Code Online (Sandbox Code Playgroud)

我的页脚.html

{{ define "Footer" }}
<script>
    
</script> …
Run Code Online (Sandbox Code Playgroud)

go go-templates go-gin

5
推荐指数
1
解决办法
8906
查看次数