简体   繁体   中英

Laravel 5.7 Eloquent

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

Link to Laracast

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.

Eloquent Query :

$options = DB::table('options')->get();

get all info using this code:

@foreach($options as $option)
     {{ $option->site_name }}
     {{ $option->option_value }}
@endforeach

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM