Problems while passing variable from controller to blade view - php

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

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

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>

laravel function which loops all duplicate project_id's and only shows last created on show.blade.php

hello i have a difficult function i need to solve:
i need a function which filters out the projects with the same project_id and only takes the project with the latest created_at data.
i currently have nothing because i dont know where to start but i am pretty sure i need to write it in my index function of my ProjectController.
overzicht.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="well">
<div class="card mt-5 mb-5">
<h2 class="card-header">{{$projectitem->project_name}></h2>
<div class="card-body">
<h2 class="card-text mt-4 mb-2"><b>Beschrijving:</b></h2><br>
{!!$projectitem->description!!}
<h2 class="mt-4 mb-2"><b>Tijdsduur(in uren):</b> {{$projectitem->time_span}} Uren</h2><br>
<h2 class="mt-2 mb-2"><b>Opdrachtgever:</b> {{$projectitem->client}}</h2>
<h2 class="mt-4 mb-2"><b>Text verslag:</b></h2><br>
{!!$projectitem->text_report!!}
<h2 class="mt-4 mb-2"><b>Foto's vooraf: </b></h2><br>
{!!$projectitem->images_before!!}
<h2 class="mt-4 mb-2"><b>Foto's achteraf: </b></h2><br>
{!!$projectitem->images_after!!}
<div class="row">
<div class="col-md-6">
<h2 class="mt-4"><b>Beschikbaar op: </b></h2>
<h4>Facebook: <?php if($projectitem->facebook == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Instagram: <?php if($projectitem->instagram == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Linkedin: <?php if($projectitem->linkedin == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Website: <?php if($projectitem->website == 1){echo "wel";}else{echo "niet";}?></h4>
</div>
</div>
<h4 class="mt-4 mb-2"><b>Aangemaakt door:</b> {{$projectitem->created_by}}</h4><br>
<div class="row">
<div class="col-md-6">
<h4 class="mt-4 mb-2 floatl "><b>Aangemaakt op:</b> {{$projectitem->created_at}}</h4><br>
</div>
<div class="col-md-6">
<h4 class="mt-4 mb-2 ml-1 mr-1 floatr "><b>Laatst gewijzigd op:</b> {{$projectitem->updated_at}}</h4><br>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
index function in Project Controller
$projects = Project::where('user_id', auth()->user()->id)->get();
$projectitem = Projectitem::all();//->orderBy('created_at')->limit(1)
return view('overzicht', [
'projects' => $projects,
'projectitem' => $projectitem,
]);
EDIT: screenshot
all help is appreciated
i dont know if it can be right for you or not but you might get it with one query
and by the way you dont need to define the project id as you get it from the project object
Project::where('project_id', project->id)->orderBy('created_at', 'desc')->first();

Undefined variable in phpstorm using laravel . Controller & blade template are provided

I'm getting this error message for my code:
Undefined variable: questions (View:
home/xxxx/PhpstormProjects/xxxxx/resources/views/home.blade.php)
Please, try to shed some light on what could be wrong.
I have provided my QuestionController & question template. Not sure, what my code is missing. Here is my code:
QuestionController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Question;
class QuestionController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function show(Question $question)
{
return view('question')->with('question', $question);
}
question.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row ">
<div class="col-md-8">
<div class="card">
<div class="card-header">Question</div>
<div class="card-body">
{{$question->body}}
</div>
<div class="card-footer">
<a class="btn btn-primary float-right"
href="#">
Edit Question
</a>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header"><a class="btn btn-primary `float-left"`
href="#">
Answer Question
</a></div>
<div class="card-body">
#forelse($question->answers as $answer)
<div class="card">
<div class="card-body">{{$answer->body}}`</div>`
<div class="card-footer">
<a class="btn btn-primary float-right"
href="#">
View
</a>
</div>
</div>
#empty
<div class="card">
<div class="card-body"> No Answers</div>
</div>
#endforelse
</div>
</div>
</div>
#endsection
home.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">Questions
<div class="card-body">
<div class="card-deck">
#foreach($questions as $question)
<div class="col-sm-4 d-flex align-items-stretch">
<div class="card mb-3 ">
<div class="card-header">
<small class="text-muted">
Updated: {{ $question->created_at->diffForHumans() }}
Answers: {{ $question->answers()->count() }}
</small>
</div>
<div class="card-body">
<p class="card-text">{{$question->body}}</p>
</div>
<div class="card-footer">
<p class="card-text">
<a class="btn btn-primary float-right" href="{{ route('question.show', ['id' => $question->id]) }}">
View
</a>
</p>
</div>
</div>
</div>
#endforeach
</div>
</div>
<div class="card-footer">
<div class="float-right">
{{ $questions->links() }}
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
The first think that comes to my mind is that you are giving a variable named question to your view in your controller and you are trying to use $questions in your foreach when it must be $question as $q
The other thing is that you may want to pass all Questions rows you have to you view, it can be accomplished (this may not be the best solution) by editing your AppServiceProvider.php
You have to add this.
use View;
use App\Question;
public function boot()
{
//
View::share('questions',Question::all());
}
But if you do this, it will share the rows obtained with all your views.
hope this helps.

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);
}

Categories