Laravel blade problems with foreach array|object, null given - laravel

First of all, forgive me for my English, it's not my native language and I don't know how to speak/write/express myself very well.
I have a problem with my view. I get the following error:
foreach() argument must be of type array|object, null given (View: /resources/views/admin/stages/index.blade.php)I get the following
in the query result, I see the stage array
Blade code:
#foreach($events as $event)
<div class="box box-default">
<div class="box-header with-border">
<i class="fa fa-road"></i>
<h3 class="box-title">{{ $event->name }}</h3>
</div>
<div class="box-body">
#foreach($event->stages as $stage)
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-light-blue-gradient">
<div class="inner">
<h3>SS{{ $stage->stage_num }}</h3>
<p>{{ $stage->name }}<br>{{ $stage->date }}</p>
</div>
<div class="icon">
<i class="fa fa-map-signs"></i>
</div>
{{ __(ucfirst($stage->state)) }} <i class="fa fa-arrow-circle-right"></i>
</div>
</div>
<!-- ./col -->
#endforeach
</div>
<!-- /.row -->
</div>
<!-- /.box -->
#endforeach
I update from Laravel 5.8 to 8.45.1
Thank you for all

Related

Foreach loop returns all results

I have a column of album covers. I want to take me to the album details after click on the cover. Actually it takes me to the album details but it shows all results, not only this one which was clicked.
AlbumDetailsController
public function index(): View
{
return View("details.index", [
'albums' => Album::paginate(1)
]);
}
AlbumDetails Blade view
#extends('layouts.app')
#section('content')
<div class="container">
#foreach ($albums as $album)
<h1 class="my-4">{{ $album->name }}</h1>
<div class="row">
<div class="col-lg-6 mb-4">
<div class="card h-100">
<div class="card-body">
<h4 class="card-title">
</h4>
{{ $album->artist }}
</div>
</div>
</div>
<div class="col-lg-6 mb-4">
<div class="card h-100">
<img src="{{asset('storage/' . $album->image_path)}}" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
{{ $album->artist }}
</h4>
{{ $album->description }}
</div>
</div>
</div>
</div>
#endforeach
#endsection
I've tried to solve this in differents ways but everything is not working. I think problem is with my controller but I'm really don't know how to modify it to show only this item which was clicked.

Property [Total_Sayur] does not exist on this collection instance

how to display or call value in array in laravel blade view ?
I already have the data that appears in the image below:
enter image description here
I still don't understand how to call the data in the :
<h3>#currency($laporan->Total_Sayur)</h3>
I'm tired of trying it in various ways and it always appears another error message. Please help me
MyController
public function index()
{
$users = User::count();
$kelompok = Kelompok::count();
$anggota = Anggota::count();
$laporan = Anggota::select('kecamatan', Produk::raw('avg(total_sayur) as Total_Sayur, avg(total_buah) as Total_Buah,avg(total_ikan) as Total_Ikan, avg(total_ternak) as Total_Ternak'))
->leftjoin('produk', 'produk.anggota_id', '=', 'anggota.id')
->where('kecamatan', '=', 'Ngajum')
->GroupBy('kecamatan')
->get();
// return $laporan;
return view('Dashboard.index', compact('users', 'kelompok', 'anggota', 'laporan'));
}
View
<div class="row">
<div class="col-lg-3 col-6">
<!-- small box -->
<div class="small-box bg-info">
<div class="inner">
<h3>#currency($laporan->Total_Sayur)</h3>
<p>TOTAl KEMANFAATAN</p>
</div>
<div class="icon">
<i class="ion ion-pricetagg"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>
</div>
Your solution start from here....
<div class="row">
<div class="col-lg-3 col-6">
#foreach($laporan as $item)
<!-- small box -->
<div class="small-box bg-info">
<div class="inner">
<h3>#currency($item->Total_Sayur)</h3>
<p>TOTAl KEMANFAATAN</p>
</div>
<div class="icon">
<i class="ion ion-pricetagg"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
#endforeach
</div>
</div>

Unable to show category wise data in sections of home page

