How desplay the number from the Code in blade Laravel - laravel-5

Show only the variable number in the blade from the $sum value
In Blade:
{{$sum}}
In controller:
$sum = OrderPosition::select(DB::raw('sum(quantity*product_price) AS total_sales'))->where('quantity', '<>', 1)->get();
the output is {"total_sales":"550.00"}

You are trying to display the object. Try this in blade
{{$sum->total_sales}}

Related

Show collection of data using while loop

How can I show collection of data using while loop in blade file? Suppose I have some data like this
$products = Product::latest()->where('status', 1)->get();
return view('products', compact('products'));
Now in this case how can I write this $products variable in my products.blade.php file.? I don't wana use #foreach loop. Can anybody help me.?
You can use Laravel Collection's get method to retrieve a 'row' at a given key.
#php $i=0; #endphp
#while($i < $products->count())
{{ $products->get($i)->name }}
#php $i++; #endphp
#endwhile

Laravel - How to Transform Raw Query to Eloquent in Laravel

How do I transform my raw query to Laravel Eloquent
I want to transform
$games = DB::table('game_point')
->select('game_point.game_name','game_point.description','game_point.point_assigned', DB::raw("DATE(game_point.created_at) as created_at"))
->paginate(15);
to
$games= new GamePoint();
To try this,
Here you change your date format in Date() function.
$data = GamePoint::select('game_point.game_name','game_point.description','game_point.point_assigned','game_point.created_at')->paginate(15);
foreach($data as $key => $val){
$data[$key]->created_at = Date('Y-m-d',strtotime($data[$key]->created_at));
}
You can directly change it by using date() in blade or in controller depend upon your usage.
try the following code
$data = GamePoint::select('game_point.game_name','game_point.description','game_point.point_assigned','game_point.created_at')->paginate(15);
Or In the blade or contrller you can use it just paste it where you using inside foreach
date('Y-m-d', strtotime($user->from_date));
Or you can use carbon which is already included in laravel for working with date and time.
You can read about this package here
try below:
$games = GamePoint::paginate(15);

How to show specific items of an array in Laravel

I am using Laravel.i want to show only the Name in my View Part. But facing problem.
function index() {
$data=array(
['id'=>'1','Name'=>'Debasais','sirname'=>'Acharya'],
['id'=>'2','Name'=>'prashant','sirname'=>'Mohaptra'],
['id'=>'3','Name'=>'Silu','sirname'=>'Mohaptra']);
return view("welcome")->withdata($data);
}
To show just the name in your view, just loop through $data and select only name.
#foreach($data as $names)
{{$names['Name']}}
#endforeach
I am pretty sure you are already looping through $data just make sure to output the name only

database query not working properly laravel

I have a database query as below:
$data = DB::table('settings')->select('value')->where('name', '=', $name)->get();
echo $data["value"];
but it gives error:
Undefined index: value
if I echo $data; then I get below result:
[{"value":"theme_default"}]
get() returns a Laravel Collection object, which has magic methods to turn itself into a string when you try to use it as such (like with an echo statement). As you can see in the JSON that you've printed, $data is an array or collection of objects, so you want the first one in the collection before trying to get the value:
$row = $data->first();
echo $row->value;
Try this:
$data = DB::table('settings')->select('value')->where('name', '=', $name)->first();
echo $data["value"];
//or
echo $data->value;
You can turn the result into an array like this:
$data = DB::table('settings')
->select('value')
->where('name', '=', $name)
->get()
->toArray();
//then use the value of the first element
dd(#$data[0]['value']);
, but I strongly suggest that you use the Laravel Collection in order to make use of its powerful methods.

htmlentities() expects parameter 1 to be string, array given? Laravel

I've found many question realated to my problem but couldn't found an answer yet. It's about my foreach loop in my blade.
I want to print all product-names in my blade but I couln't figure out how to do that.
thats how I'm getting the products:
--- current code:
// controller
$id_array = Input::get('id');
$products= Products::whereIn('id', $id_array)->get();
$product_name = [];
foreach($products as $arr)
{
$product_name= $arr->lists('name');
}
returning $product_name gives me this as a output:
["football","cola","idontknow","freshunicorn","dummy-data"]
In my blade is just a simple:
#foreach($products as $product)
{{ $product}}
#endforeach
Error: htmlentities() expects parameter 1 to be string, array given
Thanks for your help and time.
It seems you are getting an object in an array in an array.
Like this:
array(
array(
object
)
)
It happens because you use the get() function to retrieve you model. The get() function always "wants" to retrieve multiple models. Instead you will have to use the first() function.
Like this:
foreach($id_array as $arr)
{
$want2editarray[] = Product::where('id', $arr)->first();
}
Hope it helps :)
Edit after #Wellno comment
That's probably because Product::where('id', $arr)->first(); returns null because it did not find anything.
I forgot to add a check after the retrieving of the product.
This can be done like this:
foreach($id_array as $arr)
{
// First try to get model from database
$product = Product::where('id', $arr)->first();
// If $product insert into array
if ($product) $want2editarray[] = $product;
}
Why do you use loop with IDs? You can find all products by IDs:
$products = Product::whereIn('id', $id_array)->get();
And then use $products in the blade template
#foreach($products as $product)
{{ $product->name }}
#endforeach
try to use Model/Eloquent to fetch data.
View should only display the data and not fetching directly from DB or do heavy calculations.

Resources