Reputation: 531
hello im trying to get substract value in controller to pass in view using two different table.
im getting error
ErrorException
Object of class Illuminate\Database\Eloquent\Collection could not be converted to number
my table
table_a and table_b
table_a columns selected "price" //discount $100 is percent in table_a
table_b columns selected "discount" //discount $5 is percent in table_b
my controller
public function index()
{
$table_a = TABLE_A::get(['price']);
$table_b = TABLE_B::get(['discount']);
$substract = $table_a - $table_b ;
return view('Home', compact('substract' ));
}
view
<b> final value </b>
<h1> {{ $substract}}</h1> //value need to get $95
Upvotes: 0
Views: 73
Reputation: 1405
Your use case is odd, but this should work to fix your code.
public function index()
{
$table_a = TABLE_A::first();
$table_b = TABLE_B::first();
$substract = $table_a->price - $table_b->discount ;
return view('Home', compact('substract' ));
}
If you want instead to sum all values in every table you can do
public function index()
{
$table_a = TABLE_A::sum('price');
$table_b = TABLE_B::sum('discount');
$substract = $table_a - $table_b ;
return view('Home', compact('substract' ));
}
Upvotes: 1
Reputation: 10254
You must understand some basic Laravel to see that you are doing a wrong thing.
Model::get()
returns a Collection (that may or not have items)contains
,find
, map
, filter
, etc.That said, you have a few conception problems:
Table_A::first()->price - Table_B::first()->discount
Upvotes: 1