Creating new table in octobercms - laravel

I must create new table in octobercms project and I followed documentation and added new migration file inside plugin update file I have create_currency_rates_table.php file and it has this codes
<?php namespace RainLab\User\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class CreateCurrencyRateTable extends Migration
{
public function up()
{
Schema::create('currency_rates', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('currency');
});
}
public function down()
{
Schema::drop('currency_rate');
}
}
when I used php artisan october:up it is not detecting new migration. How can I create new table?Can anyone help me?

You also need to update plugins\<author>\<plugin_name>\updates\version.yaml this file and add your file name there as well.
So, in your case, you added a file like create_currency_rates_table.php then you need to add details of your file in version.yaml
for ex:
1.0.1: First version of Demo
1.0.2:
- Description About what is this update about?
- create_currency_rates_table.php
now when you next time just login to backend this table will be created automatically.
if any doubt please comment.

Related

Impementing global app settings for Laravel API?

I wish to implement a few global app settings, e.g. App name, first day of the week, and other feature flags. The end goal is to have these be fetched and edited by administrators through the API.
What might be the most convenient way of doing this? I've experimented with using a Settings model to store key-value pairs, but this doesn't make sense to me since the desired settings should be hard coded and won't change, and seeding the Settings table doesn't sound ideal. Thanks in advance!
You can access App name from Laravel provided config function.
$appName = config('app.name');
// This value is retrieved from .env file of APP_NAME=
If you have to store multiple values related with the week, you can create a new config file week.php
//config/week.php
return [
...
'first_day_of_the_week' => 0
];
In order to retrieve the first_day_of_the_week, you can use the same function config
$firstDayOfTheWeek = config('week.first_day_of_the_week')
Similar to other essential flags, you can create a new config file.
You can later cache your config variables using the following command.
php artisan config:cache
You can also create a Helper class inside any preferred location inside the laravel project. I keep my helper class inside App\Helpers.
<?php
namespace App\Helpers;
use Carbon\Carbon;
class DateHelpers
{
public const DATE_RANGE_SEPARATOR = ' to ';
public static function getTodayFormat($format = 'Y-m-d')
{
$today = Carbon::now();
$todayDate = Carbon::parse($today->format($format));
return $todayDate;
}
....
}
If you need to retrieve the method value in the Laravel project, you can access by
$getTodayDateFormat = App\Helpers\DateHelpers::getTodayFormat();
EDIT 1:
Based on the question description. You need to create one row in the settings table.
//create_settings_table.php Migration File
public function up()
{
// Create table for storing roles
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('app_name')->default("My App Name");
$table->unsignedInteger('first_day_of_the_week')->default(1);
....
$table->timestamps();
});
}
You only need one row of the settings table to retrieve/update the default value.
//Retrieving the first day
$first_day_of_the_week = App\Setting::first()->first_day_of_the_week;
//Updating the first day
...
$first_day_of_the_week = request('first_day_of_the_week');
App\Setting::first()->update([
'first_day_of_the_week' => $first_day_of_the_week
]);

Laravel migration - Update id field from integer auto increment to string

I tried to change id field from Integer auto increment to string but after I run migrate, the structure of MYSQL's not change.
This is my code:
Schema::table('table', function ($table) {
$table->dropPrimary();
$table->string('id', 50)->change()->primary();
});
Please help me, thank you so much.
You should write this. Hopefully this will solve your problem
Schema::table('table', function ($table) {
$table->dropPrimary('id');
$table->string('id', 50)->change()->primary();
});
Also you should check if doctrine/dbal is installed successfully

Laravel CRUD, Edit and Delete ID Not Found, But ID Exist

Here is migration.
Schema::create('ruanganjns', function (Blueprint $table) {
$table->increments('id');
$table->string('kode_jenis_ruangan');
$table->string('jenis_ruangan');
$table->date('tgl_berlaku');
$table->string('status');
$table->timestamps();
});
Here is model.
protected $table = 'ruanganjns';
protected $fillable = ['kode_jenis_ruangan','jenis_ruangan','tgl_berlaku','status'];
public $timestamps = true;
public function Ruangan()
{
return $this->HasMany('App\Ruangan','id_ruangan');
}
here is controller edit code.
public function edit(Ruanganjns $ruanganjns)
{
$ruanganjns = Ruanganjns::findOrFail($ruanganjns->id);
return view('ruanganjns.edit', compact('ruanganjns'));
}
here is the route.
here
The main error always comes from an ID not found. even though all of my code is copied and pasted from the first code source, but the first source code all goes well. and the third source code and so on goes well. the source of the problem is always in this second table even i change it name or make it from beginning it's always the same. are there misspellings or case sensitive or something wrong with my code?
this is the result in the web browser
here
Your are trying to implement Implicit model binding but you have some wrong variable name in controller action. It should match with the route placeholder like this
Your route is like this
Route::get('admin/ruangjns/{ruangjn}/edit', 'RuanganjnsController#edit');
Then your controller action code should be like this
public function edit(Ruanganjns $ruangjn) //here $ruangjn should match with route placeholder
{
return view('ruanganjns.edit', compact('ruangjn'));
}
It automatically fetch model record and if record not found then it will return 404 error. For details check the above link

Is there a way to extend user plgin in octoberCMS in production

I'm asking if there's any way to extend user plugin in OctoberCMS by adding more fields when the site is already in production. I know how to do this in development but I don't know how to do it i production. Help
just create new plugin in development which is going to extend your user plugin.
fields you are going to add just write them in to update files -> plugins\hardiksatasiya\demotest\updates
1.0.1:
- 'Initialize plugin.'
1.0.2:
- 'Created table hardiksatasiya_demotest_'
- update_user_my_custom_add_seen.php
update_user_my_custom_add_seen.php file
<?php namespace HardikSatasiya\DemoTest\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class UpdateUserMyCustomAddSeen extends Migration
{
public function up()
{
Schema::table('users', function($table)
{
$table->timestamp('last_seen')->nullable();
});
}
public function down()
{
Schema::table('users', function($table)
{
$table->dropColumn('last_seen');
});
}
}
this example shows how to add last_seen field in users table
now just use extend api to add field in form extendFormFields https://octobercms.com/docs/backend/forms#extend-form-fields
now test them on development server if all is working fine.
then only put that plugin in plugins directory
directory structure => plugins \ < author name > \ < plugin name >
now in live system - from backend logout and login
i guess this will do all required stuff.
make sure you test plugin well in development. to avoid unwanted problems
if any doubts please comment.

How to rewrite field in Laravel?

I tried to rewrite a primary key in laravel like as:
public function setIdAttribute($value)
{
$this->attributes['id'] = $value; // or instead $value $this->attributes['idCity']
}
So I need to replace value of id field on value of idCity field
Rename a columns you should create a new migration
To create migration for this change write on console
php artisan make:migration swapKeyId
then go to database->migrations->...swapkey....php
in method called function up() write the code below but replace your table name in my case i use users
Schema::table('users', function ($table) {
$table->renameColumn('id', 'userId');
});
now its time to push migrates to database
php artisan migrate
and its done. this method is renamin it via laravel, also you can change it directly to the database(MySQL,MariaDB,SQL Server etc)
Setting an attribute in your model to define how php should store a value is something entirely different from creating a primary key in MySQL. Setup your migrations following the documentation and there isn't a lot that can go wrong:
Migrations in Laravel 5.2

Resources