my error is undefined variable in laravel - php

I'm so confused, I was trying to fetch data from my database using laravel 8 and I'm sure I defined the variable right but it's having errors.
this is my controller:
public function homepage(){
$product_display = DB::table('products')->get();
return view('customer.homepage', compact('product_display'));
}
And this is the blade file:
<div class="content-wrapper pt-4">
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-3">
<div class="card border border-danger">
<div class="card-body">
</div>
</div>
</div>
<div class="col-lg-9">
<div class="card border border-danger">
<div class="card-body">
#foreach($product_display as $pd)
<div class="card">
<h3>{{ $pd->prod_name }}</h3>
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
</section>
</div>
it says, product_display is not defined.

Use #dd($pd) in Blade foreach loop. You will see if the variable defined or not.
DB::get() returns a stdClass so maybe tryto use view('customer.homepage')->with(['product_display' => $product_display]) instead

Related

Invalid argument supplied for foreach() - the rest is working fine

I'm developing a website like instagram in laravel
everything was going fine until i implemented the foreach command
this is my code:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-3 p-5">
<img src="/images/simbolomax.jpg" class="rounded-circle">
</div>
<div class="col-9 pt-5">
<div class="d-flex justify-content-between align-items-baseline">
<h1 class="p-2">{{ $user->username }}</h1>
Aiciona nova receita
</div>
<div class="d-flex">
<div class="pe-5"><strong>153</strong> receitas</div>
<div class="pe-5"><strong>23k</strong> seguidores</div>
<div class="pe-5"><strong>212</strong> seguindo</div>
</div>
<div class="pt-4">{{ $user->profile->description }}</div>
</div>
</div>
<div class="row pt-5">
#foreach($user->posts as $post)
<div class="col-4">
<img src="/storage/{{$post->image}}" class="w-100" style="height:100%">
</div>
#endforeach
</div>
</div>
#endsection
and the error is: Invalid argument supplied for foreach() (View: C:\projetos\easymeal\resources\views\profiles\index.blade.php)
I can't figure out, send help!
If there is a user with no posts... There goes ERROR. So.... Check user for posts before #foreach.
#if(count($user->posts)>0).... #foreach
Try this
#if ($user->posts->isNotEmpty())
#foreach ($user->posts as $item)
<div class="col-4">
<img src="/storage/{{$post->image}}" class="w-100" style="height:100%">
</div>
#endforeach
#endif

When i return value to my view pages then there should be an error . $student is defined variable . I Laravel

here is the function is given below
public function index(){
$student=Administration::Where('payment',1)->get();
//dd($student);
return view('HallAuthority.index',compact('student'));
}
Here is the view blade
<div class="container-fluid page-body-wrapper">
<div class="row row-offcanvas row-offcanvas-right">
<!-- partial:partials/_sidebar.html -->
#include('HallAuthority.inc.sidebar')
<!-- partial -->
<div class="content-wrapper">
<div class="row">
<div class="col-sm-6 col-md-3 grid-margin">
<div class="card">
<div class="card-body">
{{$student->count()}}
<h2 class="card-title">Total sutdent Student</h2>
</div>
<div class="dashboard-chart-card-container">
<div id="dashboard-card-chart-1" class="card-float-chart"></div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 grid-margin">
<div class="card">
<div class="card-body">
<h2 class="card-title">Total Department</h2>
</div>
<div class="dashboard-chart-card-container">
<div id="dashboard-card-chart-2" class="card-float-chart"></div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 grid-margin">
<div class="card">
<div class="card-body">
<h2 class="card-title">stock price Price</h2>
</div>
<div class="dashboard-chart-card-container">
<div id="dashboard-card-chart-3" class="card-float-chart"></div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 grid-margin">
<div class="card">
<div class="card-body">
<h2 class="card-title">Revenue</h2>
</div>
<div class="dashboard-chart-card-container">
<div id="dashboard-card-chart-4" class="card-float-chart"></div>
</div>
</div>
</div>
</div>
</div>
<!-- content-wrapper ends -->
<!-- partial:partials/_footer.html -->
<div class="card card-body">
<div class="card-header text-center">
Mahfujur#2019
</div>
</div>
<!-- partial -->
</div>
<!-- row-offcanvas ends -->
</div>
The Route file is given below
Route::get('/student/hall','HallController#index')->name('student.hall');
Given this type an error
Undefined variable: student (View: C:\xampp\htdocs\ProjectFinall\resources\views\HallAuthority\index.blade.php) (View: C:\xampp\htdocs\ProjectFinall\resources\views\HallAuthority\index.blade.php)*
I get the value properly but i cannot compact this value to the view pages its always shows that it is undefined value i am totally fed up to solve this . Please anyone help me.
Try this
public function index(){
$student=Administration::Where('payment',1)->count();
return view('HallAuthority.index',compact('student'));
}
<div class="card-body">
{{$student}}
<h2 class="card-title">Total sutdent Student</h2>
</div>

How to dump data into a view?

