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
Related
Best way for me to describe my problem and it's go-to solution would be this link;
StackOverflow
My problem is exactly this, and the solution actually is working, but not in my case, either I will have an alternative solution for mine, or I'm doing something wrong with my schema builder and I need to understand it better.
My code is basically like this:
//just an example, not my code
Schema A (as)
//other code, such as table->increments('id')
$table->unsignedInteger('b_id');
$table->unsignedInteger('c_id');
$table->foreign('b_id')->references('id')->on('bs');
$table->foreign('c_id')->references('id')->on('cs');
Schema B (bs)
$table->unsignedInteger('a_id');
$table->unsignedInteger('c_id');
$table->foreign('a_id')->references('id')->on('as');
$table->foreign('c_id')->references('id')->on('cs');
Schema C (cs)
$table->unsignedInteger('a_id');
$table->unsignedInteger('b_id');
$table->foreign('a_id')->references('id')->on('as');
$table->foreign('b_id')->references('id')->on('bs');
So neither order helps me with this solution.
Is there a solution to my case, or my code/schema logic is wrong and I need to modify my code?
Your schema is incorrect. You can't have tables being interdependent, i.e, they can't be both master and slave to each other at the same time. This way, you can never make them at all.
You should create master tables first, let's say A,B,C.
Schema A:
$table->increments('id');
// some other columns
Schema B:
$table->increments('id');
// some other columns
Schema C:
$table->increments('id');
// some other columns
Now, create the child tables, in other words, these are intermediate tables describing many-to-many relationships and you can access them using pivot attribute.
Schema AS:
$table->unsignedInteger('b_id');
$table->unsignedInteger('c_id');
$table->foreign('b_id')->references('id')->on('B');
$table->foreign('c_id')->references('id')->on('C');
Schema BS:
$table->unsignedInteger('a_id');
$table->unsignedInteger('c_id');
$table->foreign('a_id')->references('id')->on('A');
$table->foreign('c_id')->references('id')->on('C');
Schema CS:
$table->unsignedInteger('a_id');
$table->unsignedInteger('b_id');
$table->foreign('a_id')->references('id')->on('A');
$table->foreign('b_id')->references('id')->on('B');
Now, you can successfully run a migration in this order and you should be good to go.
In Laravel >= 5.0, one way to achieve this is to have certain scripts in properly named migration folders. Like I use to have migrations in Sprints.
--migrations/
---Sprint8/
------user_table.php
------car_table.php
--Sprint9/
------complaint_table.php
------supervisor_table.php
With this approach, you have to run the migration command on each of your subfolders:
php artisan migrate --path=/database/migrations/Sprint8
php artisan migrate --path=/database/migrations/Sprint9
However, what you can do is easily extend the artisan system to write your own migrate command that will iterate through all folders under the migrations folder, create these commands for you and run them.
You can also simply write a shell script if you don't want to get into doing this via artisan
I'm wondering how I am supposed to handle "partial" seeding.
Example, I have a "settings" table for the applications settings and these are populated on the initial seed, and down the road I need to add new settings. Is there a way to seed new data into the database or can seeding only be done on a fresh database?
If the latter, would the correct way to do this be to populate data through a migration?
Thanks.
You can try to use --class option to seed only settings, for example:
php artisan db:seed --class=SettingsTableSeeder
Here are the tables in my scenario
user (user_id)
user1_order
user2_order
user3_order
user1_calcs
user2_calcs
..........
I have my own reasons to have separate table per user (lots of data per user, lots of joins/queries in my app, separation of user data, user satisfaction)
Laravel has migration feature, so you have to create a migration that will create a table..
My question is, how would I use Laravel to my scenario? as I am not sure when user signs up, and I can not pre-generate user tables until a user signs up..
Any help is highly appreciated...
You could use Eloquent Events (do not confuse it with Laravel Events) to run table creation tasks, for example you could run custom artisan command directly from your code.
This command could create migrations from prepared stubs and then run php artisan migrate command.
I'm using YII 1.1.12. When I do:
yiic migrate
inside the protected folder of my application, I get told that there is a new migration to be applied. I answer "Yes" so that the migration would be applied. After a while, I get:
*** applied m121220_121256_initialize_database (time: 6.060s)
Migrated up successfully.
All is fine up to this point. Then when I type 'yiic migrate', instead of being told that there is no new migration, I get told that:
Yii Migration Tool v1.0 (based on Yii v1.1.12)
Total 1 new migration to be applied:
m121220_121256_initialize_database
Apply the above migration? (yes|no) [no]:
WhenI check the tbl_migration table, the only thing in there is the base migration. There's nothing aboutinitialize_database.
Any ideas?
Does your migration create the database? If so it might be throwing Yii off, and it's creating the migration structure at the start and then can't insert into, I'm not sure what the behaviour would be.
If m121220_121256_initialize_database is doing any kind of destructive work then it's probably a good idea to use yiic migrate mark 121220_121256 to manually set the database to this migration after you've ran it.
That way you can do further tests to see whether it's a migration bug or something destructive in the migration like dropping/creating a database.
I realised the problem was that the sql commands I was running straight from PHPMyAdmin contained a transaction. When I removed the lines about transactions, the database row in the yii_migration table was inserted successfully. I'm not sure why this should be, but there it is.
I am totally new to FuelPHP, ORM and migrations in general so sorry if I come across like a newbie, but I've been struggling with this for a few hours now so I thought I'd ask for help. I think I'm either doing something wrong or missing something fundamental.
I am trying to create a users model, for simplicity let's say it just has a string representing name.
I was under the impression that using the following two Oil commands would create a users model, and an associated migration which after running would build an associated table:
php oil generate model user name:string
oil refine migrate
This does successfully create the model and migration, but running the second command doesn't build the table in the database.
If I run these commands on the other hand:
php oil generate migration create_user name:text
oil refine migrate
The migration is created and the table is built in my database. I noticed that perpending 'create_' to the migration name made it possible to create the table, whereas leaving it off (i.e php oil generate migration user name:text) doesn't insert the table to the DB. I noticed the generated migrations with and without the 'create_' are significantly different.
So my question ultimately is, how do I create the model, associated migration which creates the table? Or, am I totally misunderstanding something?
Thanks!
If you get 'Already on the latest migration', your migration tracking data is out of sync. Migrations are tracked both in the database (a table called migration) and a config file in your environment folder called migrations.php.
If there is already an entry in one of them, oil will not run it again.
So you can't just delete the table through the backdoor and then run the migration again. You'll have to run a 'migrate:down' to revert the last migration, or if you delete all, also delete the migration table and config file.
Again, credit to Harro Verton on the FuelPHP forums.