How do I re-arrange a column in a Laravel migration file for a MySQL database?
So far I have the following:
$table->date('foo')->after('bar')->change();
However, it does not seem to re-arrange the column.
Why not and how can I fix this?
Can't see anything in Laravel Schema API that will allow you to rearrange columns. Your best bet will be to use raw SQL statement as below.
DB::statement("ALTER TABLE table_name MODIFY COLUMN col_name col_definition AFTER another_col");
Try this, hope it help you to find right solution:
public function up()
{
DB::statement("ALTER TABLE example MODIFY COLUMN foo DATE AFTER bar");
}
public function down()
{
DB::statement("ALTER TABLE example MODIFY COLUMN foo DATE AFTER bar");
}
First Run following artisan command
php artisan make:migration reorganize_order_of_column_<col_name> --table=<table_name>
where col_name is name of column which you want to re-order and table_name if the name of your table
and then it will generate on new file in migration folder then update that file as following function
public function up()
{
DB::statement("ALTER TABLE <table_name> MODIFY COLUMN <col_name> <col_description> AFTER <second_col_name>");
}
public function down()
{
DB::statement("ALTER TABLE <table_name> MODIFY COLUMN <col_name> <col_description> AFTER <second_col_name>");
}
table_name is the name of table which you want to modify
col_name is the name of column which you want to re-order
col_description is the type and length of column like VARCHAR(5),DATE
second_col_name is the name of second column by which we are going to reorder
Related
I receive a MySql database and one table inside it have a Date column in string format, now I need to build a .Net core server with Pomelo and EF Core and requirement is my server can query data from that table in a range of date, but because Date column of that table is in string format so I don't know how to query it, please help.
Thank you!
You are going to have to get that string into a date in order to query it.
I would probably add a new datetime column to the table and then create a simple console app that reads in each string date, try to parse this as a datetime and save it to the new datetime column.
Then you should see how many rows have valid datetimes and correct the others
Finally, you can then query using Entity Framework
how to convert a string to date in mysql?
As was told here
You can Query string to date
SELECT STR_TO_DATE(yourdatefield, '%m/%d/%Y')
FROM yourtable
With database schema change
If you can (i.e. are allowed) to change the schema of the table in question, then just add a new datetime or date column, copy the data over from the old column to the new one, and drop the column:
ALTER TABLE `YourTable` ADD COLUMN `NewDateColumn` date NOT NULL;
UPDATE `YourTable` SET `NewDateColumn` = STR_TO_DATE(`OldDateColumn`,'%Y-%m-%d');
ALTER TABLE `YourTable` DROP COLUMN `OldDateColumn`;
You can run these statements just using MySQLWorkbench or the commmand line tool. Of course you first test them with a local copy, to see that everything works fine.
With value converter
If you cannot change the schema of the table, then you can still query date ranges from the database, as long as the date strings in the database are in a string format, that sorts alphabetically (e.g. YYYY-MM-DD). In that case, you can just use a value converter in your actual app code and don't need to alter the database at all:
public class SomeModel
{
public int SomeModelId {get; set;}
public DateTime YourDateProperty {get; set;} // <-- the type you want to use in .NET
}
public class Context : DbContext
{
public virtual DbSet<SomeModel> SomeModels { get; set; }
// ...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SomeModel>(
entity =>
{
entity.Property(e => e.YourDateProperty)
.HasColumnType("varchar(255)") // <-- the type it has in the database table
.HasConversion(
v => v.ToString(#"yyyy\-MM\-dd"),
v => DateTime.Parse(v, CultureInfo.InvariantCulture));
});
}
}
// Here is how a sample query in your app would look like:
var query = context.SomeModels
.Where(m => m.YourDateProperty >= new DateTime(2020, 9, 1) &&
m.YourDateProperty < new DateTime(2020, 9, 10))
.ToList();
Is it possible to automatically append database name to a table name in laravel?
The issue is that I have to join data from multiple databases in single queries and sometime I am having to manually replace template names, which is a lot of hassle.
The only solution that I found is that I can append database name to the table name within a model, i.e.
class User extends Model
{
protected $table = 'database_name.table_name';
}
But with above we are losing support for table prefixes.
Example when database name is not applied:
$userQuery = User::where('id', 1)
->with('settings')
->select('some data');
DB::connection('x')
->table('table-on-different-connection')
->insertUsing(['some columns'], $userQuery);
$userQuery is on a different connection and database_name was not applied to the tables within that part of the query. Hence why insertUsing is trying to perform joins on connection x.
Laravel is not appending database name when generating SQL statements. To resolve that, you need to create your own MySQL wrapper and append the database name to the table name that way.
This is where the issue takes place:
vendor\laravel\framework\src\Illuminate\Database\Query\Grammar.php
public function wrapTable($table)
{
if (! $this->isExpression($table)) {
return $this->wrap($this->tablePrefix.$table, true);
}
return $this->getValue($table);
}
You need to override wrapTable method and append database name to the table that way.
i.e.
public function wrapTable($table)
{
$databaseName = $this->wrap('my_database'); // dynamically defined name here
if (! $this->isExpression($table)) {
$tableName = $this->wrap($this->tablePrefix.$table, true);
return "{$databaseName}.{$tableName}";
}
return $this->getValue("{$databaseName}.{$table}");
}
How you go about extending Grammar and override this method depends on your application and your needs. This can be done globally (i.e. via AppProvider) or for an individual query.
Using Laravel 5.5 and Mysql (10.1.19-MariaDB)
For a md5 hash I want a binary(16) column.
Let's call the colum url_hash
When using :
$table->binary('url_hash');
it will give me a BLOB column.
source : https://laravel.com/docs/5.5/migrations#creating-columns
I have seen all kind of hacks or plugins around the web for this , but what is the most simple one without any external plugins that could break on the next update?
Cheers
You can just set the character set to binary.
$table->char('url_hash', 16)->charset('binary');
This is actually shown as a real binary column type with a length of 16 in MySQL Workbench.
There shouldn't be any difference: https://stackoverflow.com/a/15335682/5412658
Extend the MySqlGrammar class, e.g. in app/MySqlGrammar.php:
namespace App;
use Illuminate\Support\Fluent;
class MySqlGrammar extends \Illuminate\Database\Schema\Grammars\MySqlGrammar {
protected function typeRealBinary(Fluent $column) {
return "binary({$column->length})";
}
}
Then use a macro to add your own column type:
DB::connection()->setSchemaGrammar(new \App\MySqlGrammar());
Blueprint::macro('realBinary', function($column, $length) {
return $this->addColumn('realBinary', $column, compact('length'));
});
Schema::create('table', function(Blueprint $table) {
$table->realBinary('url_hash', 16);
});
Laravel author recommends to do a DB:statement call and run the raw SQL.
If you are running migration, you could run this raw SQL after Schema::create:
DB::statement('ALTER TABLE table_name ADD url_hash binary(16) AFTER some_column');
Depends on use case, you could need to run this raw SQL to drop the column before dropping the table:
DB::statement('ALTER TABLE table_name DROP url_hash');
I'm trying to get values from my linking table sfees with columns student_id and mfee_id. Here, there might be multiple student_id with different mfee_id. The thing is that, i want to retrieve all mfee_id with same student_id.
I have used following syntax, but it is only returning single value:
public function verify($id,$sid)
{
$sfees = sfee::where('student_id', $sid)->value('mfee_id');//trying to get only mfee_id
return $sfees;
}
How can i solve this problem?
//edited
My table looks like:
You need to do a groupBy -
$sfees = sfee::where('student_id', '=',$sid)->groupBy('student_id')->get();
UPDATE
Try something like this -
$sfees = sfee::where('student_id', '=',$sid)->lists('mfee_id');
Or you can use the Schema Builder like this -
DB::table('sfees')->where('student_id', '=', $id)->lists('mfee_id');
I have one database in which all table names like below
configuration_dst,
developer_dst,
application_dst
Now I want to manage my query in which want to add database prefix after table name instead of before.
For example :
{TABLE NAME}{PREFIX}
is it possible to manage using CI 3.0 ?
I have tried like below in Application/configuration/database.php
$db['default']['dbprefix']="_dst";
$db['default']['swap_pre']="{POST}";
Current Query :
$this->db->get('templates');
Current Table Name :
tablename : _dsttemplates
Expected Table Name :
tablename : templates_dst
I need prefix after table Name not before but didn't get any solution.
The only option is to change in driver files. You can do this using the following steps:
Go to /system/database/DB_query_builder.php
Search public function dbprefix
Replace
return $this->dbprefix.$table;
with
return $table.$this->dbprefix;
If it also interferes in any other places in your project then create a new function with replaced code like:
public function dbsuffix($table = '')
{
if ($table === '')
{
$this->display_error('db_table_name_required');
}
return $table.$this->dbprefix;
}
Working with Database prefixes manually
if you use
$this->db->set_dbprefix('newprefix');
$this->db->dbprefix('tablename'); // outputs newprefix_tablename
its always gives the table name as
newprefix_tablename
Because this is Codeigniter pastern.
Codeigniter Prefix