Undefined variable: user (View: C:\xampp\htdocs\myproject\resources\views\works.blade.php) - laravel-5

I install Auth in my laravel project. In my user table i define a role colum for define Teacher and Student. so in my blade page I want to give access some part for the teacher and hide that part for the student. But I can't do that.
Here is my code: this part only for the teacher.
#if($user->role==Teacher)
<div class="">
<div class="">
<div class="col-md-7 col-md-offset-3">
{!! Form::open(['files'=>true])!!}
<div class="panel panel-default">
<div class="panel-heading">Submit your works</div>
<div class="panel-body">
<div class="form-group>">
<label>Write a new title</label>
<textarea class="form-control" name="assainments" id="assainments"></textarea>
</div>
</div>
<div class="panel-footer clearfix">
<div class="row">
<div class="col-md-6">
<label for="file-upload" class="custom-file-upload">
<i class="glyphicon glyphicon-paperclip"></i>
</label>
<input id="file-upload" name="status_file_upload" type="file"/>
</div>
<div class="col-md-6">
<button class="btn btn-info pull-right btn-am"><i class="fa fa-plus">Add works</i></button>
</div>
</div>
</div>
</div>
#endif

To use the $userin your view you need to pass it from the controller or the method from which you are calling the view.<br/>
Or instead of$useryou can useAuth::user()`:
#if(Auth::user()->role==Teacher)

Related

Cant get Exception object on default error.html Thymeleaf page

I have a Spring Boot application and want to display the exception on the error page.
I put the error.html page in the template directory and I can see the change, but the exception variable keeps coming back as null. I can see all the other data like message and stacktrace.
I took this from several examples. What is missing?
Thymeleaf
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Date</span>
</div>
<div class="col-md-8">
<span th:text="${timestamp}"></span>
</div>
</div>
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Error</span>
</div>
<div class="col-md-8">
<span th:text="${error}"></span>
</div>
</div>
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Status</span>
</div>
<div class="col-md-8">
<span th:text="${status}"></span>
</div>
</div>
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Message</span>
</div>
<div class="col-md-8">
<span th:text="${message}"></span>
</div>
</div>
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Exception</span>
</div>
<div class="col-md-8">
<span th:text="${exception}"></span>
</div>
</div>
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Trace</span>
</div>
<div class="col-md-8">
<span th:text="${trace}"></span>
</div>
</div>
Add this to your application.properties:
server.error.include-exception=true
server.error.include-stacktrace=always
These properties are not enabled by default (See https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html)

Bootstrap Modal is just showing last item's Data of collection rather than specific ID's data

I'am new to laravel. I've a sort of Restaurant Management System's dashboard.
I am showing menu on a blade. By selecting Category from dropdown its showing list of Dishes. Each Dish Which contains Dish Ingredients and Dish AddOn as well. Now I've Edit Button on each dish's Ingredient which will show the Modal against specific Dish's Ingredient. The problem is when I click the edit button, Modal Popup and it shows the Last Dish's Ingredients rather than the Dish's Ingredient which is intended to Edit.
#foreach($items as $item)
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="menu-block">
<div class="menu-content">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
<div class="dish-img"><img height="120px" width="120px" src="{{asset($item['image'])}}" alt="" class="img-circle"></div>
</div>
<div class="col-lg-9 col-md-9 col-sm-9 col-xs-12">
<div class="dish-content">
<h5 class="dish-title">
<b>{{$item['name']}}</b>
<i class="ft-edit"></i>
<i class="ft-trash-2"></i>
</h5>
<h6><b>Ingredients:</b></h6>
#foreach($add_ingredients as $add_ingredient)
#foreach($dish_ing as $dish_in)
#if($item['id']==$dish_in->dish_id&&$dish_in->ingredient_id==$add_ingredient['id'])
<span class="dish-meta" >/{{$add_ingredient['name']}}</span>
#endif
#endforeach
#endforeach
<i class="ft-edit"></i>Edit
<button type="button" class="btn" data-toggle="modal" data-target="#ingredient"><i class="ft-plus-square" ></i>Add</button>
<h6><b>Addons:</b></h6>
#foreach($add_items as $add_item)
#if($add_item['dish_id']==$item['id'])
<span class="dish-meta" >/{{$add_item['name']}} </span>
#endif
#endforeach
<i class="ft-edit"></i>Edit
Modal Section:
<div class="modal fade editaddon{{$item['id']}}" id="">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<form class="form form-horizontal" method="get" action="">
#csrf
<div class="form-body">
<h4 class="form-section"><i class="ft-user"></i>Dish Addon form</h4>
<div class="form-group row">
<label class="col-md-3 label-control" for="projectinput6">Category Name</label>
<div class="col-md-9">
<select id="projectinput6" name="id2" class="form-control">
<option value="none" selected="" disabled="">Select Relevant Category</option>
#foreach($add_items as $add_item)
#if($add_item['dish_id']==$item['id'])
<option value="{{$add_item['id']}}">{{$add_item['name']}}</option>
#endif
#endforeach
</select>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
All I want is to Show the Specific Dish's Ingredient in the Modal When Editing.
Any help will highly appreciated
I have modified the snippet and added the modal box in the main foreach loop. Also i have removed the dot from the data target.
#foreach($items as $item)
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="menu-block">
<div class="menu-content">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
<div class="dish-img"><img height="120px" width="120px" src="{{asset($item['image'])}}" alt="" class="img-circle"></div>
</div>
<div class="col-lg-9 col-md-9 col-sm-9 col-xs-12">
<div class="dish-content">
<h5 class="dish-title">
<b>{{$item['name']}}</b>
<i class="ft-edit"></i>
<i class="ft-trash-2"></i>
</h5>
<h6><b>Ingredients:</b></h6>
#foreach($add_ingredients as $add_ingredient)
#foreach($dish_ing as $dish_in)
#if($item['id']==$dish_in->dish_id&&$dish_in->ingredient_id==$add_ingredient['id'])
<span class="dish-meta">/{{$add_ingredient['name']}}</span>
#endif
#endforeach
#endforeach
<i class="ft-edit"></i>Edit
<button type="button" class="btn" data-toggle="modal" data-target="#ingredient"><i class="ft-plus-square"></i>Add</button>
<h6><b>Addons:</b></h6>
#foreach($add_items as $add_item)
#if($add_item['dish_id']==$item['id'])
<span class="dish-meta">/{{$add_item['name']}} </span>
#endif
#endforeach
<i class="ft-edit"></i>Edit
<div class="modal fade editaddon-{{$item['id']}}" >
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<form class="form form-horizontal" method="get" action="">
#csrf
<div class="form-body">
<h4 class="form-section"><i class="ft-user"></i>Dish Addon form</h4>
<div class="form-group row">
<label class="col-md-3 label-control" for="projectinput6">Category Name</label>
<div class="col-md-9">
<select id="projectinput6" name="id2" class="form-control">
<option value="none" selected="" disabled="">Select Relevant Category</option>
#foreach($add_items as $add_item)
#if($add_item['dish_id']==$item['id'])
<option value="{{$add_item['id']}}">{{$add_item['name']}}</option>
#endif
#endforeach
</select>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endforach

Web pages gets distorted when ever I try to specify a variable on my route

I tried parsing a variable through my route and but the CSS isn't rendered on the page and my page shows purely html=
Here is my web route;
Route::get('/create-transaction/{id}', 'AdminController#create')->name('Create Transaction');
And here is my controller;
public function create($id){
$users = User::orderBy('id', 'desc')->paginate(100);
return view('create-transaction')->with(compact('users', 'id'));
}
I get this sam error with every other web page on my website when. once I parse in a variable through my route, the whole page losses it's formatting. I've tried the same page without parsing variables and it works fine but it's a different case when I parse in a variable
Here is my view page;
#extends('layouts.admin')
#section('content')
<section class="content-top-margin page-title-small border-bottom-light border-top-light">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-12 wow fadeInUp" data-wow-duration="300ms">
<!-- page title -->
<h1 class="black-text">Create Transaction</h1>
<!-- end page title -->
</div>
<div class="col-md-6 col-sm-12 breadcrumb text-uppercase wow fadeInUp" data-wow-duration="600ms">
<!-- breadcrumb -->
<ul>
<li>Dashboard</li>
<li>Withdrawals</li>
<li>Change Password</li>
</ul>
<!-- end breadcrumb -->
</div>
</div>
</div>
</section>
<section class="wow fadeIn border-top">
<div class="container">
<div class="row">
<div class="col-md-5 col-sm-10 center-col">
#if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
#endif
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
<form action="/create-transaction" method="POST">
#csrf
<div class="form-group">
<div class="col-md-6">
<label for="exampleInputEmail1">Date</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
<div class="col-md-6">
<label for="exampleInputEmail1">Amount</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="exampleInputEmail1">Type</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
<div class="col-md-9">
<label for="exampleInputEmail1">Description</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
</div>
<button class="btn btn-black btn-small btn-round no-margin-bottom" type="submit">Create Transaction</button>
<!-- end button -->
</form>
</div>
</div>
</div>
</section>
#endsection

bootstrap modal onclick closing

I have form to submit signon inside a modal. It is not allowing me to enter email and password. When I click on the form element it is closing.What am I doing wrong here.It is supposed to enter the email and password to submit
Form Modal
<div id="click_signin" class="modal New-Bouncing-popup" role="dialog">
<div class="modal-dialog bounce animated modal-ku">
<!-- Modal content-->
<div class="modal-content1">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body row">
<div class="col-sm-6 left-side">
<h1>Surplus<br>Forex.</h1>
<p>Build your profitable forex career.</p>
<br>
<p>Login with social media</p>
<a class="fb" href="#" target="_blank">Login With Facebook</a>
<a class="tw" href="#" target="_blank">Login With Twitter</a>
</div><!--col-sm-6-->
<div class="col-sm-6 right-side">
<h1>Login</h1>
<p>Fill the below given fields to Login</p>
<br/>
<!--Form with header-->
<div class="form">
<div class="form-group">
<label for="form2">Your email</label>
<input type="text" id="form2" class="form-control">
</div>
<div class="form-group">
<label for="form4">Your password</label>
<input type="password" id="form4" class="form-control">
</div>
<div class="text-xs-center frm_grp">
Login
</div>
</div>
<!--/Form with header-->
</div><!--col-sm-6-->
</div>
</div>
</div>
</div>

Strange view error on Laravel

I have some cotroller like this:
Route::get('/article/create', 'ArticlesController#create');
and here's my ArticlesController#create:
public function create(){
return view('articles.create',
[
'title'=>'Add Artikel',
'username'=>'Whatever Myname',
'status' => 'Offline'
]
);
}
when i trying to access blog.dev/article/create i got this strange errors:
"Trying to get property of non-object (View: E:\xampp\htdocs\blog\resources\views\articles\single.blade.php)"
how come i can get this kind of error when my view is pointing at articles.create but the error is at single.blade.php which it suppose for ArticlesController#view?
this is what in create.blade.php:
#extends('admin.layout')
#section('content')
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Horizontal Form</h3>
</div>
#include('admin.formerrors')
<form method="post" class="form-horizontal" action="/articles">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input class="form-control" id="title" name="title" placeholder="Judul Artikel">
</div>
</div>
<div class="form-group">
<label for="content" class="col-sm-2 control-label">Content</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="content" name="content"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
#endsection
and here's in my single.blade.php :
#extends('admin.layout')
#section('content')
<h1>{{ $article->title }}</h1>
<p>{{ $article->content }}</p>
<hr>
#foreach($article->comments as $comment)
<blockquote>
<p>{{ $comment->comment }}</p>
<small>{{ $comment->created_at->diffForHumans() }}</small>
</blockquote>
#endforeach
#include('admin.formerrors')
<form method="post" class="form-horizontal" action="/article/{{ $article->id }}/comment">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="comment" class="col-sm-2 control-label">Comment</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="comment" name="comment"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
#endsection
and here's my router:
Route::get('/articles', 'ArticlesController#index')->name('home');
Route::post('/articles', 'ArticlesController#save');
Route::get('/article/{id}', 'ArticlesController#view');
Route::get('/article/create', 'ArticlesController#create');
Route::post('/article/{article}/comment', 'CommentsController#save');
Route::get('/register', 'RegistrationController#create');
Route::post('/register', 'RegistrationController#store');
I try to change the create function by pointing into another view, but still it showing same error and pointing at single.blade view.
I delete all code at single.blade and write 'test' text, i don't get any errors. but i'm pointing my controller for viewing into create.blade not sigle.blade
After seeing you routes the problem is in this two routes :
Route::get('/article/{id}', 'ArticlesController#view');
Route::get('/article/create', 'ArticlesController#create');
In this case Laravel will consider the create in your path blog.dev/article/create as an id parameter of the view route here => /article/{id}.
So as a solution you should simply inverse the two routes :
Route::get('/article/create', 'ArticlesController#create');
Route::get('/article/{id}', 'ArticlesController#view');
You need to find what's in the file by reading more than just that line. Maybe you're including that file in the articles.create? Maybe you're accessing a variable that doesn't exist whenever you load the page. Edit your answer with more of the error and what's inside both blades and we can pinpoint what's wrong.

Resources