7 Laravel E-Commerce Application Development - Settings Section Part 1 - LaraShout
7 Laravel E-Commerce Application Development - Settings Section Part 1 - LaraShout
TABLE OF CONTENT
https://www.larashout.com/settings-section-part-1 1/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout
02 Settings Seed
06 Conclusion
Open the migration le for settings, and update with the below
one.
https://www.larashout.com/settings-section-part-1 3/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('settings');
}
}
https://www.larashout.com/settings-section-part-1 4/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout
Now open your Se ing model and replace with the below ones.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* @var array
*/
protected $fillable = ['key', 'value'];
}
Settings Seed
Next, we will add a database seed for our settings table. Run
below command in the terminal.
https://www.larashout.com/settings-section-part-1 5/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout
property in the class. This $settings will have an array which will
contain the key/value pairs of the settings we want to manage.
use App\Models\Setting;
use Illuminate\Database\Seeder;
https://www.larashout.com/settings-section-part-1 7/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout
https://www.larashout.com/settings-section-part-1 8/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
foreach ($this->settings as $index => $setting)
{
$result = Setting::create($setting);
if (!$result) {
$this->command->info("Insert failed at record $index.");
return;
}
}
$this->command->info('Inserted '.count($this->settings). ' records');
}
}
As you can see, we are adding the various settings for our
application, we will be using them as we make the progress in
https://www.larashout.com/settings-section-part-1 9/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout
model, the rst will be get() and second will be set() method.
Open you Se ing model and add the get() function like below.
/**
* @param $key
*/
public static function get($key)
{
$setting = new self();
$entry = $setting->where('key', $key)->first();
if (!$entry) {
return;
}
return $entry->value;
}
https://www.larashout.com/settings-section-part-1 10/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout
/**
* @param $key
* @param null $value
* @return bool
*/
public static function set($key, $value = null)
{
$setting = new self();
$entry = $setting->where('key', $key)->firstOrFail();
$entry->value = $value;
$entry->saveOrFail();
Config::set('key', $value);
if (Config::get($key) == $value) {
return true;
}
return false;
}
In the above method, we are rst checking if the given $key has
any value in the database, then we are updating the value for
the given setting. Next, we are setting the current key/value for
setting to the Laravel Con guration, so we can load them using
the Laravel config() helper function.
Don’t forget to add the Config class in your model using the
use statement like use Config;
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind('settings', function ($app) {
return new Setting();
});
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
https://www.larashout.com/settings-section-part-1 12/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout
$loader->alias('Setting', Setting::class);
}
Above code rstly, bind the setting model and then using the
AliasLoader , we register it as a facade.
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
// only use the Settings package if the Settings table is present in the da
if (!\App::runningInConsole() && count(Schema::getColumnListing('settings')
$settings = Setting::all();
foreach ($settings as $key => $setting)
{
Config::set('settings.'.$setting->key, $setting->value);
}
}
}
https://www.larashout.com/settings-section-part-1 13/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout
Conclusion
In this part, we have added the settings model, migration, seed
and extra functionality for our settings section.
Code Repository
In the next post, we will add the settings section in our admin
area from where we will be able to update all our setting’s
values.
https://www.larashout.com/settings-section-part-1 14/28