Laravel create Model and Migration dynamically with dynamic table columns - laravel

Working on a task where we need to create Model and Migration files programmatically. Also in the Model there will be some predefined functions. These functions will be same for all models. The functions are just relations with other Models. I have searched for a few options and found that Laravel has Stubs and we can create custom stubs which are great but don't think there is any option to pass params to the stub file when generating it.
In this case, we want to create a model and migration with dynamic columns. May be there is no easy way to do it but in case anyone has done it already, can you please provide me a hint of how you did it.
Trying this laravel package
https://github.com/laravel-shift/blueprint
.It can generate models, migrations, controllers from Yaml file. May be we can create a yaml file dynamically and then publish it.
Thanks

you can write your own command.
for example i wrote a command for generate repository pattern in my projects
php artisan make:repository repoName

You can publish stubs
php artisan stub:publish
Create your custom stub
<?php
// stubs/controller.custom.stub
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
/**
* Hello from the custom controller stub
*/
class {{ class }}
{
//
}
And call it
php artisan make:controller --type=custom MyController
Watch --type option

Related

How to create modular in laravel?

I want to create a modular to create a controller and a model.
Of course, the model is pre-created.
php artisan module:make-controller Admin\ReportController Report --model=Report
I see this message
The "--model" option does not exist.
I assume you used nWidart Modules, so the answer is you can not use --model option because nWidart Modules doesn't support that option/parameter. You need run another command to create a model, an example:
php artisan module:make-model Report Admin
Report is model name, and Admin is module name
check it out for more module commands

PhpStorm autocomplete with \Nwidart\Modules

The problem would be that I use PhpStorm IDE. The Laravel project is managed by nwidart/laravel-modules.
However, PhpStorm does not handle basic Laravel functions. For example: findorfail(), for own models:
Method 'findorfail' not found in \Modules\Companies\Models\Companies_adresses
I did the following to enable autocomplete:
install https://github.com/barryvdh/laravel-ide-helper#automatic-phpdoc-generation-for-laravel-facades
Enable PhpStorm plugins:
https://plugins.jetbrains.com/plugin/13441-laravel-idea
https://plugins.jetbrains.com/plugin/7532-laravel
It is very confusing because it does not give tips correctly. It does not list database columns, etc... PHPDoc still came to mind, but I don't know how to get started!
You have no ideas? I feel completely lost :(
I set up the Laravel IDE Helper but I had to add the following into all my models...or you could add to a new Model class that you then extend to all of your models. Either way, this will get PHPStorm to read the facade methods like that correctly:
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* Class Employee
*
* #mixin Builder
*/
class Employee extends Model
{
// blah blah blah
}
Make sure the PHPDoc bumps up against your model. The key bit is the #mixin line which gives PHP Storm a connection to all the facade methods for your model. You will still need the IDE Helper as well. But with this PHPStorm will suggest as you type and allow you to control-click to view the method itself from elsewhere in your project.
You also can generate advanced ide helper code with Laravel Idea plugin. main menu > Code > Laravel Idea code generation > Generate Eloquent Helper code.

Laravel 5.3 DatabaseMigrations are destructive

Laravel 5.3 with mysql, PHPUnit 5.7.4
When I create a test in PHPUnit with use DatabaseMigrations;, it destroys the data that it queries.
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ThingsTest extends TestCase
{
use DatabaseMigrations;
/** #test */
public function there_are_more_than_1000_things()
{
$things = App\Thing::all();
$this->assertGreaterThan(1000, count($things));
}
Before I run phpunit, there are lots of things. After running it, mysql says ERROR 1146 (42S02): Table 'database.things' doesn't exist
Any ideas how to stop this?
DatabaseMigrations is a trait and it execs:
before test 'php artisan migrate' // creates your tables, but doesn't seed them
after test 'php artisan migrate:rollback' // remove tables
So, 1st - make sure you're using another database for testing.
2nd - seed with fake data your tables before testing your Things class.
Alternative:
use DatabaseTransactions trait instead of DatabaseMigrations.
In that case each test activity will be wrapped in a database transaction. After test all your changes will be dropped by transaction's rollback automatically.
You can be using a test database with PHPUnit within your Laravel application. Right now your tests are using your main database and will modify existing information.
Please see https://stackoverflow.com/a/35228697/823549 for how to do this.

laravel id-helpeClass 'App\Models\Eloquent' not found

i'm just started to use laravel ide-helper in phpstorm and wanted to document my models.
I've run the command php artisan ide-helper:models but comes up with the error:
'App\Models\Eloquent' not found
After reading it looks as if the error lies in what my models extend - my model class looks like this:
<?php namespace App\Models;
use \Illuminate\Database\Eloquent\Model;
class Article extends Model { }
This is the standard set in the Laravel docs and created by the generators.
Is there a way to resolve this issue so I can generate the docs with the helper or do I leave as is.
Bit confused
Thanks

Class autoloading in Laravel 4.1

I am trying to use Laravel and have been following the official Laravel Eloquent documentation and multiple tutorials at credible sources tuts plus
I created a model inside app/models called Stack with a table in the database called stacks with a primary key column called id, as corresponding to Laravels defaults.
<?php (Stack.php)
class Stack extends Eloquent
{
}
$stacks = Stack::all();
However when I run this model I get the following error message.
Fatal error: Class 'Eloquent' not found in C:\www\laravelproject\app\models\Stack.php on line 4
Including the official documentation and the reputable tutorials, I have also watched 2 youtube tutorials and it seems like there is no additional autoloading/including/requiring required to be declared in any new defined model's, so I am assuming something else here maybe wrong.
Do I have to manually find all classes I must autoload? If so, why is this not written in the official documentation?
I downloaded the latest laravel.phar file directly from laravel and used a .bat file to call it. (Not via composer)
Some things I have checked/tried to fix the problem.
Eloquent directory does exist at vendor\laravel\framework\src\Illuminate\Database\Eloquent
Eloquent alias set in app/config/app.php. Default 'Eloquent' => 'Illuminate\Database\Eloquent\Model'
Directly extending class like \Illuminate\Database\Eloquent\Model, error message the same but with \Illuminate\Database\Eloquent\Model instead of just Eloquent
Tried to directly extend through all variations by navigating down the entire Laravel directory structure \vendor\laravel\framework\src\Illuminate\Database\Eloquent, then \laravel\framework\src\Illuminate\Database\Eloquent etc... etc...
Bit the bullet and decided to try the second official method, I installed composer and ran the command composer create-project laravel/laravel --prefer-dist, the command screen alerted me it was downloading files which was then all successful at 100%, then alerted me that a generated application key was set successfully. I then navigate to the new directory model/User.php and receive the exact same error message as when I did it with the previuos method(laravel.phar direct download).
Thanks in advance.
Make sure you are accessing the application from the correct 'entrance'.
Thus, accessing it from app/public/index.php.
The app/public/index.php file loads the autoloader.
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/../bootstrap/autoload.php';
It's possible your is namespacing. Try adding the backslash before the class being extended.
class Stack extends \Eloquent
{
}
Make sure your are setting the Eloquent alias in the app config. (app/config/app.php)
Alternatively use the class directly. I believe it's: Illuminate\Database\Eloquent\Model
class Stack extends \Illuminate\Database\Eloquent\Model {}

Resources