小编LDU*_*BBS的帖子

导入文件时如何跳过第一行

我正在尝试使用3.1版导入5.7版中的.xlsx文件。我想要实现的是跳过文件的第一行以避免在我的数据库中导入列标题。LaravelMaatwebsite-excel

我尝试使用版本 2 语法,调用该skip()方法。

public function voter_import(Request $request)
{
    if (empty($request->file('file')->getRealPath())) 
    {
        return back()->with('success','No file selected');
    }
    else 
    {
        Excel::import(new VotersImport, $request->file('file'))->skip(1);
        return response('Import Successful, Please Refresh Page');
    }
}

class VotersImport implements ToModel
{
public function model(array $row)
   {
    return new Voter([
      'fname'          =>  $row[0],
      'lname'          =>  $row[1],
      'phone'          =>  $row[2],
      'gender'         =>  $row[3],
      'state'          =>  $row[4],
      'occupation'     =>  $row[5],
      'address'        =>  $row[6],
      'vin'            =>  $row[7],
      'dob'            =>  $row[8],
      'campaign_id'    =>  $row[9],
    ]); …
Run Code Online (Sandbox Code Playgroud)

laravel maatwebsite-excel

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

如何在 VueJS 中动态创建输入字段

我正在尝试动态添加或删除输入字段。

我从这里得到了一个简单的解决方案https://smarttutorials.net/dynamically-add-or-remove-input-textbox-using-vuejs/,它有效。然而,在数据库中保存输入值会停止它的功能。

组件代码:

<div class="form-group" v-for="(input,k) in inputs" :key="k">
  <input type="text" class="form-control" v-model="input.name" />
  <input type="text" class="form-control" v-model="input.party" />
  <span>
    <i
      class="fas fa-minus-circle"
      @click="remove(k)"
      v-show="k || ( !k && inputs.length > 1)"
    ></i>
    <i
      class="fas fa-plus-circle"
      @click="add(k)"
      v-show="k == inputs.length-1"
    ></i>
  </span>
</div>
<button @click="addCandidate">
  Submit
</button>

<script>
  export default {
    data() {
      return {
        inputs: [
          {
            name: "",
            party: ""
          }
        ]
      };
    },
    methods: {
      add(index) {
        this.inputs.push({ name: "", party: "" });
      },
      remove(index) …
Run Code Online (Sandbox Code Playgroud)

javascript json laravel vue.js axios

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

将额外的模型附加到 BelongstoMany 关系

我有两个带有数据透视表的表(用户和饮料),用户与配置文件表有 hasOne 关系,有没有办法将配置文件表附加到数据透视表并获取所有数据。

用户

id | name | email
Run Code Online (Sandbox Code Playgroud)

简介

id | user_id | picture
Run Code Online (Sandbox Code Playgroud)

餐桌饮料

id | name | price
Run Code Online (Sandbox Code Playgroud)

数据透视表user_drinks

id | user_id | drink_id | quantity | price | status
Run Code Online (Sandbox Code Playgroud)

饮品型号

public function users()
{
   return $this->belongsToMany('App\User', 'user_drinks', 'drink_id', 'user_id')->withPivot('price', 'quantity')->withTimestamps();
}
Run Code Online (Sandbox Code Playgroud)

用户模型

public function drinks()
{
   return $this->belongsToMany('App\Drink', 'user_drinks', 'drink_id', 'user_id')->withPivot('price', 'quantity')->withTimestamps();
}

public function profile() {
         return $this->hasOne('App\Profile');
     }
Run Code Online (Sandbox Code Playgroud)

PS:我不想用原始 sql 查询写这个,这让我发疯。

php laravel eloquent

2
推荐指数
1
解决办法
58
查看次数

标签 统计

laravel ×3

axios ×1

eloquent ×1

javascript ×1

json ×1

maatwebsite-excel ×1

php ×1

vue.js ×1