Add column to database via controller in laravel 4.2 - php

Hi so i have a project that is already done but there was a request to add a new column to a specific table i was thinking of a way to add that new column to my table without using migrate or php artisan method to it. i was thinking if it is posible to implement it in the controller. say for example the column name is isOut then values should be false not null. thanks for any advice

You can use the Schema Builder inside a controller as well as in migrations.
Just include use Illuminate\Support\Facades\Schema;
Then run this inside a controller function:
Schema::table('table_name', function($table)
{
$table->boolean('isOut')->default(false);
});
Take care only to run the controller function once, for example by creating a special route to this function with a random string.

Related

Laravel eloquent - Doctrine features equivalent

Coming from a long Symfony-Doctrine background, I have started learning Laravel 8.
One of my first discovery was that migration needed to be manually created after using make:migration (from what I understood thus far) in both Models and Migration.
Symfony, with Doctrine, allowed a bunch of automatisation, and I only needed to create the field or relation from the Model (php annotation or yaml) - before launching doctrine:schema:validate and make:migration
https://symfony.com/doc/current/doctrine.html#migrations-adding-more-fields.
Let 'say I create a Post and Comment entity, with a One-To-Many relationship.
If I define the relation in the php classes
class Comment extends Model
{
/**
* Get the Post owning this comment
*/
public function post()
{
return $this->belongsTo(Post::class);
}
}
Post class
class Post extends Model
{
/**
* Get the comments for the blog post.
*/
public function comments()
{
return $this->hasMany(Comment::class);
}
}
Is possible to generate the migration scripts that would update the database and create these relation ship ? Or will I have to rewrite it twice (once in the PHP class with hasMany/BelongsTo then again in the Migtration file ?
Not having a central file to map/read a model (which linkied php and database) seems weird to me now.
I've only started but the documentation does not seems to mention anything equivalent
Edit : for more clarity : I'm asking if there are equivalent of Generating migration code/script from models (or a central mapping file : yaml or annotation ) without having to write fields to both migration and models ( in $fillable or other fields...)
Edit 2 Closest thing I could find is this
https://github.com/laracasts/Laravel-5-Generators-Extended
Another use case where this is cumbersome : many-to-many migrations
Having to manually write that third middle table from scratch is really something I wish was made automatically.
In laravel framework despite other famous fameworks like symfony or python django, you're responsible for making database migration files. By this way, you are free to customize your database schema and add any database constrains like (unique constrain, relation constrains and etc.). Also you can add any raw sql using \DB::unprepared(); in your migration files.
I can tell you that it is normal to be a little confuse about this flow, because you've used to this kind of automation in other frameworks like I did, but beilieve me, you get to used to this flow.
By the way, there are some packages out there that do this automation (create migration files according to model) for you.
For creating a migration you need to run command
php artisan make:migration migration_name
And for creating a model you need to run command
php artisan make:model table_name
Refer to this link
https://laravel.com/docs/8.x/eloquent#generating-model-classes
By creating a model you create a table, where you can define the columns types and different properties and define the relationship with other tables.
By creating a migration file you can define the table columns and the constraints.

In Laravel project, where do I put code which reads data from database tables, which would be common to multiple controllers?

Background:
I am having my first encounter with any MVC framework, as I am developing a little application with PHP web framework Laravel v6 and MongoDB (with jenssegers moloquent) as database engine. I am following this tutorial series to learn Laravel 6.
Normally when I used to develop such applications with simple php, I would create one file called readFromDb.php and in it I would find/read/select data from all DB tables (collections in Mongodb). Then I would include in on top of every PHP file in which I would need to do some processing on any data from DB.
For example, if I have the following collections
allPaintingsCollection
paintingHistoriesCollection
paintingCategoriesCollection
artGalleriesCollection
paintingArtistsCollection
supervisorArtistsCollection
smPlatformsCollection
nonSmPlatformsCollection
targetSchoolsCollection
I would select all records/documents from them into associative arrays in the readFromDb.php, and then include readFromDb.php on top of every page where I would need to display or do processing on data from DB.
Question:
Now, in Laravel, should I create such a script called readFromDb.php and include it on top of every function in every single controller? In that case, where should I put this readFromDb.php file, and how do I include it in controllers?
Or should I write code to read from relevant DB collection/table in each function in every controller, before using that data from DB?
I am using class and call it as repository to handle all my request through database
First i create an file UserRepository
<?php
namespace App\Repositories;
use App\Model\User;
class UserRepository
{
function getUsers(){
Return User::get();
}
}
Then in Controller, you just need to call the repository in the parent of contruct
<?php
namespace App\Http\Controllers;
use App\Repositories\UserRepository;
class UserController extends Controller
{
protected $user;
function __construct(){
$this->user = new UserRepository();
}
function index(){
$users = $this->user->getUser(); // getUser() is the function inside the repository class
return view('user',compact('users'));
}
}
That's how I do code in laravel, because i dont want to call Laravel Model and do all the store, reads inside Controller and cause messy code
Laravel has Eloquent ORM what presents Object Relation Mapping interface.
In Introduction:
The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.
Basically, same as your readFromDb.php file but it handles with OOP so codebase is very understandable and expandable and strictly related with your code.
When querying with Eloquent you use Models. If your query result has single row you get single Model, if your result has multiple rows you get Eloquent\Collection result set contain your models.
Finally, you mustn't write ORM in Laravel because you already have.
If you think high level abstract accessors; you should explore design patterns. After that you can think to Repository Pattern, Decorator Pattern etc.

How to treat / create models from tables created in Laravel frontend app?

I'm creating an app using laravel. Tables and columns will be created on the frontend applying the schema codes inside the controller. But how can I do to create models without programming each one manually? Any idea to code it?
Controller example (is working fine):
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class teste extends Controller
{
public function CreateTable(Request $tableName)
{
Schema::create($tableName, function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
//return view
}
}
You can use this to create models from your controller.
Artisan::call('make:model ' + $modelPath + '/' + $request->modelName)
Now there are a lot of options to create migrations, controller, resource routes etc. along with the model. You can check them out if you want.
The command will create the model. You can create the model from the table name of course. But it will not follow the naming conventions. Like table names are in plural and model name are in singular form with the first letter in upper case.
If you had used artisan to create the table migration along with model Laravel would've handled some complex namings. Like Category model with categories table, or Software model with software table. That's why I recommend sending the model name separately from your view.
The real problem arises when you plan to build a whole model programmatically along with the fields. Laravel does not allow you to do that. You would have to build a custom command to handle this.
Also, even if you make the fields programmatically, you'd still have to go back to the model to define the relations. You can build your custom command to handle this too.
However, you can go through this question to have some idea. This guy asked for a similar thing like you.
Also, there are some packages that read your table and backtrack it to build your model. Here's an example. Another example here.
Note: if you plan on building your custom command and make it open-source please let me know.

Creating Laravel Model dynamically

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!

Building database schema Laravel4 vs Symfony2

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

Categories