Vehicles Data for Laravel 5.2
This package allows you to work with vehicles makes, models, years and other details in
Laravel 5.2., (*1)
Where the data come from?
The original list was collected from www.fueleconomy.gov. (They offer XML and
CSV files). The list is updated and validated against some industry standards., (*2)
Content
Installation
Composer
Pull this package through Composer (file composer.json
), (*3)
{
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"gerardojbaez/vehicle": "1.*"
}
}
Run this command inside your terminal., (*4)
composer update
Service Provider
Add the package to your application service providers in config/app.php file., (*5)
<?php
'providers' => [
[...]
/**
* Third Party Service Providers...
*/
'Gerardojbaez\Vehicle\VehicleServiceProvider',
]
Config File, Migration and Seeders
Publish package config file, migrations and seeders with the command:, (*6)
php artisan vendor:publish
Then run migrations., (*7)
php artisan migrate
Then the vehicle seeder., (*8)
php artisan db:seed --class VehicleTablesSeeder
Traits and Contracts
When one of your models has make, model, model year and/or a vehicle you can add the required relations with the traits., (*9)
See the following example:, (*10)
<?php
namespace App\Models;
// [...]
use Gerardojbaez\Vehicle\Contracts\HasMake as HasMakeContract;
use Gerardojbaez\Vehicle\Contracts\HasModel as HasModelContract;
use Gerardojbaez\Vehicle\Contracts\HasModelYear as HasModelYearContract;
use Gerardojbaez\Vehicle\Contracts\HasVehicle as HasVehicleContract;
use Gerardojbaez\Vehicle\Traits\HasMake;
use Gerardojbaez\Vehicle\Traits\HasModel;
use Gerardojbaez\Vehicle\Traits\HasModelYear;
use Gerardojbaez\Vehicle\Traits\HasVehicle;
class Vehicle extends Model implements HasMakeContract, HasModelContract, HasModelYearContract, HasVehicleContract
{
use HasMake, HasModel, HasModelYear, HasVehicle;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'make_id',
'model_id',
'year_id',
'vehicle_id',
...
];/**
* Belongs to one model year.
*
* @return mixed
*/public function modelYear(){
// TODO: Implement modelYear() method.
}
You do not need to use all traits if you do not need them. You can use only HasMake
trait when only make is used for example., (*11)
Usage
The CSV file
All data is stored in a CSV file. This is done so it's easy to manage., (*12)
File structure:, (*13)
Make |
Model |
Year |
cylinders |
displacement |
drive |
transmission |
class |
Acura |
ILX |
2017 |
4 |
2.4 |
Front-Wheel Drive |
8-Speed Automated Manual |
Compact Cars |
Update the CSV file
Export to the desired location:, (*14)
php artisan vehicle:export "/path/to/exported/file.csv"
When you have done, run:, (*15)
php artisan vehicle:generate "/path/to/exported/file.csv"
php artisan db:seed --class VehicleTablesSeeder
The vehicle:generate
command will generate individual files containing its own data to be used with seeders., (*16)
Remember to migrate:refresh
if you have prevously seeded the database!, (*17)
Models
This package comes with Gerardojbaez\Vehicle\Models\VehicleMake
,
Gerardojbaez\Vehicle\Models\VehicleModel
,
Gerardojbaez\Vehicle\Models\VehicleModelYear
and
Gerardojbaez\Vehicle\Models\Vehicle
models., (*18)
For more information please take a look at each model., (*19)
Controllers
It's more likely that you will want to retrieve makes, models and years from your frontend (via ajax for example); we have created these basic controllers for you:
Gerardojbaez\Vehicle\Controllers\MakesController
,
Gerardojbaez\Vehicle\Controllers\ModelsController
,
Gerardojbaez\Vehicle\Controllers\ModelYearsController
,
Gerardojbaez\Vehicle\Controllers\VehicleController
. You can use these directly or extends with your own., (*20)
Controllers returns a json reponse containing (if any) the requested data., (*21)
Routes
This is an example. You can structure these routes as you want., (*22)
<?php
// Show make list
Route::get('api/vehicles/makes', [
'uses' => 'Gerardojbaez\Vehicle\Controllers\MakesController@makes',
'as' => 'api.vehicles.makes'
]);
// Show make models list
Route::get('api/vehicles/{make}/models', [
'uses' => 'Gerardojbaez\Vehicle\Controllers\ModelsController@models',
'as' => 'api.vehicles.models'
]);
// Show model years list
Route::get('api/vehicles/{make}/{model}/years', [
'uses' => 'Gerardojbaez\Vehicle\Controllers\ModelYearsController@years',
'as' => 'api.vehicles.years'
]);
// Show vehicles list
Route::get('api/vehicles/{make}/{model}/{year}/vehicles', [
'uses' => 'Gerardojbaez\Vehicle\Controllers\VehiclesController@vehicles',
'as' => 'api.vehicles.vehicles'
]);
// Show vehicle details
Route::get('api/vehicles/{vehicle}/vehicle', [
'uses' => 'Gerardojbaez\Vehicle\Controllers\VehiclesController@vehicle',
'as' => 'api.vehicles.vehicle'
]);
License
This package is free software distributed under the terms of the MIT license., (*23)