I'm migrating an app that uses passport and I want to know if there is any way to change passport default table names. For instance, changing the table oauth_clients to something_oauth_clients.
I think this is a "solution" here but I don't quite understand it because that "solution" implies creating new models and that means that I will need to make more changes that I would like to do.
Related
So I'm trying to modify the default Passport migrations to use different column names than what it ships with. I want to use "id_User" instead of "user_id" and "id_Client" instead of "client_id."
My only question is, will this mess anything up later down the line? Is Laravel passport expecting a user_id column, and is there a way to change the default behavior to use id_User instead?
Yes, it would probably mess things up down the line. If you do a search for client_id in the Passport repository you'll get a few results. And a search for user_id reveals many results.
It might depend how you would use Passport in your application, but I would not recommend changing the column names, especially not user_id. Perhaps have a look through the methods you plan to use in Passport and see how it would affect you.
You are probably better off using accessors on your User model.
public function getId_UserAttribute(){
return $this->user_id;
}
This way you don't mess with the core functionality but you still get your attributes the way you want it when you work with them.
I am currently using the PermissionManager package for Laravel-Backpack, and I see instructions on how to add permissions through the UI. I need to add roles and permissions from a script or setup (DB Migration maybe), and I see no documentation around this. Would I have to reverse engineer a migration script from a manually created permission (would this even work?) or is there an accepted way of doing this?
Actually the documentation is here or here. Creating new roles&permissions can be done any way you want (migration, script). The tables are very simple, they only have these columns:
id
name
created_at
updated_at
So the only thing you need to worry about is entering the name of the role/permission.
I'm learning laravel 5 and it's relationship with databases. I can find tons of info regarding how to work with sqlite and mysql but I'm having problems understanding what migrations are, how they are related with databases and my main issue...
If I have a pre-created postgres database with, let's say, 10 tables and their relationships already up and running, how can I make Laravel interact with them?
For example, I have my postgres schema table defined like this:
CREATE TABLE "users" (
"id" integer NOT NULL DEFAULT nextval('users_seq'),
"name" character varying(30) NOT NULL,
"email" character varying(50) NOT NULL UNIQUE,
"password" character varying(120) NOT NULL UNIQUE,
CONSTRAINT users_pk PRIMARY KEY ("id")
) WITH (
OIDS=FALSE
);
I already know Laravel comes with a generic migration for users table but how exactly should I work from within Laravel connecting to my db?
By the way, my database's name would be dummy.
May I use something like a migration class?
But as far as I understand, migrations are for creating/deleting/etc tables, right?
Or should I directly create a controller to interact with de db like these?
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class PagesController extends Controller
{
public function index()
{
$users = DB::table('users')->get();
return view('DBHandler',compact('users'));
}
}
And I imagine this is the query builder way of doing things and eloquent's way is quite different.
Any help would be appreciated.
According to Documentation of Laravel:
Migrations are like version control for your database, allowing a team to easily modify and share the application's database schema. Migrations are typically paired with Laravel's schema builder to easily build your application's database schema.
If you have a team working with you, is highly recommendable to use them for changes that must be applied on the scheme to the application works, these migrations let you to apply changes to database like drop, create, update, etc. On the other hand if you're going to start developing with a Schema already created, there is no problem you can interact with your scheme with Eloquent.
The way as Laravel take information from your database is with Eloquent a ORM, Here is the introduction from Laravel Documentation:
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.
So you have to create a Model for each one of your tables, except tables many to many. If your table is Users you must name the model User and Eloquent take it automatically if not, you must overwrite the $table attribute at the model like this:
public $table = 'user';
The rest what you can do with Eloquent you can find it at the documentation here
I hope this helps you out.
From the blog laravelbooks.com, quoting:
[...] Migrations are the Laravel way of helping you to evolve the database schema of your application (also known as its DDL) without having to drop and re-create the database each time you make a change. And not having to drop and recreate the database each time a change happens means that you don’t lose your development data. The only changes made when you execute a migration are those necessary to move the schema from one version to another, whether that move is forward or backward in time.
Not only does Laravel migration provide you with a means to change your database schema in an iterative manner, but it lets you do so using PHP code, rather than SQL! The Laravel Schema Builder allows us to create database tables and insert columns or indices quickly. It uses clean and expressive syntax to make database operations happen. You may think of Laravel migration as version control for your databases! [...]
There I found a more explanatory way to laravel than the official documentation, though the later it's also good
I am trying to figure out how best to modify a MySQL Table's existing Column using the CakePHP Migrations plugin. I do not need to add or drop the column, I simply want to modify a string column's length.
Currently the column is defined as a varchar(50); I am repurposing the column and wish to define it as varchar(2000).
The goal of the migration is to be part of an automated deployment taking place on a standard CakePHP web app installation on a typical web server.
Near as I can tell, it looks like the only way (other than an ALTER statement) to accomplish this using the Migrations Plugin would be to:
rename the column
add the new column
Move/copy the existing data to the new column
drop the old column
Perhaps I have missed the discussion in the documents and countless tutorials and how to's out there on a better way to accomplish this, but this seems like a cumbersome and self defeating method.
I have been through both the CakePHP Migration Plugin's documentation and the Phinx's documentation but am failing to see the recommended method for this change. I appreciate any input for this.
Unfortunately the Phinx docs aren't that complete, there seem to be various undocumented methods, like the one you are looking for: \Phinx\Db\Table::changeColumn()
https://github.com/robmorgan/phinx/blob/v0.4.6/src/Phinx/Db/Table.php#L392
https://github.com/robmorgan/phinx/issues/682
The following should work
$table = $this->table('table_name');
$table->changeColumn('column_name', 'string', [
'limit' => 2000
]);
$table->update();
I have two tables in DB (topic, topic_content):
what kind of entities i should create for symfony2?
I think, i should have something like this in my symfony structure (Entities/Topic.php, Entities/Topic_content.php) help me please..
Yes, you would create Topic and Topic Content. And likely also a User Entity (because user_id looks like a foreign key).
However, the idea in Symfony2 is to approach the application from the Model site instead of the database site. Quoting https://doctrine-orm.readthedocs.org/en/latest/tutorials/getting-started-database.html:
Development Workflows
When you Code First, you start with developing Objects and then map them onto your database. When you Model First, you are modelling your application using tools (for example UML) and generate database schema and PHP code from this model. When you have a Database First, you already have a database schema and generate the corresponding PHP code from it.
For database first, there is a generator that will derive objects based on your schema:
https://github.com/beberlei/DoctrineCodeGenerator
The recommended approach though is to have Doctrine generate the db schema from your Entities.
Quoting Getting Started - Generating the DB Schema
Doctrine has a Command-Line Interface that allows you to access the SchemaTool, a component that generates the required tables to work with the metadata.
It requires some setup, which is explained in the guide. Once you have that, you simply tell Doctrine to generate or update your schema, whenever your object structure changes.