How to dump data into a view? - php

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

Related

my error is undefined variable in laravel

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

How to show label in a loop. For example label 1, label 2 and so on

How to show label in a loop.For example label 1, label 2 and so on continuous till the end of the loop.
Here is my code i want to show Experience 1 and Experience 2 and so on through out the loop
till loop ends.
#foreach($employee->experiences as $experince)
<div class="card-header">
<label for="">Experience</label>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Company:</b></div>
<div class="col-md-6">{{ ucfirst($experince->company) }}</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Degree :</b></div>
<div class="col-md-6">{{ ucfirst($employee->degree) }}</div>
</div>
</div>
</div>
</div>
#endforeach
In blade template there is a $loop variable:
$loop->index
will print the current index, starting from 0
$loop->iteration
will instead start from 1
The variable is available inside a #foreach:
#foreach($employee->experiences as $experince)
{{ $loop->iteration }}
#endforeach
Try this $loop->iteration ref link https://laravel.com/docs/8.x/blade#the-loop-variable
#foreach($employee->experiences $experince)
<div class="card-header">
<label for="">Experience {{ $loop->iteration }}</label>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Company:</b></div>
<div class="col-md-6">{{ ucfirst($experince->company) }}</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Degree :</b></div>
<div class="col-md-6">{{ ucfirst($employee->degree) }}</div>
</div>
</div>
</div>
</div>
#endforeach
Create it variable as iteration and increment it each iterations
#php
$it = 1;
#endphp
#foreach($employee->experiences as $experince)
<div class="card-header">
<label for="">Experience</label>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Label:{{ $it }}</b></div>
<div class="col-md-6"><b>Company:</b></div>
<div class="col-md-6">{{ ucfirst($experince->company) }}</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Degree :</b></div>
<div class="col-md-6">{{ ucfirst($employee->degree) }}</div>
</div>
</div>
</div>
</div>
#php
$it += 1;
#endphp
#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);
}

Laravel multipule relationships count

I have these models:
ForumCategory
Forum
Topic
Reply
In the main forum page, I want to count and display the number of replies that associated to this forum.
All the models have relationships.
This is the view:
#foreach ($categories as $category)
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default striped">
<div class="panel-heading">
<div class="row">
<div class="col-md-11">{{ $category->name }}</div>
<div class="col-md-1 text-right"><a><span class="fa fa-arrow-down"></span></a></div>
</div>
</div>
#foreach ($category->forums as $forum)
<div class="panel-body">
<div class="row">
<div class="col-md-1 text-right"><span class="fa fa-envelope fa-4x"></span></div>
<div class="col-md-7">
<h4>
{{ $forum->name }}
<br>
<small>{{ $forum->description }}</small>
</h4>
</div>
<div class="col-md-1 post-num">
<h5>{{ $forum->topics->count() }}<br>
<small>Topics</small></h5>
<h5>{{ $forum->topics->replies->count() }}<br>
<small>Replies</small></h5>
</div>
<div class="col-md-3">
<img src="" class="img-circle last-comnt-img">
Last comment<br>
By Username<br>
<span class="text-muted">12:31</span>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
#endforeach
This line won't work:
$forum->topics->replies->count()

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