I am using an existing project of Laravel and this existing project already has models, here is an example of one:
<?php
/**
* Created by Reliese Model.
* Date: Fri, 20 Apr 2018 08:56:36 +0000.
*/
namespace App\Models;
use Reliese\Database\Eloquent\Model as Eloquent;
/**
* Class PdTcountry
*
* @property int $pkcountry
* @property string $country_code
* @property string $country_name
* @property string $country_localName
* @property string $country_webCode
* @property string $country_region
* @property string $country_continent
* @property float $country_latitude
* @property float $country_longitude
* @property string $country_surfaceArea
* @property string $country_population
* @property string $country_postcodeexpression
* @property \Carbon\Carbon $create_at
* @property \Carbon\Carbon $update_at
*
* @property \Illuminate\Database\Eloquent\Collection $pd_tregions
*
* @package App\Models
*/
class PdTcountry extends Eloquent
{
protected $table = 'pd_tcountry';
protected $primaryKey = 'pkcountry';
public $timestamps = false;
protected $casts = [
'country_latitude' => 'float',
'country_longitude' => 'float'
];
protected $dates = [
'create_at',
'update_at'
];
protected $fillable = [
'country_code',
'country_name',
'country_localName',
'country_webCode',
'country_region',
'country_continent',
'country_latitude',
'country_longitude',
'country_surfaceArea',
'country_population',
'country_postcodeexpression',
'create_at',
'update_at'
];
public function pd_tregions()
{
return $this->hasMany(\App\Models\PdTregion::class, 'fkcountry');
}
}
My question is, with this Model is there away via php artisan to create a database table from the model? If there is a php artisan command to do it for all my models that would be super.
In my database folder I have these, but I don't know what they do.
If you are looking to generate these tables automagically, then no, Laravel doesn't really have a way to do that. In theory, you could write your own command to generate migration files for each of your models, but it will still require you to provide all the column names, data types, etc. anyways. Take a look at Laravel's make:migration
command for instance. It just uses stub files and replaces key words when generating (vendor/laravel/framework/src/Illuminate/Database/Migrations/MigrationCreator.php
). If you have a ton of models that need database tables, then maybe this would be a good approach for you to dive into.
If not, however, you're probably best off generating a migration using the standard command and just supply it with the --create
tag. Afterwards, you would just have to define your table in your model (or use the naming convention so it finds it automatically, see: https://laravel.com/docs/5.6/eloquent#defining-models for more info on the naming conventions).
Example:
php artisan make:migration create_my_model_table --create=my_model_table_name
If you don't use the naming convention, add your table name to your model:
class PdTcountry extends Eloquent {
protected $table = "my_model_table_name"
...
}