You can create a CRUD environment for settings or use a settings while which you have to modify though.
What have you tried?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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!
You can create a CRUD environment for settings or use a settings while which you have to modify though.
What have you tried?
@blackbird
sorry,i'm a newbie,and i don't know what you means.could you give me some example? many thanks!
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?
@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.
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');
@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.
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
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 ;)
@blackbird
you are good man. └(^o^)┘
If you need any help, you know where to find me ;)
Controller
namespace App\Http\Controllers;Modeluse 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.']); }}
namespace App;Requestuse Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes;
class Setting extends Model {
use SoftDeletes; protected $fillable = [ 'name', 'value' ]; protected $dates = ['deleted_at'];}
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.
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?
@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?
@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.
@ganto you can also do it like this:
return view('settings.index')->with('settings' => Setting::all());
or
return view('settings.index')->withSettings(Setting::all());
$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.
@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.
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
oh, i click wrong button.how to cancel the right answer.
You can do Setting::all() or Setting::get('your.setting') in every view
You can click on another asnwer and it will change then ;)
@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.
as i said, both sides have their pros and cons - it wasn't meant for or against you or blackbird
@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?
@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!
@frezno @blackbird @zeeshan
code for fun,i hope you all get along well.
forgive me bad english.
So you basically want a variable that you can always access? That's not what you asked in the beginning! Take a look at view composers: http://laravel.com/docs/5.0/views#view-composers
@blackbird This is what I want. sorry,i'm poor in laravel and english.
@ganto Give it a try and if you are stuck let us know ;) It's all in the documentation
@blackbird ;)
Please or to participate in this conversation.