Reputation: 13
So, I'm having some issues with getting some information from my database in Laravel. The thing is, I want to have a site settings table, with some global defaults.
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| option_name | varchar(255) | NO | | NULL | |
| option_value | longtext | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
Like this.. How do I get all the info with Laravels Eloquent?
select * from `options`;
> 1 | site_name | mysite.com
I want to be able to do
$options->site_name
in my controllers and views. Can't remember what this kind of thing is called, so I can't really search for it.. :(
EDIT:
Model class after request
namespace App;
use Illuminate\Database\Eloquent\Model;
class DNBOptions extends Model
{
protected $table = 'dnb_options';
protected $primaryKey = 'option_name';
protected $fillable = [
'option_name', 'option_value'
];
}
EDIT 2:
Got answer on Laracast to use ->pluck() and cast to an (object) to get the result I was looking for.
$option->site_name
Upvotes: 0
Views: 523
Reputation: 5180
First create the Model. You can do it manually or with artisan
in terminal :
php artisan make:model Option
This will create App\Option
class. Then configure the Model in app/Option.php
:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Option extends Model
{
//
protected $table = 'options';
}
Now in any controller like App\Http\Controllers\AnyController
you can use code :
use App\Option;
// Other codes ...
public function show($id) {
$option = Option::find($id);
echo $option->option_name; // print "site_name"
}
EDIT AFTER OP GIVE NEW MODEL -------------
So if you use model :
namespace App;
use Illuminate\Database\Eloquent\Model;
class DNBOptions extends Model
{
protected $table = 'dnb_options';
protected $primaryKey = 'option_name';
protected $fillable = [
'option_name', 'option_value'
];
}
then in controller or view use :
$option = \App\DNBOptions::find('site_name');
echo $option->option_value;
// Get all
$options = \App\DNBOptions::all();
foreach($options as $option) {
echo $option->option_name;
echo $option->option_value;
}
// Print 1st
echo $options->first()->option_name;
// With where
$option = \App\DNBOptions::where('option_name', 'site_name')->first();
echo $option->option_value;
A full Controller - View example :
<?php
namespace App\Http\Controllers;
use App\DNBOptions;
class OptionController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$options = DNBOptions::all();
$data ['options'] => $options;
return view('options.index', $data);
}
}
And the View class is resources/views/options/index.blade.php
:
@extends('layouts.app')
@foreach($options as $option)
{{ $option->site_name }}
{{ $option->option_value }}
@endforeach
Use @extends
if you have a layout template. And See Laravel Documentation for further information.
Upvotes: 1
Reputation: 2683
In your App/Http/Controllers/Controller.php
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected $options;
public function __construct()
{
$this->options = (object)DNBOptions::pluck('option_value', 'option_name')->toArray();
\View::share('options', $this->options);
}
}
In views:
{{ $options->site_name }}
In controllers:
$this->options->site_name
Upvotes: 0
Reputation: 1583
In Controller first add model name space:
use App\Option;
....
....
public function methodName($id) {
$data['options'] = Option::all();
return view('index',$data);
}
view located in resources/views/
directory wich name is index.blade.php
or in blade :
@foreach($options as $option)
{{ $option->site_name }}
{{ $option->option_value }}
@endforeach
Upvotes: 0
Reputation: 4285
DNBOptions::where('option_value', 'mysite.com')->get();
https://laravel.com/docs/5.7/eloquent#retrieving-models
https://laravel.com/docs/5.7/eloquent#query-scopes
Upvotes: 0
Reputation: 775
Eloquent Query :
$options = DB::table('options')->get();
get all info using this code:
@foreach($options as $option)
{{ $option->site_name }}
{{ $option->option_value }}
@endforeach
Upvotes: 0