I get a list of number and series. But when i dump data in to a view it appears like this:
[{"id":1,"number":"e379079p272730","series":"88000000001","type":"import","group_series":null}]
I want on the view it will display the number value in the number column and the series value in the series column. But I don't know how to do that?
View admin.student.listcard
<div class="card-block" >
<div class="row">
<div class="col-md-3 ">
<div class="card-title">
<strong>Number</strong>
</div>
</div>
<div class="col-md-2">
<div class="card-title">
<strong>Series</strong>
</div>
</div>
</div>
#foreach($orders as $order)
<div class="row bottom">
<div class="col-md-3">
<div class="form-group">
{{ $order->card }}
</div>
</div>
<div class="col-md-2">
<div class="form-group">
</div>
</div>
</div>
#endforeach
</div>
function
public function listCard($studentId)
{
$orders = Order::where('member_id',$studentId)->get();
foreach($orders as $order){
$order->card = Card::where('id',$order->card_id)->get();
}
return view('admin.student.listcard',compact('orders'));
}
You have to use eloquent relationships !
In your Order model :
public function card()
{
return $this->belongsTo(Card::class);
}
In your controller :
$orders = Order::with('card')->where('member_id',$studentId)->get();
In your view :
#foreach($orders as $order)
<div class="row bottom">
<div class="col-md-3">
<div class="form-group">
{{ $order->card->number }}
</div>
</div>
<div class="col-md-2">
<div class="form-group">
{{ $order->card->series }}
</div>
</div>
</div>
#endforeach

Laravel foreach database issue

I am making a forum and when the user clicks on a theme, a page shows up with every topic that belongs to that theme. The problem is, how do I do this?
I made a for each loop which shows ALL the topics from the database instead of the topics that belong to that theme I clicked on. How can I do this?
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{id}', 'ThemesController#show')->name('showtheme');
ThemesController.php
I only show the show method because I believe that's the only one necessary here
public function show($id)
{
$topics = Topic::all($);
return view('themes/topics')->with('topics', $topics);
}
topics.blade.php
#extends('layouts.default')
#section('content')
<div class="container main-content">
<div class="row first-row">
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 100, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Gepost door: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Laatste reply</h6>
<p class="center-align">Naam</p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
</div>
</div>
#endsection
I believe I'm doing something obviously wrong but I can't see what.
Help will be appreciated. Thanks in advance
If I miss some code, Please inform me.
How are you defining the relationship between a theme and it's topics? You'll have to have something like a one to many or many to many established in your database for you to be able to query correctly. Do you have anything like that yet?
****edit****
public function show($id)
{
$topics = Theme::find($id)->topics;
return view('themes/topics')->with('topics', $topics);
}

Problems while passing variable from controller to blade view

I have a model class
<?php
class CategoriaModificador extends Base
{
protected $table = 'categorias_modificadores';
public function restaurantes()
{
return $this->hasMany('CategoriaModificadorRestaurante', 'categorias_modificadores_id');
}
}
When I try to pass the variable to the blade view in my controller
public function show($id = 0)
{
if (!get_session_empresa()) {
return Redirect::route('empresa.logar')->with('message_error', 'Você precisa está logado');
}
$categoriaModificador = CategoriaModificador::where('empresas_id', (int)get_session_empresa()->id)
->where("id", (int)$id)
->first();
return View::make('frontend.' . $this->theme_base . '.categoria-modificador.show')
->with('categoriaModificador', $categoriaModificador);
}
I find this error
This is my show.blade.html
#extends('frontend.default.base_restrita')
#section('title')
Categoria de Modificador - {{ config_value('site_nome') }}
#stop
#section('content')
<section id="main-content">
<section class="wrapper">
<section class="panel">
<header class="panel-heading">
{{$categoriaModificador->nome}}
</header>
<div class="panel-body">
<div class="clearfix">
<div class="row">
<div class="col-lg-5">
<h4 class="pull-right"> Nome:</h4>
</div>
<div class="col-lg-6">
<h4 class="pull-left"> {{$categoriaModificador->nome}}</h4>
</div>
</div>
<div class="row">
<div class="col-lg-5">
<h4 class="pull-right"> Obrigatório:</h4>
</div>
<div class="col-lg-6">
<h4 class="pull-left"> {{$categoriaModificador->obrigatorio}}</h4>
</div>
</div>
<div class="row">
<div class="col-lg-5">
<h4 class="pull-right"> Máximo de Opções:</h4>
</div>
<div class="col-lg-6">
<h4 class="pull-left"> {{$categoriaModificador->maximo_opcoes}}</h4>
</div>
</div>
<div class="row">
<div class="col-lg-5">
<h4 class="pull-right"> Restaurantes:</h4>
</div>
#for($i=0;$i<$categoriaModificador->restaurantes->count();$i++)
#if($i==0)
<div class="col-lg-6">
<h4 class="pull-left">{{$$categoriaModificador->restaurantes[$i]->nome}}</h4><br>
</div>
#else
<div class="col-lg-5">
<h4 class="pull-right"></h4>
</div>
<div class="col-lg-6">
<h4 class="pull-left">{{$$categoriaModificador->restaurantes[$i]->nome}}</h4><br>
</div>
#endif
#endfor
</div>
</div>
</div>
</section>
</section>
</section>
#stop
Thanks for posting the view. I think the problem is you're using $categoriaModificador->restaurantes[$i]->nome, but it seems you do not have nome in your $categoriaModificador->restaurantes collection.
Also, why are you using double dollar sign with $$categoriaModificador?
If you clearly look at your error, you will find your solution yourself. Undefined variable error means you are printing something in the view file that is not defined.
For general solution, you should check if the variable is defined previously or is passed to the view. This may helps other.
Thanks

Categories