Issue retrieving id from mysql table after calculation in Laravel - php

I have a distance calculation query that works, i.e. it returns the closest points, but it simply yields an array of distances. What I want to do is retrieve the ids of the points stored in the array so I can access further data.
Here's the query:
$Eastings = $asset->Eastings;
$Northings = $asset->Northings;
$micromarket_size = 10000;
$competitors = DB::table('homes')
-> select(DB::raw('SQRT(POW('.$Eastings.' - `Eastings`,2) + POW('.$Northings.' - `Northings`,2)) AS distance'))
-> having('distance', '<', $micromarket_size)
-> get();
And trying to access the id like so:
$competitors->id;
yields an error trying to get property of non-object

Your select query is not requesting the id field:
->select(array('id', DB::raw('SQRT(POW('.$Eastings.' - `Eastings`,2) + POW('.$Northings.' - `Northings`,2)) AS distance')))

You get array of stdObjects using DB::table()->get(), thus if you want to retrieve only one model replace get() with first(). Otherwise get single element from array.

Related

returning all records with the same ID but different values

I've been trying this query
Model::find()
->innerJoin('TranslationTable', 'TranslationTable.model_id = Model.id')
->where(['IN', 'translation_code', $arrayOfTranslationCodes])
->asArray()
->all();
The translation table contains multiple rows with the same ID but with different translation codes.
This query only returns the first matching locale for a given ID. How would I retrieve the other translation codes for a given ID?
This is the solution that I came to:
$query = Model::find()
-> innerJoin('TranslationTable', 'TranslationTable.model_id = Model.id');
foreach ($arrayOfTranslationCodes as $translation)
{
$query->andWhere(['OR', 'translation_code', $translation])
}
$queryResponse = $query->asArray()->all();
This allowed me to find rows with the same id, but have different translations. You need to store the $query->asArray()->all(); as $query itself just returns the active query.

want to get difference between two values

i want to get difference between two values. i get my last entered value in bill no and get difference with input value.
this is my controller function model
$restPaymentSales = DB::table('sales')
->select('rest_payment')
->where('bill_no', $request->input('bill_no'))
->orderBy('created_at', 'desc')
->first();
$payment = $request->input('payment');
$doubleVal = doubleval($payment);
and i wrote calculation like this
$restSales = $restPaymentSales->child['rest_payment'] - $doubleVal;
but it shows me this error
Trying to get property of non-object
my table row like this
rest_payment -> double(11,2)
how can i fix this issue. basically i want to get difference between table last value order by date and input value.
Update your calculation from:
$restSales = $restPaymentSales->child['rest_payment'] - $doubleVal;
TO
$restSales = $restPaymentSales->rest_payment - $doubleVal;

Return column value in Laravel 5.2

I'm new to Laravel and here's my issue.
I have a table currentpercentage.
This is the structure of the table currentpercentage
currentpercentage(**id**, name, total_cap, current_usage, created_at, updated_at)
I'm trying to calculate percentage of current usage; based on total_cap and current usage.
total_cap = 1000,
current_usage = 237,
name = User Name
In my controller i've setup a query to get the value of total_cap and the value of current_usage then calculate that the percentage would be.
When i call my query, it returns an array with the column name (total_cap) and value (1000). Same as when i query for current_usage.
{
"currentpercentage": [
{
"total_cap": 1000
}
]
}
I just want the query to return just the number (1000) without the array.
This is my query
$totalcap = CurrentPercentageModel::select('total_cap')->where('name', '=', 'User Name')->get();
How do I just get the value. Or is there an easier way to calculate the percentage from one query?
CurrentPercentageModel //What I use to connect to the database. DB Model
The problem is that you are using the get method which returns a collection even when you only have one row.
$totalcap = CurrentPercentageModel::select('total_cap')->where('name', '=', 'User Name')->get();
If you just want one record and one column value, then use the value method to get just the value of the column, more info here (you might have to scroll a little bit)
$totalcap = CurrentPercentageModel::select('total_cap')->where('name', '=', 'User Name')->value('total_cap');

Laravel eloquent issue getting db datas

I'm having a problem getting some datas from my database via eloquent:
at first I do this to get 'id' and 'capMax' from clase_schedule table:
$plazas = DB::table('clase_schedule')->select(['schedule_id', DB::raw('SUM(capMax) as capMax')])->groupBy('schedule_id')->get();
and then i want to get the the name of table schedule where the id i got before coincides with the one on this table, like this:
$nom_horarios = DB::table('schedule')->select('name')->where('id', 'in', $plazas->schedule_id)->get();
I get this error:
"Trying to get property of non-object"
Since, $plazas is an array so you should loop the $plazas to get the value in next query as below :
foreach ($plazas as $plaza)
{
$nom_horarios[] = DB::table('schedule')->select('name')->where('id', 'in',$plaza->schedule_id)->get();
}
print_r($nom_horarios);
$plazas returns an array of items, instead of get(), use first(). This will return single item.
$plazas = DB::table('clase_schedule')->select(['schedule_id', DB::raw('SUM(capMax) as capMax')])->groupBy('schedule_id')->first();
Or loop plazas in foreach
What is returning in the $plazas variable?
I think the problem is you are getting a collection and not a item... If you want to get just one plaza you should ->first() instead of ->get();

Select MySQL If (ID) In Array

So I trying to build a notification system (CodeIgniter) and not store it in my database by this unique ID. Now I got some problem to using `SELECT query.
Also this is array stored in "value" row:
[{"id":0,"user_id":"1","comment":"That's a Nice Video.","posttime":1403523177,"status":1},{"id":1,"user_id":"4","comment":"Nice to see this..!!","posttime":1403590409,"status":1}]
And this is my (not work) query:
$query = $this->db->get_where('post_meta',array('status'=>1),in_array('user_id', $id));
Ideas?
Note: Notification will be sparated by "user_id".
You should not use in_array('user_id', $id) on that function because it returns boolean.
On the active record page: https://ellislab.com/codeigniter/user-guide/database/active_record.html you can take a look at parameter it takes for get_where() function
$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
Notice how the third parameter takes $limit which talks about the number of data you'll receive. (Leaving this blank will return you all data).
Some code examples:
If you just want to get the data with status = 1, use the following:
$query = $this->db->get_where('post_meta',array('status'=>1));
If you want to get the data with status = 1 and user_id = $id, use the following:
$this->db->like('value', '"user_id":"'.$id.'"');
$query = $this->db->get_where('post_meta',array('status'=>1));
The solution above is not the best, but it should work.
The $this->db->like() function will create rules to get data in value row which has "user_id":"$id" where $id is the value that you define.
What if I want to get all notifications and group them based on their ID?
I usually get all the data and then use PHP to group them into its own array. I think it's cheaper than relying on database to do that (Correct me if i'm wrong).
So use the first code:
$query = $this->db->get_where('post_meta',array('status'=>1));
Then iterate them using foreach() or whatever you find convenient.
$data = array();
foreach($query as $k=>$v) {
// Run a function to get User ID
$user_id = your_function_here($v->value);
if(!isset($data[$user_id]))
$data[$user_id] = array();
// This is just some example to group them together, you can optimize it to your own liking
array_push($data[$user_id],$v);
}
your_function_here() is your function to get the user_id from value row on your database.

Categories