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;
Related
I am trying to select records from my MSSQL database, with below parameters:
Column 'first_etd' must be between two dates
Column 'car_id' must not be the same as the current $carId variable.
This is my code:
$carId = 47;
$from = Carbon::now()->subWeeks(2)->startOfWeek()->toDateString();
$to = Carbon::now()->addWeeks(4)->endOfWeek()->toDateString();
$consols = Consol::with(['car'])
->where('car_id', '!=', $carId)
->whereBetween('first_etd', [$from, $to])->get();
The above variable $consols returns no results.
If I remove the ->where('car_id', '!=', $carId) from the statement, I successfully get results.
All the records in my database currently have NULL in the car_id column:
I have also tried to change the != operator to <> without any luck.
So this is not a fix to the SQL statement itself, but more a fix to my problem.
As said in my OP, I can find the two records if I remove the where() method. I figured I could start by getting the records, that's between my two dates and then do the where() filtering on the result collection.
This works:
//Get the records from the database, that's between two dates.
$consols = Consol::with(['car'])->whereBetween('first_etd', [$from, $to])->get();
//Now $consols is a Laravel collection, so I can use the where() method here.
$consols = $this->consols->where('car_id', '!=', $carId);
Above successfully returns the two records.
I have this code in Laravel:
DB::table('items')
->whereRaw("? = 1", ['active'])
->get();
In my database table, I have a column named active and the query I want to run is:
SELECT *
FROM items
WHERE active=1
My code fails because the query passes my 'active' parameter as a String instead of a column name in SQL syntax (which is the expected behavior).
So, instead of the above, I get something like this:
SELECT *
FROM items
WHERE "active"=1
Any idea how to solve this?
PS: I tried the MySQL function TRIM but with no success (perhaps I did not do it correctly).
It is not the cleanest way;
$day = 'Monday'; // dynamically Tuesday, Wednesday....
$method = 'where' . $day;
return DB::table('items')->$method('1')->get();
I'm beginner in laravel and I'm trying to run comparison queries given in the database.
I saved a field date that is implemented by a form together with other fields including the name.
I tried to query the name and it works all regularly with this code below.
I would like to retrieve all the rows that have the name variable as the field name that I pass (and here it seems to work) and then only those with the field date that have the specified month at the number that I pass as variable $month.
what would be the right form to do this?
thanks
Piero
public function filterparamenter(){
$name = request('name');
$month = request('$month');
$query = subagente::all();
$query = $query->where('subagente', $subagente);
$query = $query->whereMonth('data', $month)->get();
Method Illuminate\Database\Eloquent\Collection::whereMonth does not exist.
Using ::all() returns a Collection, which has a ->where() method, but ->whereMonth() is only available on Eloquent's Builder class. Change your code as follows:
$query = subagente::query();
$query = $query->where('subagente', $subagente);
$query = $query->whereMonth('data', $month)->get();
Or, more compact:
$results = subagente::where("subagente", $subagente)
->whereMonth("data", $month)
-get();
Using ::query() or ::where() to start your query will generate a Builder instance, which you can chain addition clauses (->where(), ->whereMonth(), etc) on before calling ->get() to return a Collection of subagente records.
Side note, should "data" be "date"?
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');
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.