0% found this document useful (0 votes)
15 views14 pages

7 Laravel E-Commerce Application Development - Settings Section Part 1 - LaraShout

Uploaded by

Iggy Csi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

7 Laravel E-Commerce Application Development - Settings Section Part 1 - LaraShout

Uploaded by

Iggy Csi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout

LaraShout → Laravel → Laravel E-Commerce Application


Development – Settings Section Part 1

Laravel E-Commerce Application Development


– Settings Section Part 1

POSTED ON 27 MAY 2019 POSTED IN LARAVEL


TAGS: LARAVEL ECOMMERCE APPLICATION, LARAVEL SETTINGS
SECTION
5270 VIEWS

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

01 Settings Model & Migration

02 Settings Seed

03 Setting Model Set and Get Methods

04 Registering Setting Model as Facade

05 Autoloading All Settings

06 Conclusion

Laravel E-Commerce Application Development


( 27 Lessons )
In this course, you’ll learn how to create an E-Commerce
Website from scratch in Laravel. The process has never
been easier I’ll take you from the very beginning stages of
setting up Laravel till the last steps of adding products to
the cart. If you’ve good understanding & experience in
PHP & MySQL then this course is for you.
SEE FULL SERIES

This is the sixth part of the Laravel E-Commerce Application


Development series, in this part will add the settings model,
migration and seed.

I assume you should have the e-commerce application project


on your machine or you can grab it from Laravel E-Commerce
Application repository, we will start from where we left it in the
last part.
https://www.larashout.com/settings-section-part-1 2/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout

Any web application’s backend needs to have some sort of a


settings section from where you can change the various
settings of your application. In our e-commerce application, we
would like to have some settings such as site name, logos,
shipping methods control, payment methods control and so on.

There are various packages available to add the settings


functionality in your application, but I would like to implement
it from scratch. I will share with you how I add the settings to
any of my Laravel based application.

So let’s start some coding.

Settings Model & Migration


First thing rst, we would like to save our settings in the
database. So we need to add a model and migration for our
settings table. Open your terminal and run the below command
to generate the model and migration for settings.

php artisan make:model Models\\Setting -m

Above command will generate a Se ing model in app/Models

folder and a migration le in database/migrations folder.

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;

class CreateSettingsTable extends Migration


{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('settings', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('key')->unique();
$table->text('value')->nullable();
$table->timestamps();
});
}

/**
* 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

We will be stroing the settings as key value pairs, so we simply


added the key and value columns.

Now open your Se ing model and replace with the below ones.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Setting extends Model


{
/**
* @var string
*/
protected $table = 'settings';

/**
* @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.

php artisan make:seed SettingsTableSeeder

https://www.larashout.com/settings-section-part-1 5/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout

Above command will generate a database seed in database/seeds

folder. Open the Se ingsTableSeeder class and add the $settings

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;

class SettingsTableSeeder extends Seeder


{
/**
* @var array
*/
protected $settings = [
[
'key' => 'site_name',
'value' => 'E-Commerce Application',
],
[
'key' => 'site_title',
'value' => 'E-Commerce',
],
[
'key' => 'default_email_address',
'value' => '[email protected]',
],
[
'key' => 'currency_code',
'value' => 'GBP',
],
[
https://www.larashout.com/settings-section-part-1 6/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout

'key' => 'currency_symbol',


'value' => '£',
],
[
'key' => 'site_logo',
'value' => '',
],
[
'key' => 'site_favicon',
'value' => '',
],
[
'key' => 'footer_copyright_text',
'value' => '',
],
[
'key' => 'seo_meta_title',
'value' => '',
],
[
'key' => 'seo_meta_description',
'value' => '',
],
[
'key' => 'social_facebook',
'value' => '',
],
[
'key' => 'social_twitter',
'value' => '',
],
[

https://www.larashout.com/settings-section-part-1 7/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout

'key' => 'social_instagram',


'value' => '',
],
[
'key' => 'social_linkedin',
'value' => '',
],
[
'key' => 'google_analytics',
'value' => '',
],
[
'key' => 'facebook_pixels',
'value' => '',
],
[
'key' => 'stripe_payment_method',
'value' => '',
],
[
'key' => 'stripe_key',
'value' => '',
],
[
'key' => 'stripe_secret_key',
'value' => '',
],
[
'key' => 'paypal_payment_method',
'value' => '',
],
[

https://www.larashout.com/settings-section-part-1 8/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout

'key' => 'paypal_client_id',


'value' => '',
],
[
'key' => 'paypal_secret_id',
'value' => '',
],
];

/**
* 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

this series. In the run() method, we are looping through the


$settings property and creating a new record for each record.

Setting Model Set and Get Methods


Now, we will add two static methods in our app/Models/Setting

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;
}

In this method, we are simply querying the record by $key and


returning the value for a given key.

Now we will add a set() method, which we will use to update


the setting 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;

Registering Setting Model as Facade


https://www.larashout.com/settings-section-part-1 11/28
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 1 | LaraShout

Now using the service provider, we will bind our model to


Laravel’s application container and register it as a facade so we
can use the setting model like Setting::get() or Setting::set() .

For this, we will run the below command to generate a service


provider.

php artisan make:provider SettingServiceProvider

Register Service Provider

Don’t forget to register Se ingServiceProvider in your


config/app.php le.

Now open the Se ingServiceProvider class and add the below


code in the register method.

/**
* 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.

Autoloading All Settings


In an ideal scenario, it will be very helpful if we load all our
settings when our application boot up, so we can use the
setting anywhere we want it.

To autoload all the settings update the boot() method of the


Se ingServiceProvider with below one.

/**
* 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

In the above code example, rstly we are checking if the


application is not running in the console and there is a table
exist with the name se ings. Then we are loading all settings
from the Se ing model and then setting all values using the
Laravel Con g class.

Conclusion
In this part, we have added the settings model, migration, seed
and extra functionality for our settings section.

Code Repository

You can nd the code base of this series on Laravel


eCommerce Application 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.

If you have any question, please leave it in the comment box


below.

17 comments on “Laravel E-Commerce Application


Development – Settings Section Part 1”

https://www.larashout.com/settings-section-part-1 14/28

You might also like