I want to add custom_config.php
to app/config directory of my Laravel 5 project, but can't find any article explaining this process. How is this done?
6 | I created custom config file in Laravel 5 and try to use settings from it in other files ( How can I access to my settings from laravel config files? |
8 | First you need add the file in the config folder for example: laravel\config\Test.php
then you need to call the config
| ||||||||
|
3 | Why not just use the You would just have to set the value in your
and get it on each configuration file
Have a look at the helper code. | ||||||||||||||||
|
1 | We should also check the cache for custom config files added.
Check this link |
how to add custom config ?
PUBLISHED 2 YEARS AGO BY GANTODAY
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!
Best Answer(As Selected By gantoday)
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 includeSetting::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 useConfig::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;
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.']);
}
}
Modelnamespace 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'];
}
Requestnamespace 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());