How can I create Laravel Model dynamically or without php artisan command?
Alongside creating the model dynamically, how to crate the table and the rows for it?
I cannot be able to find the exact solution anywhere!
Thanks!
Related
On a part of a website I am making, I want to have a table for the website's information (About Us Page). To do this, I need to have a table that can only have one row. How can I limit the Laravel migration for this?
As far as I understand you want to have a database table with only and exactly one row, and you want to make that limitation with a Laravel Migration? As far as I'm aware there is no such limitation in Laravel, but if you are just trying to populate the table with one row you could set up a seeder:
php artisan make:seeder UserSeeder
and then run the seeder with the following command:
php artisan db:seed
You can find all the information in the docs here: https://laravel.com/docs/9.x/seeding
I'm getting started with PHP and I wanted to know if there is a way that you can extract data from the DB in a model.
If I have a Users table in my DB, I'm looking for something like Users.all in rails that will use the model to extract all the entires in the Database.
Does PHP offer that type of functionality?
The closest to Rails is Laravel. Laravel's database layer Eloquent was already mentioned in the comment. Eloquent: Getting Started
If you go for an ORM instead of Eloquent's active record pattern Doctrine is the most popular project. Doctrine Website
Pure php doesn't provide an ORM out of the box ( I suppose you can install a package or something), but if you are using Laravel (since the tag in your question), you need to create a model (supposing you have a migration for users_table)
php artisan make:model user
then simply 'use' the model in any controller
use App\User
and get all users :
$users = User::all();
I' m developing a plugin for october cms. But I have yet a database with table on mySQL. Now I'm looking for a methods to autogenerate "models" based on the database table.
I' m looking for somethings like the laravel command
php artisan code:models
to work on the plugin.
thanks
I believe what youre looking for is:
php artisan create:model Acme.Blog Post
The create:model command generates the files needed for a new model. The first parameter specifies the author and plugin name. The second parameter specifies the model class name.
reference: https://octobercms.com/docs/console/scaffolding#scaffold-create-model
My question is if it is possible to add all the fields directly to a new model via Eloquent.
I guess it would be something like
php artisan make:model MyModel --fields=?
However, I can't find anything related with that. Anyway, I have to generate a lot of model and any trick would be really appreciated.
Thanks in advance
If you mean table's column by fields then:
Firstly you don't need to define fields in modal. I mean in Laravel no need to define fields while creating model. Besides, model automatically work with your database table's columns as its property.
So, now you may want to define columns while creating migration, not while creating model. There is library to serve this demand named as Generator(https://github.com/laracasts/Laravel-5-Generators-Extended) maintained by Laracasts.
Using this generator you can generate migration file to create table in DB specifying their column names and their type also. Here is a example from their Github repo, how you can do this:
php artisan make:migration:schema create_users_table --schema="username:string, email:string:unique"
You can checkout their documentation for more information. Best of luck.
It's not possible with make:model or make:migrations commands, but you can create your own console command and add this feature by yourself.
Also, take a look at source code of make:model and make:migration commands to get some ideas on how to do that.
it looks like only built in options are --migration and -m to include a migration with the model generation. L5.3 Docs
There does look like there is a package for L5.0, which looks like it would work in 5.*+. It is put out by Laracasts:
https://github.com/laracasts/Laravel-5-Generators-Extended
It also looks like you can make a custom solution as well:
https://laracasts.com/discuss/channels/tips/l5-artisan-command-makemodel
Hope that helps!
No options while creating a model,
This is my terminal output (laravel 5.3) while i check,
You don't need to mention fields while creating model.
Ex:- based on the rules you should keep the names as like below,
model name as User
table name as users
then the model automatically handle everything, you don't need to mention the table/fields name.
I was looking for the same thing myself, as I used to work like that in previous frameworks, but could not find it, or at least not as I wanted it, so I did my thing. You can check it out if you like:
https://github.com/Triun/laravel-model-base
It will read your database, and create the laravel eloquent models for you.
It is meant to be very flexible, so the configuration may be a little complex, and I guess that I didnt' catch up with the documentation, but you can ask me if you don't know how to make it do what you want.
Basically it has 4 customization levels:
By out of the box modificators, configurable by the config files.
Including interfaces and traits to your auto-generated models.
Create your own modificators. Classes where you receive the model skeleton before it is saved, so you can add or remove properties, methods, etc.
Generate the model base, but edit yourself the final model.
And, of course, suggestions and contributions are more than welcome.
To make it clear let's make classic example - User and Post.
Creating db schema in Symfony2 is clean and simple:
we create entities Post and User
additionaly we can simply add columns/indexes to each.
then just add value with OneToMany annotation in User and ManyToOne in Post
..well, that's it. Now if we run db:schema:update --force and we can get what we want - database schema and simple adding another rows in database.
What about Laravel4? So far only solution I found:
create/generate Post and User models
declare in each model which table it refers to
create migrations and in Post migration add foregin key to user_id column
run migration
add in each model methods in which we refer to the other model (hasMany, belongsTo .. )
As I wrote it, it doesn't seem so complicated, but it's not so concentrated in Laravel as it is in Symfony. I'm kinda lazy person and I really enjoy the process in Symfony, while in Laravel it is a little bit too diffuse. Is there any simpler ( lazier :P ) way to do this in Laravel? Something like creating schema based on Model?
The question makes sense but unfortunately there isn't such functionality on Laravel at the moment.
As opposed to running migrations from your models (symfony) you must create the migrations first, the you can use the models to seed database tables if they have foreign keys.
I use the Jeffrey Way Generators https://github.com/JeffreyWay/Laravel-4-Generators
to speed up the process so for example if I have a users table and a profile table (many to many) then I would perform these tasks on command line:
php artisan generate:migration create_users_table --fields="username:string, password:string, email:string"
php artisan generate:migration create_profiles_table --fields="name:string, lastname:string, phone:string"
php artisan migrate
php artisan generate:pivot users profiles
php artisan migrate
Then you can create your models (you can also generate an entire CRUD resource or Scaffold)
php artisan generate:model user
php artisan generate:model profile
Then in your User Model
public function profile()
{
return $this->belongsToMany('Profile');
}
In your Profile Model
public function user()
{
return $this->belongsToMany('User');
}
Yes, there are some plugins / commands that speed up the development.
For example Jeffrey Way's Laravel-4-Generators