2021-08-30

Is There A Way To Attach Relationship(One-One, One-Many, Many-Many) When Importing Data From Excel In Laravel?

Is there a way to attach relationship in laravel directly in just one function?

For example I had Three Table in Database: users(first table), user_role(relationship table), and roles(second table)

In controller, AdminController.php I had this:

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();
    }

and in import, UserImport.php I had this:

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],
        ]);
    }
}

And for models, in User.php I had this:

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();
}

in Role.php I had this:

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

So, I'm wondering where to place this code: ->roles()->attach(Role::where('name', 'Student')->first()); I usually use this code to attach relationship after making new data one-per-one using CRUD, but I don't know how to place or code to attach relationship to imported data from excel with only one function(import_student). If no one know, maybe I would create CRUD for relationship table.



from Recent Questions - Stack Overflow https://ift.tt/38jl6Dc
https://ift.tt/eA8V8J

No comments:

Post a Comment