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.
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;
Could you use ->first() to select the first value from the returned set?
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);
}
Related
My controller looks like this:
public function show($id)
{
$model = MyModel::with([
'model2.model3.model4:id,value',
...
]);
if (myCondition) {
unset($model->model2->model3->model4);
$model->model2->model3->model4 = Model4::where('value', 'Some Value')->first();
}
return $audit;
}
In certain condition I'd like to override the result from the query with another value from the Model4 to return the good data to the client.
But I want to know if there is another way with laravel to do that. Actually I have to use unset and then push the new content if I want to change the value of the model4 property. If I don't use unset the object isn't changed, the value new value assigned to model4 is ignored I don't know why I can't just write this line
$model->model2->model3->model4 = Model4::where('value', 'Some Value')->first();
So I want to know why I can't see changes in my json object when I don't use unset and I want to know if there is anotehr way to deal with laravel for my situation ?
You can simply use setRelation method.
if ($myCondition) {
$model->model2->model3->setRelation('model4', Model4::where(...)->first());
}
I've a very big doubt about how works laravel for a very simple thing:
If I call:
$companies=User::All();
Then I can use statement like this in a forach:
foreach($companies as $company)
$company['new_field']= 'something';
If i'm limiting the output of the query like:
$companies = DB::table('companies')
->select('id','name','email','business_name',...)->get();
The things doesnt work as before,
I try with or without the ->get()
I try to convert with ->toArray() (errors rised)
I try with put() and push() for collections method and agains errors...
How can I add a field in every item of the collection just to pass it to a view?
Try like this, hope it works for you:
$users=User::select('id','name','email','business_name',...)->get()->toArray();
and then use foreach loop like this:
foreach($users as $key => $value ){
$users[$key]['newField'] = "Demo";
}
If you are using Laravel and model in it so there is a better way to add custom attribute or field here is what i do for custom field
For Example :
There is a Model Name User
so in User Model
add a property name appends like :
class User extends Model
{
protected $appends = ['new_field'];
public function getNewFieldAttribute() // defining field logic here
{
return // your code
}
So you no need to use foreach and looping and adding new field
for more have a look on laravel doc : https://laravel.com/docs/5.5/eloquent-mutators#accessors-and-mutators
Suggestion
you can limit your output with Model too.
User::select('id','name','email','business_name',...)->get();
if you are making an array like
User::select('id','name','email','business_name',...)->get()->toArray();
so this will also give you your custom field
i am using Fat-Free Framework to do rapid prototyping of my application. Now, whenever I try to load some data from database, i can use the load() function within the SQL\Mapper but it returned all of the column.
I found SELECT() function but it does not returning any data.
$this->load(['myId=?',$id]) will return the data along with the other columns
$this->select('name',['myId=?',$id]) should return the data from name column but i got nothing.
$this->db->exec('SELECT name FROM persons WHERE myId=?',$id) will return the the data from name column.
what is the proper way of using SELECT() from Fat-Free framework? my goal is to only retrieve single data from name column only.
The right way to do it is like this:
$table = new DB\SQL\Mapper($db, 'persons');
// assign to $results
$results = $table->load(array('myId=?', $id));
foreach($results => $row){
echo $row->name;
}
As described here: https://fatfreeframework.com/3.6/databases#SeekandYouShallFind
I have used following code:
public function verify($id,$sid)
{
$sfees = sfee::where('student_id', $sid)->first(['mfee_id']);//mfee_id is the column I'm trying to recover
foreach ($sfees as $sfee) {
echo $sfee;
}
The problem is that it is returning three 1. While it was supposed to return only one.
If i echo $sfees before foreach it returns only one value: {"mfee_id":1}.
What is the problem? Can anyone help me?
You are asking for one model, you don't need to try and iterate it, its a single model (object). (first() can return null though, so you should check)
$sfees->mfee_id;
If you just want the value of that column for that one record:
$mfee_id = sfee::where('student_id', $sid)->value('mfee_id');
Laravel 5.2 Docs - Query Builder - Retrieving Results - Retrieving A Single Row / Column From A Table
You should use return statement for returning from function, not echo.
public function verify($id,$sid)
{
$sfees = sfee::where('student_id', $sid)->first(['mfee_id']);//mfee_id is the column I'm trying to recover
return $some_variable;
}
Your problem is that that your trying to itterate over a single model. That's cause that you itterate over attributes and you get odd result the only think that you need is to do like this:
public function verify($id,$sid)
{
$sfees = sfee::where('student_id', $sid)->first(['mfee_id']);//mfee_id
echo $sfees->mfee_id;
}
$sfees in this example is a single model not a collection of models becasuse you've used first to get it.
First and foremost, i'm very green in Laravel. So pls take that into consideration if i use any wrong terminlogy or dont grasp some stuff that should be obvious to you all.
I'm trying to populate a dropdown list with the results of database query.
Model for Dropdown Entries:
Class Dropdown {
public static function getFilters() {
$filter = DB::select(DB::raw('SELECT DISTINCT filter AS filt
FROM filter.database'));
return $filter;
}
}
Controller:
class FilterController extends BaseController {
public function getSql_Test() {
$dropdown = Dropdown::getFilters();
return View::make('Dropdown.show', ['Dropdown' =>$dropdown]);
}
}
What my controller accomplishes right now is to display the results of the query (which i want to populate the dropdown) on the page. Can someone pls help me on how to pass this entries onto the select tag here and show it on my View?
That is very easy to do, and fun (cause its easy)!
$filter = DB::table('filter.database')->select(DB::raw('DISTINCT(filter) AS filt'))
->lists('filt');
This will give you an array where the key is an integer and the value is your filter.
$filter = DB::table('filter.database')->select(DB::raw('DISTINCT(filter) AS filt'))
->lists('filt', 'filt');
This will give you an array where the key is filt and the value is filt.
you can push it into the view by extending your view with the variable like so:
return View::make('Dropdown.show')->with('dropdown', $dropdown);
Now you can make your select and just push in the array where the value should be:
echo Form::select('size', $dropdown);
More information can be found here:
http://laravel.com/docs/4.2/queries#selects
As a tip, stay on the laravel docs. They are a treat to work with.