Laravel - How to import excel file file and save into a table with foreign keys - laravel

I am using Laravel-5.8 for a web application. Also, I am using maatwebsite-3.1 to import excel file.
Model: HrEmployee
protected $table = 'hr_employees';
protected $fillable = [
'employee_code',
'user_id',
'address',
'company_id',
'email',
'line_manager_id',
'employee_designation_id',
'employee_job_title_id',
'employee_status_id',
'employee_type_id',
'employement_type_id',
'employment_date',
'first_name',
'last_name',
'is_hod'
];
public function user()
{
return $this->belongsTo('App\User');
}
public function parent()
{
return $this->belongsTo('HrEmployee', 'line_manager_id');
}
public function children()
{
return $this->hasMany('HrEmployee', 'ine_manager_id');
}
public function company()
{
return $this->belongsTo('App\Models\Organization\OrgCompany','company_id');
}
public function designation()
{
return $this->belongsTo('App\Models\Hr\HrDesignation','employee_designation_id');
}
public function jobtitle()
{
return $this->belongsTo('App\Models\Hr\HrJobTitle','employee_job_title_id');
}
public function employeestatus()
{
return $this->belongsTo('App\Models\Hr\HrEmployeeStatus','employee_status_id');
}
public function employeetype()
{
return $this->belongsTo('App\Models\Hr\HrEmployeeType','employee_type_id');
}
public function employementtype()
{
return $this->belongsTo('App\Models\Hr\HrEmployementType','employement_type_id');
}
public function department()
{
return $this->belongsTo('App\Models\Organization\OrgDepartment','department_id');
}
I have already configured the Maatwebsite Excel Package
app/Imports/EmployeesImport.php
namespace App\Imports;
use App\User;
use App\HrEmployee;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class EmployeesImport implements ToModel, WithHeadingRow
{
public function model(array $row)
{
return new HrEmployee([
'employee_code' => $row['employee_code'],
'email' => $row['email'],
'first_name' => $row['first_name'],
'last_name' => $row['last_name'],
'line_manager_id' => $row['line_manager_id'],
'employee_designation_id' => $row['employee_designation_id'],
'employee_job_title_id' => $row['employee_job_title_id'],
]);
}
}
Controller
class HrEmployeesController extends Controller
{
public function importExportView()
{
return view('import');
}
public function import()
{
Excel::import(new EmployeesImport,request()->file('file'));
return back();
}
}
How do I re-write my code to accommodate the foreign keys (line_manager_id,employee_designation_id,employee_job_title_id), so that it will map the keys to the name.
For example, if designation_name is entered to the excel sheet it should map it to employee_designation_id.
Thank you

Just find the foreign key ID in the EmployeesImport
$row['employee_designation_id'] = HrDesignation::where("name", "like", "%".$row['designation']."%");
$row['line_manager_id'] = HrEmployee::where("first_name", "like", "%".$row['line_manager']."%");
$row['employee_job_title_id'] = HrJobTitle::where("name", "like", "%".$row['job_title']."%");
or the full code
namespace App\Imports;
use App\User;
use App\HrEmployee;
use App\Hr\HrDesignation;
use App\Hr\HrJobTitle;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class EmployeesImport implements ToModel, WithHeadingRow
{
public function model(array $row)
{
$row['employee_designation_id'] = HrDesignation::where("name", "like", "%".$row['designation']."%");
$row['line_manager_id'] = HrEmployee::where("first_name", "like", "%".$row['line_manager']."%");
$row['employee_job_title_id'] = HrJobTitle::where("name", "like", "%".$row['job_title']."%");
return new HrEmployee([
'employee_code' => $row['employee_code'],
'email' => $row['email'],
'first_name' => $row['first_name'],
'last_name' => $row['last_name'],
'line_manager_id' => $row['line_manager_id'],
'employee_designation_id' => $row['employee_designation_id'],
'employee_job_title_id' => $row['employee_job_title_id'],
]);
}
}

namespace App\Imports;
use App\User;
use App\HrEmployee;
use App\Hr\HrDesignation;
use App\Hr\HrJobTitle;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class EmployeesImport implements ToModel, WithHeadingRow
{
public function model(array $row)
{
$employee_designation = HrDesignation::where("name", "like", "%".$row['designation']."%")->first();
$line_manager = HrEmployee::where("first_name", "like", "%".$row['line_manager']."%")->first();
$employee_job_title = HrJobTitle::where("name", "like", "%".$row['job_title']."%")->first();
$row['employee_designation_id'] = $employee_designation->id;
$row['line_manager_id'] = $line_manager->id;
$row['employee_job_title_id'] = $employee_job_title->id;
return new HrEmployee([
'employee_code' => $row['employee_code'],
'email' => $row['email'],
'first_name' => $row['first_name'],
'last_name' => $row['last_name'],
'line_manager_id' => $row['line_manager_id'],
'employee_designation_id' => $row['employee_designation_id'],
'employee_job_title_id' => $row['employee_job_title_id'],
]);
}
}

