1

I'm currently trying to create my settings page by creating a table settings with company_name column. My idea is to fetch the single value company_name and print it in the title of the pages and where the name is going to be appearing. I just am not sure how to convert the array return from the DB::select to be able to print it correctly.

The code:

public function __construct()
{ 
    $company_name = DB::table('settings')->select('company_name')->get();

    return View::share('company_name', $company_name);
}

And print it in the blade system as {{ $company_name }}, although it can't convert an array to string.

3 Answers 3

1

Either use eloquent (that I would recommend) which would be like this :

$company_name = Setting::first()->company_name;

or using fluent :

$company_name = DB::table('settings')->company_name;
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I was actually close to that one using Eloquent with $company_name = Setting::where('id', '=', '1')->firstOrFail(); My only concern now is that it prints as: {"company_name":"NOMBRE DE EMPRESA"}
0

Could you use ->first() to select the first value from the returned set?

2 Comments

And if you have more than one entry in your settings, you can perhaps use ->where($id, $_COOKIE["SessionId"]) and add a SessionID column to your table.
That returns with a "Object of class stdClass could not be converted to string"
0

I think you should re-think your DB schema.

But if you want an answer to keep working with you current schema:

DB::table('settings')->select('company_name')->first();

This can throw an Eloquent ModelNotFound exception.

EDIT

public function __construct()
{ 
     $result = DB::table('settings')->select('company_name')->first()->toArray();

     return View::share($result);
}

5 Comments

Unfortunately that doesn't work either. It actually returns with an errorBadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::firstOrFail()
You're right, @LucasGomez! I learnt something today! The query builder doesn't have that method. One more reason to use Eloquent. Use the ->first() method instead.
Yup, I was reading it works only when using Eloquent models, which I actually am, for this settings (update mostly). Using first(); returns "Object of class stdClass could not be converted to string"
Went ahead and made it like this: $company_name = Setting::where('id', '=', '1')->firstOrFail(); It works now, but it prints the whole thing: "{"id":1,"company_name":"Sabores No Colombianos","created_at":"0000-00-00 00:00:00","updated_at":"2014-04-14 15:25:32"}"
@LucasGomez Try Setting::where('id', '=', '1')->select('company_name')->firstOrFail();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.