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.
Related
I am facing undefined variable error when trying to access a variable in my view. I have gone through several solutions out here but none of them has worked for me. I have thoroughly examined everything that could cause this error in mu code and cannot find anything at all, please assist
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use App\Category;
use App\Depot;
use Illuminate\Support\Facades\DB;
use Gloudemans\Shoppingcart\Facades\Cart;
use Auth;
class CheckoutController extends Controller
{
public function index()
{
$allcategories=Category::get();
$delivery_method = DB::table('deliverymethods')
->get();
$cartItems=Cart::content();
return view('checkout.index',['delivery_method'=>$delivery_method],['allcategories'=>$allcategories]);
}
public function deliverymethod()
{
$cartItems=Cart::content();
$allcategories=Category::get();
return view('delivery.index',['cartItems'=>$cartItems],['allcategories'=>$allcategories]);
}
public function billinginfo()
{
$depots=Depot::get();
$cartItems=Cart::content();
$allcategories=Category::get();
return view('billing.index',compact('cartItems'),['allcategories'=>$allcategories],['depots'=>$depots]);
}
public function shipping(Request $request)
{
$user_id=Auth::user()->id;
$shippingaddress=DB::table('shippingaddresses')
->select('shippingaddresses.*')
->where('user_id',$user_id)
->get();
$cartItems=Cart::content();
$allcategories=Category::get();
return view('shipping.index',['cartItems'=>$cartItems],['allcategories'=>$allcategories],['shippingaddress'=>$shippingaddress]);
}
}
My View
#extends('home.base')
#section('action-content')
<div class="container">
<div class="container" id="cart-window">
<div class="row">
<div class="col-lg-12" id="cart-header">
<h3>Billing & Shipping Details <span class="cart-return pull-right"><i class="ionicons ion-ios-cart"></i> Back to Cart</h3>
</div>
</div>
<div class="row">
<div class="col-lg-9">
#if(Cart::Count()==0)
<div class="row">
<div class="col-lg-12" id="empty-cart">
<p>Your cart is currently empty</p>
</div>
</div>
<div class="row">
<div class="col-lg-12">
Return to Shop
</div>
</div>
#else
<div class="card detail" id="delivery-card">
<div class="card-header">
</div>
<div class="card-body">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-lg-6">
<h5><strong>Existing Addresses</strong></h5>
</div>
<div class="col-lg-6">
<select class="select2 form-control" id="newAddress">
<option selected disabled>Please Select Delivery Address</option>
<option value="0">Add New Address</option>
#foreach($shippingaddress as $depot)
<option value="{{$depot->depot_name}}">{{$depot->depot_name}}</option>
#endforeach
<option>LIGHt</option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
#endif
</div>
<div class="col-lg-3">
#if(Cart::Count()>0)
<div class="card detail">
<div class="card-header">
</div>
<div class="card-body">
<p class="card-text" id="cart-summary">
Order Summary
</p>
<hr/>
<div class="row">
<div class="col-sm-6">
{{Cart::Count()}}
</div>
<div class="col-sm-6">
#if(Cart::Count()==1)
Item
#else
Items
#endif
</div>
</div>
<hr/>
<div class="row">
<div class="col-sm-6">
<p>
Subtotal
</p>
<p>
VAT (15%)
</p>
<p>
Total to Pay
</p>
</div>
<div class="col-sm-6" id="cart-totals">
<p>
R{{Cart::subtotal()}}
</p>
<p>
R{{Cart::tax()}}
</p>
<p>
R{{Cart::total()}}
</p>
</div>
</div>
<hr/>
<div class="row">
<div class="col-12 continue-btn">
Continue
</div>
</div>
{{-- <hr>
<div class="row" id="delivery-method">
<select class="form-control select2" id="delivery_method" name="del">
<option value="0" selected disabled>Select Delivery Method</option>
#foreach ($delivery_method as $item)
<option value="{{$item['delivery_method']}}">{{$item['delivery_method']}} R{{$item['price']}}</option>
#endforeach
</select>
</div> --}}
</div>
{{-- <div class="card-footer" id="cart-footer">
<div class="delivery_type">
<strong>Checkout</strong>
</div>
{{-- <div class="delivery_type2">
<strong>Checkout 2</strong>
</div> --}}
{{-- </div> --}}
</div>
<div class="card order-review">
<div class="card-body">
<p class="card-text" id="cart-summary">
Order Review
</p>
<div class="row">
<div class="col-sm-12">
<p><strong>Delivery Method</strong> <span class="change-del pull-right">Change</span></p>
<p>Delivery</p>
</div>
</div>
</div>
</div>
#endif
</div>
</div>
</div>
</div>
<!-- NewAddressModal -->
<!-- Modal -->
<div class="modal fade show" id="newAddressModal" style="display: none;" aria-modal="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Default Modal</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
#push('custom_scripts')
<script type="text/javascript">
$(document).ready(function() {
{{-- $("#newAddress").change(function() {
var curVal = $("#newAddress option:selected").val();
if (curVal.indexOf('#newAddressModal') == 0){
$('#newAddressModal').modal('show');
}
}); --}}
$("#newAddress").on("change", function () {
$modal = $('#newAddressModal');
if($(this).val() === '0'){
$modal.modal('show');
}
});
});
</script>
#endpush
#endsection
Error Image
Error Image
Try grouping Multiple collection into one array, Then send it to the view like this:
$viewData = [
'cartItems' => $cartItems,
'allcategories' => $allcategories,
'shippingaddress' => $shippingaddress
];
return View::make('shipping.index')->with($viewData);
There is an issue in the answer section of this code. The question section works fine, it is just the question.. when dd($questionnaire) answer array is there, but no items are shown.
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ $questionnaire->title }}</div>
<div class="card-body">
<a class="btn btn-dark" href="/questionnaires/{{ $questionnaire->id }}/questions/create">Create Question</a>
<a class="btn btn-dark" href="/surveys/{{ $questionnaire->id }}-{{ Str::slug($questionnaire->title) }}">Complete Questionnaire</a>
</div>
</div>
#foreach($questionnaire->questions as $question)
<div class="card mt-3">
<div class="card-header">{{ $question->question }}</div>
<div class="card-body">
<ul class="list-group">
#foreach($question->answers as $answer)
<li class="list-group-item">{{ $answer->answers }}</li>
#endforeach
</ul>
</div>
</div>
#endforeach
</div>
</div>
</div>
#endsection
dd Function means die and debug so your script stops at this point. Nothing else runs after dd
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);
}
I am working on laravel 5.2.I want to display those members who belongs to that particular group which is open at this time. Actually, i am getting all the members which i have stored in my database but, i only want to access or display only those members who belongs to a particular on which i am currently accessing.
I am getting an error: Method groups does not exist. which is shown below:
My controller:
public function members($id){
$dashes=Grouptable::findorFail($id);
$members=Member::all();
return view('members' , ['dashes'=>$dashes,'members'=>$members]);
}
public function dashboard($id){
$dashes=Grouptable::findorFail($id);
return view('dashboard' , ['dashes'=>$dashes]);
}
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member);
return redirect()->back();
}
My view:
<body>
<div class="row">
<div class="col-lg-3 col-lg-offset-1">
<img src="images/ImgResponsive_Placeholder.png"
class="img-circle img- responsive" alt="Placeholder image"> </div>
<div class="col-lg-7">
<h1 style="color:black;">{{ $dashes->name }}</h1></div>
<br />
</div>
<div class="row">
<div class="col-lg-3">
<button class="btn btn-success" onclick="myFunction()">
Add Members + </button>
<div>
<form id="demo" style="display:none;" method="post"
action="{{ route('addmember') }}">
<input class="form-control" type="text" name="addmember">
<button class="btn btn-primary" type="submit">Add</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
<div class="col-lg-7 col-lg-offset-0">
<div class="panel panel-default">
<div id="grp" class="panel-heading">
<h3 id="grouptitle" class="panel-title">Group Members</h3>
</div>
<div id="zx" class="panel-content">
<div class="row">
#foreach($members as $member)
<section class="col-md-6">
<div class="row">
<section class="col-md-offset-1 col-md-3 col-xs-offset-1 col-xs-4">
<img id="imagesize" src="images/g.jpg" class="img-circle"/>
</section>
<section class="col-md-offset-1 col-md-7 col-xs-7">
<section class="col-md-12">
<h5 id="friendname">{{$member->members}}</h5>
</section>
<section class="col-md-12">
<button type="button" class="btn btn-sm btn-
default">Score</button>
</section>
</section>
</div>
<div class="row">
<section class="col-md-offset-9 col-md-3 col-xs-offset-6
col-xs-4">
<div class="btn-group">
<button id="btnclr1" type="button" class="btn btn-block
btn-warning dropdown-toggle" data-toggle="dropdown" aria-
expanded="false"><span class="caret"></span></button>
<ul id="bckdrp" class="dropdown-menu" role="menu">
<li role="presentation"><a id="drpmenu" href="#">Remove</a>
</li>
</ul>
</div>
</section>
</div>
<div class="row">
<section class="col-md-offset-1 col-md-10">
<hr>
</section>
</section>
#endforeach
</div>
<div id="mn" class="panel-footer"><a id="seemr1"
href="#.html">See More</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
My routes:
Route::get('/members/{id}',[
'uses'=>'GroupController#members',
'as'=>'members'
]);
Route::get('/dashboard/{id}',[
'uses'=>'GroupController#dashboard',
'as'=>'dashboard'
]);
Route::post('/memeber/add',[
'uses'=>'GroupController#addmembers',
'as'=>'addmember'
]);
My modals:
Grouptable:
public function members(){
return $this->hasMany('App\Member');
}
Member:
public function groups(){
return $this->belongsTo('App\Grouptable');
}
I do not quite understand the entire problem, but
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member);
return redirect()->back();
}
should look more like
public function addmembers(Request $request){
$member=new Member();
$member->propertyX = $request->get('propertyX');
$member->propertyY = $request->get('propertyY');
$member->groups()->attach($group); // FOR MANY-TO-MANY (N-N) RELATION
$member->groups()->associate($group); // FOR ONE-TO-MANY (1-N) RELATION
$member->save();
return redirect()->back();
}
Depending on your migrations you should choose attach() or associate()
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