I want to fetch category wise data in home page's sections but I am unable to do this. Only one category section is working[enter image description here][1]
I made Section model and under this model Category model My IndexController Code :
<?php
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Slider;
use App\Category;
use App\Section;
class IndexController extends Controller
{
public function index(){
$sliders = Slider::where('status',1)->get();
$categories = Category::with('sections')->where('status',1)->get();
//dd($categories);die;
return view('frontend.index')->with(compact('sliders','categories'));
}
}
and blade file code is :
<section class="latest_work">
<div class="container">
<div class="row sub_content">
<div class="col-md-12">
<div class="dividerHeading">
<h4><span>Find your best tours</span></h4>
</div>
<div id="recent-work-slider" class="owl-carousel">
#foreach($categories as $category)
#if($category['sections']['section_name'] == "Tours")
<div class="product">
<figure class="touching effect-bubba">
<div class="product-img">
<img class="img-responsive" src="{{ asset('images/categories/'.$category['category_banner']) }}">
<div class="option">
<a class="fa fa-shopping-cart" href="portfolio_single.html"></a>
<a class="fa fa-search mfp-image" href="{{ asset('images/categories/'.$category['category_banner']) }}"></a>
</div>
</div>
</figure>
<div class="product-info">
<div class="product-title">
<h3>
{{ $category['category_name'] }}
</h3>
</div><br>
</div>
</div>
#endif
#endforeach
</div>
</div>
</div>
</div>
</section>
<section class="latest_work">
<div class="container">
<div class="row sub_content">
<div class="col-md-12">
<div class="dividerHeading">
<h4><span>Find your best stays</span></h4>
</div>
<div id="recent-work-slider" class="owl-carousel">
#foreach($categories as $category)
#if($category['sections']['section_name'] == "Stays")
<div class="product">
<figure class="touching effect-bubba">
<div class="product-img">
<img class="img-responsive" src="{{ asset('images/categories/'.$category['category_banner']) }}">
<div class="option">
<a class="fa fa-shopping-cart" href="portfolio_single.html"></a>
<a class="fa fa-search mfp-image" href="{{ asset('images/categories/'.$category['category_banner']) }}"></a>
</div>
</div>
</figure>
<div class="product-info">
<div class="product-title">
<h3>
{{ $category['category_name'] }}
</h3>
</div><br>
</div>
</div>
#endif
#endforeach
</div>
</div>
</div>
</div>
</section>
<section class="latest_work">
<div class="container">
<div class="row sub_content">
<div class="col-md-12">
<div class="dividerHeading">
<h4><span>Find your best cars</span></h4>
</div>
<div id="recent-work-slider" class="owl-carousel">
#foreach($categories as $category)
#if($category['sections']['section_name'] == "Cars")
<div class="product">
<figure class="touching effect-bubba">
<div class="product-img">
<img class="img-responsive" src="{{ asset('images/categories/'.$category['category_banner']) }}">
<div class="option">
<a class="fa fa-shopping-cart" href="portfolio_single.html"></a>
<a class="fa fa-search mfp-image" href="{{ asset('images/categories/'.$category['category_banner']) }}"></a>
</div>
</div>
</figure>
<div class="product-info">
<div class="product-title">
<h3>
{{ $category['category_name'] }}
</h3>
</div><br>
</div>
</div>
#endif
#endforeach
</div>
</div>
</div>
</div>
</section>
only one tours section's categories is showing but not showing other categories
Yes Only one tour sections is showing because of this condition in your code
#if($category['sections']['section_name'] == "Tours")
........................
#endif

Undefined variable: product in blade view

<div class="row">
#foreach($product as $data)
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img src="{{ asset('image/product_image/'.$data->product_image) }}" alt="photo">
<div class="card-body">
<h4 class="card-title">
{{ $data->product_name }}
</h4>
<h5>{{ $data->product_price }}</h5>
<p class="card-text">{{ $data->product_description }}</p>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
#endforeach
<!-- /.row -->
</div>
First collect what you want in the controller. It could be something like this:
$product = Product::all();
And then you must send the product variable to the view. Something like this:
return view('path.to.view', compact('product'));
By the way. it's better to use plural form products.
You can add the code in the blade to retrieve all the products from the product model
<div class="row">
#php
$product = App\Product::all();
#endphp
#foreach($product as $data)
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img src="{{ asset('image/product_image/'.$data->product_image) }}" alt="photo">
<div class="card-body">
<h4 class="card-title">
{{ $data->product_name }}
</h4>
<h5>{{ $data->product_price }}</h5>
<p class="card-text">{{ $data->product_description }}</p>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
#endforeach
<!-- /.row -->
</div>
Your code should be like this
Controller's index method,
public function index()
{
$product = Product::all();
return view('path', compact('product'));
}

Sections inside included blade templates

I want to be able to add as many widgets as i want to a page.
To make it easier to visualize, here is what i want to achieve:
Master Page
Regular Page
In regular page, i want to call blade template panel as many times as i want. Of course, i would like to change sections inside each panel too.
Master Page
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">#yield('title')</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-8">
#yield('panelsLeft')
</div>
<!-- /.col-lg-8 -->
<div class="col-lg-4">
#yield('panelsRight')
</div>
<!-- /.col-lg-4 -->
</div>
<!-- /.row -->
Panel Partial
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-{{ $panelIcon }} fa-fw"></i> #yield('panelTitle')
<div class="pull-right">
<div class="btn-group">
#yield('panelActions')
</div>
</div>
</div>
<!-- /.panel-heading -->
<div class="panel-body">
#yield('panelBody')
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
Regular Page
I want my regular page to extend master page, include as many panels as needed, and set sections of each of them. How can i achieve this?
I couldn't find an answer to this problem. Here is what i tried but it doesn't work:
#extends('layouts.content')
#section('title')
Dashboard
#stop
#section('panelsLeft')
#include('layouts.partial.panel', ['panelIcon'=>'flag'])
#section('panelTitle')
Current Projects
#stop
#section('panelActions')
<button type="button" class="btn btn-default btn-lg">
<span class="glyphicon glyphicon-star"></span> Create New Project
</button>
#stop
#section('panelBody')
<ul class="project-list">
{{ $projects }}
</ul>
#stop
#include('layouts.partial.panel', ['panelIcon'=>'user'])
#section('panelTitle')
Current Users
#stop
#section('panelActions')
<button type="button" class="btn btn-default btn-lg">
<span class="glyphicon glyphicon-star"></span> Create New User
</button>
#stop
#section('panelBody')
<ul class="user-list">
{{ $users }}
</ul>
#stop
#stop

Resources