How to get all atrribute labels in yii 1 - php

I have Brand table(it contains brand_id, brand_name, b_year ) and this code
`Products::model()->getAttributeLabel('brand_id')`
var_dump(Products::model()->getAttributeLabel('brand_id'));
It shows only brand_id label. How can I show all labels instead of one?

Try this
var_dump(Products::model()->attributeLabels());

Hii attributeLabels() is a function in Model. It return a array with database field as key.
to get all labels you have to just call it with no agruments like
var_dump(Products::model()->attributeLabels()); // this will return complete array
Where as getAttributeLabel is written in CActiveRecord and it expect and argument by definition it will not given all fields lablels

Please try this,
$lables = Products::model()->attributeLabels();
print_r($labels);
$lables returns all labels from Products model.

Related

Laravel mySQL view return by specific ID

So I am quite new to Laravel, and I have a situation, where I am trying to gather data from a pivot table (contains 2 foreign keys only) in order to retrieve data from other tables.
Before everything, I'd like to note, that word "campaign" is the same as "box". Simply it differs in database and front.
I have multiple boxes, that contains specific gifts.
I have set the URL of the box to be something as such: http://127.0.0.1:8000/box/1
http://127.0.0.1:8000/box/2
etc..
I have done so, by simply using a button with the {id}:
View the box
My plan is, to print out only that specific boxes gifts (right now, all boxes print out all gifts).
I have tried to use the ->where option within my function, although, it seems that I can't try equaling to the campaigns ID.
Incorrect code:
function box(){
$data = array(
'list'=>DB::table('campaigns_gifts')
->join('gift_items', 'gift_items.id', '=', 'campaigns_gifts.gift_foreignK')
->select('gift_items.*')
->where($campaign_foreignK = '{id}')
->get()
);
return view('DBqueries.boxView', $data);
}
My question is, how can I specifically return data, that only belongs to that specific box, since I am not able to use mysql where option.
For reference, these are the database tables:
Basically, I would need to match my URL's id with campaign_foreignK
Thank you in advance.
First of all, yout need to start to use Laravel Eloquent Models.
But doing by your way (the hardest):
You need to create a route in web or api, something like that:
Route::get('/box/{id}', [BoxController::class, 'view']);
Then you need to put this function on your controller:
function view($id){
/**
* You can do it by 2 ways:
* 1 - Do a where in the result of DB query (the bad way)
*/
$list = DB::table('campaigns_gifts')
->join('gift_items', 'gift_items.id', '=', 'campaigns_gifts.gift_foreignK')
->select('gift_items.*')
->where($campaign_foreignK = '{id}')
->get();
$list = (array)collect($list)->where('abc', 123);
/**
* Or the second way (the best is to use the Eloquent, but using DB the following is the best)
* 1 - Get the relations:
* Is git_items id the key for gift_foreignK ? i'm supposing that is it! so....
*/
$giftsIds = array_values((array)DB::select("select * from campaigns_gifts where campaign_foreignK = $id"));
$giftsIdsString = implode($giftsIds, ',');
$list = (array)DB::select("select * from gift_items where id in ($giftsIdsString)");
return view('DBqueries.boxView', ['list' => $list]);
}

Laravel. After call toArray(), the value in attribute property is changed

I want to get address data from customer table.
But my address data includes country, city and address.
Therefore I want to make address field to country, city and address three fields.
The following is my code
$result = Customer::select('customers.address AS country', 'customers.address AS city', 'customers.address')->get();
dump($result[0]); // First result
$temp = $result->toArray();
dd($temp[0]); // Second result
The following is my questions:
Why is country and city changed in attribute property? I didn't do anything.
It is magic. I change [country] to [countrys] and [city] to [citys].
The following is my new code
$result = Customer::select('customers.address AS countrys', 'customers.address AS citys', 'customers.address')->get();
dump($result[0]);
$temp = $result->toArray();
dd($temp[0]);
The countrys and citys didn't change in attribute property anymore.
The output picture:
Can anyone tall me why? It's really strange.
As per Laravel Documenation:
The toArray method converts the collection into a plain PHP array. If the collection's values are Eloquent models, the models will also be converted to arrays
Aslo
toArray also converts all of the collection's nested objects that are
an instance of Arrayable to an array. If you want to get the raw
underlying array, use the all method instead.
Laravel -> Collection -> toArray()
The following is my code to resolve my question [Why is country and city changed in attribute property?]
// Add the following code to Customer model
public function setCountryAttribute($value)
{
$this->attributes['country'] = $value;
}
public function getCountryAttribute()
{
return $this->attributes['country'];
}
public function setCityAttribute($value)
{
$this->attributes['city'] = $value;
}
public function getCityAttribute()
{
return $this->attributes['city'];
}

Dynamic accessing of the php list

I have 2 tables in the database employee(Ename,id,manager_id) and manager(name,id).1 manager has multiple employees under him.
The first line extracts all the employees under 1 manager and creates a list of it. Now, I wish to extract the name of each employee name from the retrieved employee id. How do i access each element of the list? This is what i have tried and it throws errors
$emplyeeId=DB::table('employee')->where('manager_id', $givenManagerId)->lists('id');
for ($i=0;$i<listCount;$i++)
{
$Ename = DB::table('employee')->where('id', $emplyeeId($i))-> value('Ename');
}
By looking at the docs (and scroll down a little bit, you find a method named pluck. This will return an array with all the values of the given column.
In your case this would be:
$names = DB::table('employee')->where('manager_id', $givenManagerId)->pluck('Ename');
// This will return the following array:
['Kevin', 'Tom', 'Tina', ...]
Laravel pluck method ,for example get just name from data
$names = DB::table('employee')->where('manager_id', $givenManagerId)->pluck('Ename');
Second method with array_filter
$names = DB::table('employee')->where('manager_id', $givenManagerId)->get()->toArray();
$justNames=array_filter($names, function ($ar) { return $ar['Ename']; });

Laravel Eloquent : Get object from database using JSON column key

I tried to use the following code to retrieve an entire subscription object from my subscription table using the testId column.
testId column is delclared as "json" type but in reality, the content of this column is an array with a single string as follow :
["51602a95-73d1-4c24-b3b3-eee288b427e4"]
I tried to get the subscription object with this code but ot doesn't work. How can i adpat this piece of code to get the subscription object by searching the value of testId into the array ?
function getSubscriptionByTestId($testId) {
$subscription = Subscription::where('testId', $testId)->first();
return $subscription;
}
If all the values are like ["51602a95-73d1-4c24-b3b3-eee288b427e4"] depending on your database engine you could use:
$subscription = Subscription::where('testId','like', "%$testId%")->first();
You can try the following, using a raw query. I'm not yet sure how to handle this with Eloquent, but will investigate further.
return DB::select(
"SELECT * FROM subscription
WHERE testId->\"$[0]\" = \"{$testId}\""
);

Populate Dropdown using database query results

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.

Categories