When I tried using alias using select with eloquent, it has given me weird results
my code:
$caseStudy = CaseStudy::find($id)->select('title_case as title','description_case as description')->get();
It returned collection. My question is to find model and change the column names. How to do it?
The find function can take an array as a second parameter.
In this array you can list the fields you need.
$caseStudy = CaseStudy::find($id, ['title_case as title','description_case as description']);
This way you should be able to access your aliases:
$caseStudy->title
Instead of:
$caseStudy->title_case
try using this
$caseStudy = CaseStudy::select(DB::raw('title_case as title','description_case as description'))->find($id)->get();
Related
Database table SITE has many columns. One of them is site_id. I need all the site_ids as an array since it has to be fed to a method which accepts only a string array.
What I tried so far is:
$sites = DB::select('select site_id from site_tab');
$sites_arr = $sites->toArray();
But this doesn't produce the result I want. I need $sites_arr to be like ['A','B','C',...]
Please suggest a way to get this done. A solution based on Eloquent is also OK for me.
Thanks
Try this:
DB::table('site_tab')->pluck('site_id')->toArray();
reference pluck
referen toArray
If you open a manual, you will see that
The select method will always return an array of results
So, there's no need to use ->toArray(), as result is already an array.
To get values as array of names you can do:
$site_ids = DB::table('site_tab')->pluck('site_id');
Using ->toArray() here is optional, as you can iterate over $site_ids (which is a Collection) with a foreach too.
Im using laravel 5.2 and I want to group a table based on the value of two columns (configuration and type). So basically if two or more rows in the table have the same configuration and type they will be grouped.
I've tried querying like this and its not working:
$groups = Model::where('active', 1)->get()->groupBy('type_id', 'configuration_id');
Any suggestions?
EDIT: I think I'm using the wrong query. What I want to do is get the rows that have the same values in these two columns and have them grouped. Apparently group by is used to aggregate things e.g if I want a sum or something.
EDIT 2: Actually the groupby here I'm using is not part of the query it is a helper function provided by laravel to use on collections see this link Does anyone know if it groups by two feilds??
Ok I've figured it out.
The groupBy method I'm using is not a query it is a laravel function that groups collections by a certain field. You can see the documentation here. So the workaround was to do the grouping twice like this.
$groups = Model::where('active', 1)->get()->groupBy('type_id');
foreach ($groups as $group) {
$grouped[] = $group->groupBy('configuration_id');
}
Pass Columns names in array form this is working for me
$groups = Model::where('active', 1)->groupBy(['type_id', 'configuration_id','etc']);
$groups = Model::where('active', 1)->groupBy('type_id', 'configuration_id')->get();
Try like this
$groups = Model::where('active', 1)->groupBy('type_id', 'configuration_id');
$groups = Model::where('active', 1)->groupBy('type_id', 'configuration_id')->get(['type_id', 'configuration_id']);
I need to eliminate the specific prefix of a string by default on getting value from the database.
In MySQL, i can use the following,
SELECT RIGHT('abc3',1) -- Results in "3"
SELECT RIGHT('abc3',2) -- Results in "c3"
But, how can i use same process in Laravel eloquent?.
Or any other solutions are available for remove the prefix of a string while retrieve from database in laravel.
I know trim will eliminate, but only spaces.
ex.
property_color
property_size
Here i need to extract "property_".
expect.
color
size
Is it possible in laravel, in without using PHP String function.
Only on Direct eloquent Operation.
Thanks in Advance !
That's what I would do:
$arrayData = DB::select(DB::raw("SELECT RIGHT('abc3',1) ")):
you can pass array of parameter to bind values:
DB::select(DB::raw(" SQL QUERY "),$paramsArray);
You have to use raw queries within your builder like
$results = YourRepo::where(DB::raw("SELECT SUBSTRING('property_color',9)") , 'LIKE', "%property_xxx%");
keep in mind that substring is slow.
I have two columns in my table: max and current. I want to build simple scope
public function scopeNotMax($query)
{
return $query->where('max', '>', 'current');
}
But Laravel gives me that query:
SELECT * FROM table WHERE `max` > 'current'
I don't want this result and I know that I can use in this place whereRaw() or DB::raw(). But I can't find another way to say "hey, this is column, not string!'. Can I do it? Or I must use raws? I want to avoid it.
There is no other way.
where() method in this case add third parameter (value) to bindings and passes it ot PDO library. So it will be escaped.
Alternatively You can pass as third parameter a Closure, but then laravel will form a sub-select for You, which doesn't helps much.
Looks like whereRaw() is made for this kind of sitiuation.
Did you give a try with this ? return $query->where('max > current');
you can use whereRaw():
->whereRaw('table_1.name = table_2.name');
You exmaple code:
->whereRaw('max>current');
I am trying to get all values of a column, and then use it in another query. I used lists and got the values as an array but, I couldn't find the way to check that variable (array containing each id) in other query
$subscribes = Subscribe::where('from_id', $currentUser)->lists('id')
// dd($subscribes), logs values as array.
$videos = Photo::where('id', $subscribes)->get();
This doesn't work because $subscribes is an array.
Should I use a for loop and send another query for each id? Or is there a practical way that I am missing out? What is the proper way of using it?
Use whereIn method:
$videos = Photo::whereIn('id', $subscribes)->get();
http://laravel.com/docs/5.1/queries (scroll down / search 'whereIn')
Apply whereIn method
if(is_array($subscribes) && count($subscribes) > 0){
$videos = Photo::whereIn('id', $subscribes)->get();
}