Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gantoday's avatar

how to add custom config ?

i want to controller some configs in my site dashboard
such as how many items per page,name of site...
i wish could controller them in my site dashboard,not to modify files or change values in database manually.
how to realize it? please help me,thanks!

0 likes
31 replies
bobbybouwmann's avatar

You can create a CRUD environment for settings or use a settings while which you have to modify though.

What have you tried?

gantoday's avatar

@blackbird
sorry,i'm a newbie,and i don't know what you means.could you give me some example? many thanks!

bobbybouwmann's avatar

Well you want to change some settings right? Where do you want to change that? What have you tried? What do you have now that needs to be changed?

gantoday's avatar

@blackbird
i want to change something in my site dashboard. i mean controller some setting in my dashboard.
i hope could controller such as site name,how many items per page shows in my dashboard.
i have tried stored settings in my database,but every method use setting values should include
Setting::all();
and than compact it to views.
i hope could use my custom config as laravel's build in config.stored a custom config file in config directory and get setting values use
Config::get('xxx');
forgive me poor english.

bobbybouwmann's avatar

You can just create a new file in the config folder and use that

config/settings.php

<?php 

return  [
 
    'pages' => 5

];
Config::get('settings.pages');
12 likes
gantoday's avatar

@blackbird
wow,many thanks.
and could you tell me what is the best way to realize site setting ?
does other users of laravel use this way to realize controller settings in site dashboard?
maybe sometimes files in config directory couldn't change for secure purpose.

bobbybouwmann's avatar

You can actually do this in your controller and output it on the page. Just a simple example:

class MyController extends Controller {

    public function index()
    {
        $settings = Config::all();
        return view('settings.index', compact('settings'));
    }

}

In your view

@foreach ($settings as $settingsarray)

    <ul>

    @foreach($settingsarray as $setting)
        
        <li>{{ var_dump($setting) }}</li>

    @endforeach

    </ul>

@endforeach
bobbybouwmann's avatar

Most developers would store the settings in the database. So you would create a new controller called SettingsController and a Setting model to do this. Then your controller would get all the data from the database and display it on the page ;)

1 like
zeeshan's avatar

Controller

namespace App\Http\Controllers;

use App\Http\Controllers\Controller; use App\Setting; use App\Http\Requests\SettingRequest;

class SettingsController extends Controller {

public function index() {

    $settings = Setting::oldest("name")->paginate($this->pagesize);
    return view("admin.settings.index")->with('settings', $settings);
}

public function create() {
    return view("admin.settings.create");
}

public function store(SettingRequest $request) {

    Setting::create($request->all());
    return redirect("admin/setup/settings")->with(['success' => 'New Setting created successfully.']);
}

public function edit($id) {

    $setting = Setting::findOrFail($id);

    return view("admin.settings.edit")
                    ->with('setting', $setting);
}

public function update($id, SettingRequest $request) {

    $setting = Setting::findOrFail($id);
    $setting->update($request->all());
    return redirect("admin/setup/settings")->with(['success' => 'Setting updated successfully.']);
}

public function destroy($id) {

    $setting = Setting::findOrFail($id);
    $setting->delete();
    return redirect("admin/setup/settings")->with(['success' => 'Setting deleted successfully.']);
}

}

Model
namespace App;

use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes;

class Setting extends Model {

use SoftDeletes;

protected $fillable = [
    'name',
    'value'
];
protected $dates = ['deleted_at'];

}

Request
namespace App\Http\Requests;

use App\Http\Requests\Request;

class SettingRequest extends Request {

public function authorize() {
    return true;
}

public function rules() {

    return [
        'name' => 'required',
        'value' => 'required'
    ];
}

}

Unfortunately I can not paste the blade templates here. I hope you would be able to create the form etc.

bobbybouwmann's avatar

How is this gonna help ganto? I bet he/she doesn't know what a Request object is.. You add paginate, but does he/she really need it?

gantoday's avatar

@zeeshan
thank you very much.
but if i use SettingsController to setting name of the site,i should add $settings=Setting::all(); and compact('$setting') every times i use views.
does there has easy way to do that?

zeeshan's avatar

@blackbird thats why I added whole classes which include the namespace (which may help him where to copy) and also has all the the use statements so he does not get confused.

Thats my way of sending help. If he does not understand he will come back to here and ask. if its not helpful to him he may not use it. Simple.

bobbybouwmann's avatar

@ganto you can also do it like this:

return view('settings.index')->with('settings' => Setting::all());

or

return view('settings.index')->withSettings(Setting::all());
zeeshan's avatar

@ganto:

$site_name =  DB::table('settings')->where('name', 'site_name')->get();
you can write a small function in model which takes a setting name and returns the value.
gantoday's avatar

@blackbird
i mean does there have a way that type one time return view('settings.index')->with('settings' => Setting::all()); and use $settings eveywhere?
@zeeshan @blackbird thank you all.all of you are helpful to me.

frezno's avatar

@zeeshan @blackbird

the good old question 'how much help is really a help'

Is it better to point the user to the right direction and make him to get the final result by himself (and thus learning) or do i give him a (more or less) copy & paste solution.

Both ways have their pro and con

gantoday's avatar

oh, i click wrong button.how to cancel the right answer.

bobbybouwmann's avatar

You can do Setting::all() or Setting::get('your.setting') in every view

zeeshan's avatar

@frezno It depends: when you have exactly the same solution already implemented you would like to share to save others time. Thats why I use open source technologies. And I am many time used the code sent by others and it worked perfect.

frezno's avatar

as i said, both sides have their pros and cons - it wasn't meant for or against you or blackbird

gantoday's avatar

@blackbird i mean does there have a way that type one time
return view('settings.index')->with('settings' => Setting::all());
and use $settings evey views i need $setting?
think if i want to get value of site name,which is used in every views.so i should type those code every method use view?

zeeshan's avatar

@frezno @blackbird no issue guys. I am also new to community so would learn who to effectively help others. Laravel is something I would like to use for next couple of years so hopefully we will see each other around and will help each other effectively sort out problems. Cheers!

Next

Please or to participate in this conversation.