0% found this document useful (0 votes)
56 views34 pages

8 Laravel E-Commerce Application Development - Settings Section Part 2

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)
56 views34 pages

8 Laravel E-Commerce Application Development - Settings Section Part 2

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

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

LaraShout → Laravel → Laravel E-Commerce Application


Development – Settings Section Part 2

Laravel E-Commerce Application Development


– Settings Section Part 2

POSTED ON 29 MAY 2019 POSTED IN LARAVEL


TAGS: LARAVEL ECOMMERCE APPLICATION, LARAVEL SETTINGS
SECTION
4934 VIEWS

TABLE OF CONTENT

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

01 Adding Setting Routes

02 Creating Setting Controller

03 Updating Settings Menu Links

04 Creating File Upload Trait

05 Creating Flash Message Partial

06 Creating Settings Page Main View

07 Adding Setting Sections Views

08 Saving Settings using Setting Controller

09 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 seventh part of the Laravel E-Commerce Application


Development series, in this part will add the settings section in
the admin area of our application.

https://www.larashout.com/settings-section-part-2 2/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

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.

In this post, we will add a settings section in the admin area


from where we will be able to control all settings for our
applications. This is how our settings page will look like.

Settings Section

Adding Setting Routes


Firstly, we will add some routes for our settings section. Open
your routes/admin.php le which looks like below.