Related

Relationship between pivot tables - return resource do not found using pivot or belongsToMany

good afternoon!
I would like some help from them regarding the scenario below:
I have 3 tables, 1 is the Moviment which is the main one, the 2 is the cart_moviments which is the secondary one which when I enter the movement data, it saves some data in cart_moviments and I have the 3 table called vehicles, which I have all the vehicle information, all of them have a foreignId, just to relate them.
So on my return from the resource, I have Moviment->cartMoviment, and I want to access it from cartMoviment ->vehicles, which I can't, because it doesn't relate.
My code Store:
public function store(StoreMovimentRequest $request, StoreCartMovimentRequest $requestCart, StoreDocumentRequest $requestDocument)
{
$this->authorize('create', Moviment::class);
$moviment = Moviment::create($request->validated());
$moviment->cartMoviment()->create($request->validated());
foreach(array($request['key_number']) as $notasFiscais) {
foreach($notasFiscais as $notas) {
$moviment->document()->create($notas);
}
}
return new MovimentResource($moviment);
}
My CartMoviment MOdel:
class CartMoviment extends Model
{
use HasApiTokens, HasFactory, Notifiable, UserTenant;
protected $fillable = ['type', 'vehicle_cart_id'];
// protected $table = 'cart_moviments';
public function Moviment() {
return $this->belongsTo(Moviment::class);
}
public function Vehicle() {
return $this->belongsTo(Vehicle::class, 'vehicle_cart_id');
}
My Moviment Model:
class Moviment extends Model
{
use HasApiTokens, HasFactory, Notifiable, UserTenant;
protected $fillable = [
'document_type_id', 'people_id', 'company_id', 'vehicle_id', 'department_id'
];
public function Vehicle() {
return $this->belongsTo(Vehicle::class);
}
public function cartMoviment() {
return $this->belongsToMany(Moviment::class, cartMoviment::class);
}
`
My resource Array:
public function toArray($request)
{
return [
'id' => $this->id,
// 'count' => $this->count(),
'type' => $this->type,
'department_id' => $this->department_id,
'person_id' => $this->people_id,
'person_name' => $this->person->name,
'vehicle_id' => $this->vehicle_id,
'vehicle_board' => $this->vehicle->board,
'vehicle_status' => $this->vehicle->status,
'vehicle_manufacturer' => $this->vehicle->vehicleModel->name,
'vehicle_type' => $this->vehicle->vehicleType->type,
'cart_moviment' => $this->cartMoviment,
My return resource
"cart_moviment": [
],
"documents": [
],
`
I need relationship , thanks you very match.

Laravel - dd() is not working in Maatwebsites import

I have Maaetwebsites-3.1 in Laravel-5.8 for Excel Import:
use App\Imports\GradesImport;
use Maatwebsite\Excel\Facades\Excel;
public function gradeForm()
{
return view('hr.grades.import_grade_form');
}
public function importGradeLevel(Request $request){
$request->validate([
'file' => 'required|max:10000|mimes:xlsx,xls',
]);
$path1 = $request->file('file')->store('import');
$path=storage_path('app').'/'.$path1;
$import = new GradesImport;
$import->import($path);
dd($import->errors());
Session::flash('success', 'Grade Records Imported Successfully');
return redirect()->route('hr.grades.index');
}
Then this is the Excel Import
Import:
class LevelImport implements ToModel, WithHeadingRow, SkipsOnError, WithValidation, WithBatchInserts
{
use Importable, SkipsErrors;
public function model(array $row)
{
$grade_data = [
'grade_level_code' => $row['grade_code'] ?? '',
'grade_level_name' => $row['grade_name'] ?? '',
];
$grade = Grade::create($grade_data);
}
I observed that it's not seeing the:
dd($import->errors());
Why and How do resolve this?
Thanks

incorrect table name inserting into using Laravel Excel to Collection

I'm attemptng to insert relational data using the Laravel Excel concern ToCollection
use Maatwebsite\Excel\Concerns\ToCollection;
however the query is attempting to insert into the table objective_years and not objective_year
ObjectivesImport.php
namespace App\Imports;
use App\Models\Objective;
use App\Models\ObjectiveYear;
use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Collection;
class ObjectivesImport implements ToCollection
{
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
$objectiveDetail = Objective::create([
'description' => $row[0],
'guidance' => $row[1],
'team_id' => $row[2],
'subject_id' => $row[3],
]);
$yearDetail = ObjectiveYear::create([
'objective_id' => $row[4],
'year_id' => $row[5],
]);
}
}
}
I have two models with a many to many relatinship pivot.
Objective
class Objective extends Model
{
use HasFactory;
protected $fillable = [
'description', 'guidance','subject_id','team_id'
];
public function years ()
{
return $this->belongsToMany('App\Models\Year');
}
}
Year
class Year extends Model
{
use HasFactory;
protected $fillable = [
'name'
];
public function objectives()
{
return $this->belongsToMany('App\Models\Objective')->withTimestamps();
}
}
ObjectiveYear
class ObjectiveYear extends Model
{
use HasFactory;
protected $fillable = [
'objective_id', 'year_id'
];
}
Controller
class ImportController extends Controller
{
public function getImport()
{
return view('import');
}
public function import()
{
Excel::import(new ObjectivesImport, request()->file('csv_file'));
return redirect('/')->with('success', 'All good!');
}
}

How can I test a store function with Laravel and Mockery

I want to test a repository pattern in Laravel 5.6 using PHPUnit and Mockery.
This is my code:
// PackageControllerTest.php
namespace Tests\Feature;
use Tests\TestCase;
use App\Contracts\PackageInterface;
use App\Http\Controllers\PackageController;
use Illuminate\Database\Eloquent\Collection;
class PackageControllerTest extends TestCase
{
protected $mock;
protected $target;
public function setUp()
{
parent::setUp();
parent::initDatabase();
$this->mock = $this->initMock(PackageInterface::class);
$this->target = $this->app->make(PackageController::class);
}
public function testIndex()
{
$expected = new Collection([
['name' => 'Name 1', 'html_url' => 'HTML URL 1'],
['name' => 'Name 2', 'html_url' => 'HTML URL 2'],
['name' => 'Name 3', 'html_url' => 'HTML URL 3'],
]);
$this->mock
->shouldReceive('getAllPackages')
->once()
->andReturn($expected);
$actual = $this->target->index()->packages;
$this->assertEquals($expected, $actual);
}
public function testUpdate()
{
//
}
public function tearDown()
{
parent::resetDatabase();
$this->mock = null;
$this->target = null;
}
}
// PackageControllerTest.php
...
public function index()
{
$packages = $this->package->getAllPackages();
return view('package.index', compact('packages'));
}
public function update($package_id)
{
$package = $this->package->updatePackage($package_id);
return redirect()->back();
}
// PackageRepository.php
...
public function getAllPackages()
{
$packages = $this->package->all();
return $packages;
}
public function updatePackage($package_id)
{
$package = $this->package->find($package_id);
$package->description = $this->request->description;
$package->save();
return $package;
}
The part of "testIndex()" works.
But next, I want to test the part of "testUpdate()".
How can I do?
Please help, thanks.
like this
$this->mock
->shouldReceive('updatePackage')
->with(1)
->once()
->andReturn($expected);
$actual = $this->target->update(1);
$this->assertRedirect('edit page url');
or
use DatabaseTransactions in top of class
$id = DB::table('your_table')->insertGetId(['description' => 'old'])
request()->set('description', 'test');
$actual = $this->target->update(id);
$this->assertDatabaseHas('your_table', [
'id' => $id,
'description' => 'test'
]);

Call to a member function associate() on a non-object

Error Message: http://puu.sh/d4l0F/5b0ac07e68.png
I've even saved the $transportation object before trying to create associations. I've verified that both $transporation, $from and $to are all their respective objects and they are.
I'm sure I'm missing something stupid here but I'm out of ideas.
My code:
class RideBuilder implements RideBuilderInterface
{
public function create(Advance $advance)
{
$ride = new Ride;
if($ride->validate(Input::all())) {
$ride->save();
$to = Location::find(Input::get('dropoffLocation'));
$from = Location::find(Input::get('pickupLocation'));
$transportation = new Transportation;
$transportation->save();
$transportation->transportable()->associate($ride);
$transportation->to()->associate($to);
$transportation->from()->associate($from);
$event = new Event;
$event->start = Input::get('ridePickUpTime');
$event->save();
$event->eventable->save($transportation);
$event->subjectable->save($advance);
}
return $ride;
}
}
Location Model:
class Location extends Elegant
{
protected $table = 'locations';
public $rules = array(
'label' => 'required|min:2',
'street' => 'required',
'city' => 'required',
'state' => 'required',
'type' => 'required',
);
public function advance()
{
return $this->belongsTo('Booksmart\Booking\Advance\Model\Advance');
}
public function locationable()
{
return $this->morphTo();
}
}
Transportation Model:
class Transportation extends Elegant
{
protected $table = 'transportations';
public function event()
{
$this->morphOne('Booksmart\Component\Event\Model\Event');
}
public function start_location()
{
$this->belongsTo('Booksmart\Component\Location\Model\Location', 'start_location');
}
public function end_location()
{
$this->belongsTo('Booksmart\Component\Location\Model\Location', 'end_location');
}
}
I had a similar issue. I made the stupid mistake of not adding the "return" in the relationship method!
Make sure you return the relationship... Obviously this will not work:
public function medicineType()
{
$this->belongsTo('MedicineType', 'id');
}
This is the correct way:
public function medicineType()
{
return $this->belongsTo('MedicineType', 'id');
}
Easy to miss, hard to debug...

Resources