【问题标题】:Is There A Way To Attach Relationship(One-One, One-Many, Many-Many) When Importing Data From Excel In Laravel?在 Laravel 中从 Excel 导入数据时,有没有办法附加关系(一对一、一对多、多对多)?
【发布时间】:2021-10-28 11:32:46
【问题描述】:

有没有办法在 laravel 中直接在一个函数中附加关系?

例如,我在数据库中有三个表:用户(第一个表)、user_role(关系表)和角色(第二个表)

在控制器中,AdminController.php 我有这个:

public function import_student(Request $request) 
    {
    $user = Auth::user();
    $this->validate($request, [
        'file' => 'required|mimes:csv,xls,xlsx'
    ]);
    $file = $request->file('file');
    $nama_file = rand().$file->getClientOriginalName();
    $file->move('file_student',$nama_file);
    Excel::import(new UserImport, public_path('/file_student/'.$nama_file));
    Session::flash('sukses','Data Siswa Berhasil Diimport!');
    return redirect()->back();
    }

在导入时,UserImport.php 我有这个:

use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithConditionalSheets;

class UserImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    use WithConditionalSheets;
    public function conditionalSheets(): array
    {
        return [
            'Worksheet 1' => new FirstSheetImport(),
            'Worksheet 2' => new SecondSheetImport(),
            'Worksheet 3' => new ThirdSheetImport(),
        ];
    }
    public function model(array $row)
    {
        return new User([
        'nisn' => $row[1],
        'nip' => $row[2],
        'name' => $row[3],
        'username' => $row[4],
        'email' => $row[5],
        'kelas' => $row[6],
        'jabatan' => $row[7],
        'tempat_lahir' => $row[8],
        'tgl_lahir' => $row[9],
        'bulan_lahir' => $row[10],
        'tahun_lahir' => $row[11],
        'jenis_kelamin' => $row[12],
        'agama' => $row[13],
        'tahun_masuk' => $row[14],
        'no_telp' => $row[15],
        'password' => $row[16],
        ]);
    }
}

对于模型,在 User.php 中我有这个:

protected $fillable = [
    'nisn',
    'nip',
    'name',
    'username',
    'email',
    'kelas',
    'jabatan',
    'tempat_lahir',
    'tgl_lahir',
    'bulan_lahir',
    'tahun_lahir',
    'jenis_kelamin',
    'agama',
    'tahun_masuk',
    'no_telp',
    'avatar',
    'password',
];
protected $table = 'users';
/*
 * Role & User Relationship
 *
 */
public function roles()
{
    return $this
        ->belongsToMany('App\Role')
        ->withTimestamps();
}

在 Role.php 我有这个:

public function users()
    {
        return $this
            ->belongsToMany('App\User')
            ->withTimestamps();
    }

所以,我想知道在哪里放置此代码:->roles()->attach(Role::where('name', 'Student')->first()); 我通常在使用 CRUD 一对一制作新数据后使用此代码附加关系,但我不知道如何放置或编码仅使用一个函数(import_student)将关系附加到从 excel 导入的数据。如果没有人知道,也许我会为关系表创建 CRUD。

【问题讨论】:

    标签: php excel laravel import relationship


    【解决方案1】:

    UserImport 类中,你可以这样做而不是你所拥有的:

    public function model(array $row){
    
        $user = new User([
        'nisn' => $row[1],
        'nip' => $row[2],
        'name' => $row[3],
        'username' => $row[4],
        'email' => $row[5],
        'kelas' => $row[6],
        'jabatan' => $row[7],
        'tempat_lahir' => $row[8],
        'tgl_lahir' => $row[9],
        'bulan_lahir' => $row[10],
        'tahun_lahir' => $row[11],
        'jenis_kelamin' => $row[12],
        'agama' => $row[13],
        'tahun_masuk' => $row[14],
        'no_telp' => $row[15],
        'password' => $row[16],
        ]);
        return $user->roles()->attach(Role::where('name', 'Student')->first());
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 2019-01-03
      • 2018-06-15
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 2018-03-16
      相关资源
      最近更新 更多