Route::group(['prefix' => 'admin'], function () {


Route::get('login', 'Admin\LoginController@showLoginForm')->name('admin.log
Route::post('login', 'Admin\LoginController@login')->name('admin.login.post

https://www.larashout.com/settings-section-part-2 3/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

Route::get('logout', 'Admin\LoginController@logout')->name('admin.logout');
Route::group(['middleware' => ['auth:admin']], function () {
Route::get('/', function () {
return view('admin.dashboard.index');
})->name('admin.dashboard');
});
});

Just after the dashboard route, we will add two more routes
for our settings section.

Route::get('/settings', 'Admin\SettingController@index')->name('admin.settings'
Route::post('/settings', 'Admin\SettingController@update')->name('admin.setting

First route is a GET route for accessing our settings page,


second one is a POST route to update all our settings. Instead of
creating separate routes for all setting’s sections, we will use
one controller method to update any kind of setting.

Creating Setting Controller


Now we need to create a Se ingController, open your
command line terminal and run the below command:

php artisan make:controller Admin\SettingController

https://www.larashout.com/settings-section-part-2 4/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

Above command will generate a new controller class at


app/Http/Controllers/Admin called Se ingController.

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SettingController extends Controller


{
//
}

As you can see this class is extending Controller class but we


want to switch it to use our BaseController class, which we
created in this post.

Update your controller class code with the below one.

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;

class SettingController extends BaseController


{
//
}

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

Now we have extended the BaseController class which will


enable us to use the ash messages and redirect methods we
created earlier in this series.

Now, we will add two new methods in this class called index()

and update() . Firstly add the index() method like below.

/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
$this->setPageTitle('Settings', 'Manage Settings');
return view('admin.settings.index');
}

In the index method, rstly we are setting the page title and
subtitle, then returning the settings index view.

Now add the second method like below and leave it as it is, we
will update this method later in this post.

/**
* @param Request $request
*/
public function update(Request $request)
{

https://www.larashout.com/settings-section-part-2 6/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

Updating Settings Menu Links


As we have created the settings routes and a corresponding
controller, now we can go ahead and update the settings links
in our application.

Open resources/views/admin/partials/sidebar.blade.php le and update the


whole markup of your le to below one.

<div class="app-sidebar__overlay" data-toggle="sidebar"></div>


<aside class="app-sidebar">
<ul class="app-menu">
<li>
<a class="app-menu__item {{ Route::currentRouteName() == 'admin.das
<i class="app-menu__icon fa fa-dashboard"></i>
<span class="app-menu__label">Dashboard</span>
</a>
</li>
<li>
<a class="app-menu__item {{ Route::currentRouteName() == 'admin.set
<i class="app-menu__icon fa fa-cogs"></i>
<span class="app-menu__label">Settings</span>
</a>
</li>
</ul>
</aside>

In this le, we have updated the href link using the route()

helper function for settings menu link. Also we have used the

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

Route::currentRouteName() to check the current route against the


supplied one and based on that we are injecting the active class
to list items to make them active.

Now we will update the link of settings in the header le. Open
resources/views/admin/partials/header.blade.php le and update the setting’s
link with the below one.

<li>
<a class="dropdown-item" href="{{ route('admin.settings') }}"><i class="fa
</li>

Now if you run your application and try to access the settings
page using settings menu, you will receive a view not found
exception from Laravel. We will create this view shortly.

Creating File Upload Trait


We will be uploading images in our application for various
reasons like product images, brand logos, and category images,
etc. In our settings section, we want to add the functionality of
changing the website logo and favicons from our admin area.
Because we will be using over and over image upload logic, it
will be useful if we create a trait which provides us the image
upload handling.

Create a new PHP le in the app/Traits folder named UploadAble


and add the below code in this le.
https://www.larashout.com/settings-section-part-2 8/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

namespace App\Traits;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

/**
* Trait UploadAble
* @package App\Traits
*/
trait UploadAble
{
/**
* @param UploadedFile $file
* @param null $folder
* @param string $disk
* @param null $filename
* @return false|string
*/
public function uploadOne(UploadedFile $file, $folder = null, $disk = 'publ
{
$name = !is_null($filename) ? $filename : str_random(25);

return $file->storeAs(
$folder,
$name . "." . $file->getClientOriginalExtension(),
$disk
);
}

/**
* @param null $path

https://www.larashout.com/settings-section-part-2 9/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

* @param string $disk


*/
public function deleteOne($path = null, $disk = 'public')
{
Storage::disk($disk)->delete($path);
}
}

Above trait will provide us two methods uploadOne and deleteOne .

Learn More

If you want to learn more about image/ le uploading in


Laravel, please read our post on Laravel Image Upload Made
Easy
.

Now we will use this trait in our Se ingController. Open your


Se ingController and include the trait like below:

use UploadAble;

Creating Flash Message Partial


In the previous post, we added the functionality of showing
Flash messages in our application. We created a FlashMessages
trait for this purpose and included it in our BaseController.

https://www.larashout.com/settings-section-part-2 10/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

Now we will creata a partial view to show those ash


messages. For that, create a new le in resources/views/admin/partials

folder and name it ash.blade.php. Add the below code into


this le.

@php
$errors = Session::get('error');
$messages = Session::get('success');
$info = Session::get('info');
$warnings = Session::get('warning');
@endphp
@if ($errors) @foreach($errors as $key => $value)
<div class="alert alert-danger alert-dismissible" role="alert">
<button class="close" type="button" data-dismiss="alert">×</button>
<strong>Error!</strong> {{ $value }}
</div>
@endforeach @endif

@if ($messages) @foreach($messages as $key => $value)


<div class="alert alert-success alert-dismissible" role="alert">
<button class="close" type="button" data-dismiss="alert">×</button>
<strong>Success!</strong> {{ $value }}
</div>
@endforeach @endif

@if ($info) @foreach($info as $key => $value)


<div class="alert alert-info alert-dismissible" role="alert">
<button class="close" type="button" data-dismiss="alert">×</button>
<strong>Info!</strong> {{ $value }}
</div>
@endforeach @endif

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

@if ($warnings) @foreach($warnings as $key => $value)


<div class="alert alert-warning alert-dismissible" role="alert">
<button class="close" type="button" data-dismiss="alert">×</button>
<strong>Warning!</strong> {{ $value }}
</div>
@endforeach @endif

In this view, we are getting the ash messages of all types from
the Laravel session and then showing the different type of
bootstrap alerts based on the ash message type.

Creating Settings Page Main View


Now it’s time to create our setting page view, create a new
folder inside resources/views/admin folder and name it settings . Inside
this folder we will create a new le called index.blade.php .

Add the below markup in this le.

@extends('admin.app')

@section('title') {{ $pageTitle }} @endsection

@section('content')
<div class="app-title">
<div>
<h1><i class="fa fa-cogs"></i> {{ $pageTitle }}</h1>
</div>
</div>
@include('admin.partials.flash')

https://www.larashout.com/settings-section-part-2 12/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

<div class="row user">


<div class="col-md-3">
<div class="tile p-0">
<ul class="nav flex-column nav-tabs user-tabs">
<li class="nav-item"><a class="nav-link active" href="#gene
<li class="nav-item"><a class="nav-link" href="#site-logo"
<li class="nav-item"><a class="nav-link" href="#footer-seo"
<li class="nav-item"><a class="nav-link" href="#social-link
<li class="nav-item"><a class="nav-link" href="#analytics"
<li class="nav-item"><a class="nav-link" href="#payments" d
</ul>
</div>
</div>
<div class="col-md-9">
<div class="tab-content">
<div class="tab-pane active" id="general">
@include('admin.settings.includes.general')
</div>
<div class="tab-pane fade" id="site-logo">
@include('admin.settings.includes.logo')
</div>
<div class="tab-pane fade" id="footer-seo">
@include('admin.settings.includes.footer_seo')
</div>
<div class="tab-pane fade" id="social-links">
@include('admin.settings.includes.social_links')
</div>
<div class="tab-pane fade" id="analytics">
@include('admin.settings.includes.analytics')
</div>
<div class="tab-pane fade" id="payments">
@include('admin.settings.includes.payments')

https://www.larashout.com/settings-section-part-2 13/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

</div>
</div>
</div>
</div>
@endsection

If you will try to load /admin/settings URL, you will get some errors
which are perfectly ne as we haven’t created the different
partials for this view.

Create a new folder named includes inside settings folder. Inside


this folder create following les and leave them empty.

1. general.blade.php
2. logo.blade.php
3. footer_seo.blade.php
4. social_links.blade.php
5. analytics.blade.php
6. payments.blade.php

Now after creating above les load the /admin/settings URL and
you will have a view something like below.

https://www.larashout.com/settings-section-part-2 14/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

Settings Section – Empty

Adding Setting Sections Views


Now we will create the subviews of our main settings page one
by one.

general.blade.php

Open your general.blade.php e and add the below code in this le.

<div class="tile">
<form action="{{ route('admin.settings.update') }}" method="POST" role="for
@csrf
<h3 class="tile-title">General Settings</h3>
<hr>
<div class="tile-body">
<div class="form-group">
<label class="control-label" for="site_name">Site Name</label>
<input

https://www.larashout.com/settings-section-part-2 15/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

class="form-control"
type="text"
placeholder="Enter site name"
id="site_name"
name="site_name"
value="{{ config('settings.site_name') }}"
/>
</div>
<div class="form-group">
<label class="control-label" for="site_title">Site Title</label
<input
class="form-control"
type="text"
placeholder="Enter site title"
id="site_title"
name="site_title"
value="{{ config('settings.site_title') }}"
/>
</div>
<div class="form-group">
<label class="control-label" for="default_email_address">Defaul
<input
class="form-control"
type="email"
placeholder="Enter store default email address"
id="default_email_address"
name="default_email_address"
value="{{ config('settings.default_email_address') }}"
/>
</div>
<div class="form-group">
<label class="control-label" for="currency_code">Currency Code<

https://www.larashout.com/settings-section-part-2 16/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

<input
class="form-control"
type="text"
placeholder="Enter store currency code"
id="currency_code"
name="currency_code"
value="{{ config('settings.currency_code') }}"
/>
</div>
<div class="form-group">
<label class="control-label" for="currency_symbol">Currency Sym
<input
class="form-control"
type="text"
placeholder="Enter store currency symbol"
id="currency_symbol"
name="currency_symbol"
value="{{ config('settings.currency_symbol') }}"
/>
</div>
</div>
<div class="tile-footer">
<div class="row d-print-none mt-2">
<div class="col-12 text-right">
<button class="btn btn-success" type="submit"><i class="fa
</div>
</div>
</div>
</form>
</div>

https://www.larashout.com/settings-section-part-2 17/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

This is a simple card view with a form inside. I am adding all


our general settings in this form. For each setting input block,
we are prepopulating the value usinf config() helper of Laravel
as our all settings are available globally. You can also use the
Config::get($key) or Setting::get($key) facades to get a getting value
inside your view.

Here is how our General section of settings will look like.

Settings Section – General

logo.blade.php

Open your logo.blade.php le and add the following code inside


this le.

<div class="tile">
<form action="{{ route('admin.settings.update') }}" method="POST" role="for
@csrf

https://www.larashout.com/settings-section-part-2 18/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

<h3 class="tile-title">Site Logo</h3>


<hr>
<div class="tile-body">
<div class="row">
<div class="col-3">
@if (config('settings.site_logo') != null)
<img src="{{ asset('storage/'.config('settings.site_log
@else
<img src="https://via.placeholder.com/80x80?text=Placeh
@endif
</div>
<div class="col-9">
<div class="form-group">
<label class="control-label">Site Logo</label>
<input class="form-control" type="file" name="site_logo
</div>
</div>
</div>
<div class="row mt-4">
<div class="col-3">
@if (config('settings.site_favicon') != null)
<img src="{{ asset('storage/'.config('settings.site_fav
@else
<img src="https://via.placeholder.com/80x80?text=Placeh
@endif
</div>
<div class="col-9">
<div class="form-group">
<label class="control-label">Site Favicon</label>
<input class="form-control" type="file" name="site_favi
</div>
</div>

https://www.larashout.com/settings-section-part-2 19/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

</div>
</div>
<div class="tile-footer">
<div class="row d-print-none mt-2">
<div class="col-12 text-right">
<button class="btn btn-success" type="submit"><i class="fa
</div>
</div>
</div>
</form>
</div>
@push('scripts')
<script>
loadFile = function(event, id) {
var output = document.getElementById(id);
output.src = URL.createObjectURL(event.target.files[0]);
};
</script>
@endpush

In this view we have added two le input elds for site_logo and
site_favicon . We have also added some javaScript to change the
placeholder image of each input when a new le will be
uploaded.

Here is how our Logo section of settings will look like.

https://www.larashout.com/settings-section-part-2 20/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

Settings Section – Logo

footer_seo.blade.php

Open your footer_seo.blade.php le and add the following code


inside this le.

<div class="tile">
<form action="{{ route('admin.settings.update') }}" method="POST" role="for
@csrf
<h3 class="tile-title">Footer &amp; SEO</h3>
<hr>
<div class="tile-body">
<div class="form-group">
<label class="control-label" for="footer_copyright_text">Footer
<textarea
class="form-control"
rows="4"
placeholder="Enter footer copyright text"

https://www.larashout.com/settings-section-part-2 21/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

id="footer_copyright_text"
name="footer_copyright_text"
>{{ config('settings.footer_copyright_text') }}</textarea>
</div>
<div class="form-group">
<label class="control-label" for="seo_meta_title">SEO Meta Titl
<input
class="form-control"
type="text"
placeholder="Enter seo meta title for store"
id="seo_meta_title"
name="seo_meta_title"
value="{{ config('settings.seo_meta_title') }}"
/>
</div>
<div class="form-group">
<label class="control-label" for="seo_meta_description">SEO Met
<textarea
class="form-control"
rows="4"
placeholder="Enter seo meta description for store"
id="seo_meta_description"
name="seo_meta_description"
>{{ config('settings.seo_meta_description') }}</textarea>
</div>
</div>
<div class="tile-footer">
<div class="row d-print-none mt-2">
<div class="col-12 text-right">
<button class="btn btn-success" type="submit"><i class="fa
</div>
</div>

https://www.larashout.com/settings-section-part-2 22/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

</div>
</form>
</div>

Here is how our Footer section of settings will look like.

Settings Section – Footer

footer_seo.blade.php

Open your social_links.blade.php le and add the following code


inside this le.

<div class="tile">
<form action="{{ route('admin.settings.update') }}" method="POST" role="for
@csrf
<h3 class="tile-title">Social Links</h3>
<hr>
<div class="tile-body">

https://www.larashout.com/settings-section-part-2 23/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

<div class="form-group">
<label class="control-label" for="social_facebook">Facebook Pro
<input
class="form-control"
type="text"
placeholder="Enter facebook profile link"
id="social_facebook"
name="social_facebook"
value="{{ config('settings.social_facebook') }}"
/>
</div>
<div class="form-group">
<label class="control-label" for="social_twitter">Twitter Profi
<input
class="form-control"
type="text"
placeholder="Enter twitter profile link"
id="social_twitter"
name="social_twitter"
value="{{ config('settings.social_twitter') }}"
/>
</div>
<div class="form-group">
<label class="control-label" for="social_instagram">Instagram P
<input
class="form-control"
type="text"
placeholder="Enter instagram profile link"
id="social_instagram"
name="social_instagram"
value="{{ config('settings.social_instagram') }}"
/>

https://www.larashout.com/settings-section-part-2 24/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

</div>
<div class="form-group">
<label class="control-label" for="social_linkedin">LinkedIn Pro
<input
class="form-control"
type="text"
placeholder="Enter linkedin profile link"
id="social_linkedin"
name="social_linkedin"
value="{{ config('settings.social_linkedin') }}"
/>
</div>
</div>
<div class="tile-footer">
<div class="row d-print-none mt-2">
<div class="col-12 text-right">
<button class="btn btn-success" type="submit"><i class="fa
</div>
</div>
</div>
</form>
</div>

Here is how our Social Links section of settings will look like.

https://www.larashout.com/settings-section-part-2 25/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

Settings Section – Social

analytics.blade.php

Open your analytics.blade.php le and add the following code inside


this le.

<div class="tile">
<form action="{{ route('admin.settings.update') }}" method="POST" role="for
@csrf
<h3 class="tile-title">Analytics</h3>
<hr>
<div class="tile-body">
<div class="form-group">
<label class="control-label" for="google_analytics">Google Anal
<textarea
class="form-control"
rows="4"
placeholder="Enter google analytics code"

https://www.larashout.com/settings-section-part-2 26/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

id="google_analytics"
name="google_analytics"
>{!! Config::get('settings.google_analytics') !!}</textarea>
</div>
<div class="form-group">
<label class="control-label" for="facebook_pixels">Facebook Pix
<textarea
class="form-control"
rows="4"
placeholder="Enter facebook pixel code"
id="facebook_pixels"
name="facebook_pixels"
>{{ Config::get('settings.facebook_pixels') }}</textarea>
</div>
</div>
<div class="tile-footer">
<div class="row d-print-none mt-2">
<div class="col-12 text-right">
<button class="btn btn-success" type="submit"><i class="fa
</div>
</div>
</div>
</form>
</div>

Here is how our Analytics section of settings will look like.

https://www.larashout.com/settings-section-part-2 27/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

Settings Section – Analytics

payments.blade.php

Open your payments.blade.php le and add the following code inside


this le.

<div class="tile">
<form action="{{ route('admin.settings.update') }}" method="POST" role="for
@csrf
<h3 class="tile-title">Payment Settings</h3>
<hr>
<div class="tile-body">
<div class="form-group">
<label class="control-label" for="stripe_payment_method">Stripe
<select name="stripe_payment_method" id="stripe_payment_method"
<option value="1" {{ (config('settings.stripe_payment_metho
<option value="0" {{ (config('settings.stripe_payment_metho
</select>

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

</div>
<div class="form-group">
<label class="control-label" for="stripe_key">Key</label>
<input
class="form-control"
type="text"
placeholder="Enter stripe key"
id="stripe_key"
name="stripe_key"
value="{{ config('settings.stripe_key') }}"
/>
</div>
<div class="form-group pb-2">
<label class="control-label" for="stripe_secret_key">Secret Key
<input
class="form-control"
type="text"
placeholder="Enter stripe secret key"
id="stripe_secret_key"
name="stripe_secret_key"
value="{{ config('settings.stripe_secret_key') }}"
/>
</div>
<hr>
<div class="form-group pt-2">
<label class="control-label" for="paypal_payment_method">PayPal
<select name="paypal_payment_method" id="paypal_payment_method"
<option value="1" {{ (config('settings.paypal_payment_metho
<option value="0" {{ (config('settings.paypal_payment_metho
</select>
</div>
<div class="form-group">

https://www.larashout.com/settings-section-part-2 29/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

<label class="control-label" for="paypal_client_id">Client ID</


<input
class="form-control"
type="text"
placeholder="Enter paypal client Id"
id="paypal_client_id"
name="paypal_client_id"
value="{{ config('settings.paypal_client_id') }}"
/>
</div>
<div class="form-group">
<label class="control-label" for="paypal_secret_id">Secret ID</
<input
class="form-control"
type="text"
placeholder="Enter paypal secret id"
id="paypal_secret_id"
name="paypal_secret_id"
value="{{ config('settings.paypal_secret_id') }}"
/>
</div>
</div>
<div class="tile-footer">
<div class="row d-print-none mt-2">
<div class="col-12 text-right">
<button class="btn btn-success" type="submit"><i class="fa
</div>
</div>
</div>
</form>
</div>

https://www.larashout.com/settings-section-part-2 30/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

Here is how our Payments section of settings will look like.

Settings Section – Payments

Saving Settings using Setting Controller


As our views markup is nalized, now we can head to
Se ingController and update the update() method with the
settings saving functionality. Open your Se ingController
controller and add update the update() method with the below
one.

/**
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request)
{
if ($request->has('site_logo') && ($request->file('site_logo') instanceof U

https://www.larashout.com/settings-section-part-2 31/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

if (config('settings.site_logo') != null) {
$this->deleteOne(config('settings.site_logo'));
}
$logo = $this->uploadOne($request->file('site_logo'), 'img');
Setting::set('site_logo', $logo);

} elseif ($request->has('site_favicon') && ($request->file('site_favicon')

if (config('settings.site_favicon') != null) {
$this->deleteOne(config('settings.site_favicon'));
}
$favicon = $this->uploadOne($request->file('site_favicon'), 'img');
Setting::set('site_favicon', $favicon);

} else {

$keys = $request->except('_token');

foreach ($keys as $key => $value)


{
Setting::set($key, $value);
}
}
return $this->responseRedirectBack('Settings updated successfully.', 'succe
}

Let’s go through with this method line by line. Firstly we are


checking if the Request has the site_logo and it is an instance of
UploadedFile class (means a le is uploaded) then before saving

https://www.larashout.com/settings-section-part-2 32/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

site logo, we are checking if there is already a site logo


uploaded already. If yes, delete it and then upload the logo
using uploadOne() method de ned in UploadAble trait.

When logo is uploaded it will resturn the path of logo with it’s
le name generated randomly, we are setting the site_logo vlaue
in database using the Setting facade.

Same thing we are doing for the site_favicon .

If there is no le uploaded, means form is submited other than


the logo.blade.php le, we are taking the whole $request except
_token and running a loop on them and adding the values to
database.

Finally, we are redirecting user back to the settings page using


responseRedirectBack() method de ned in the BaseController. Note
that how we are sending user back and setting the ash
message at the same time.

Conclusion
That’s it for now, in the next post we will start creating the
categories section for our e-commerce application.

Code Repository

You can nd the code base of this series on Laravel


eCommerce Application repository.

https://www.larashout.com/settings-section-part-2 33/73
2/13/2020 Laravel E-Commerce Application Development – Settings Section Part 2

If you have any question about this post, please leave a


comment in the comment box below.

88 comments on “Laravel E-Commerce Application


Development – Settings Section Part 2”

Ariful islam says:

MAY 29, 2019 AT 5:32 PM

Awesome! keep going 🙂

REPLY

nbn pk says:

JUNE 2, 2019 AT 1:53 PM

when is next coming?

REPLY

LaraShout says:

JUNE 2, 2019 AT 3:38 PM

It’s coming tomorrow. From this week will be posting on Monday,

Wednesday and Friday.

Thanks

https://www.larashout.com/settings-section-part-2 34/73

You might also like