When passing value it says undefined variable - php

Here is my view its #include which call in layouts.master:
<li id="alert_notificatoin_bar" class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="
{{url('/admin/notification')}}">
<i class="icon-bell-l"></i>
<span class="badge bg-important">{{ $users}}</span>
</a>
<ul class="dropdown-menu extended notification">
<div class="notify-arrow notify-arrow-blue"></div>
<li>
<p class="blue">You have notifications</p>
</li>
<li>
<a href="{{url('/admin/notificationshow')}}">
#foreach($users1 as $u)
<span class="label label-success"><i class="icon_like"></i></span>
{{$u->name}}
<span class="small italic pull-right"> {{$u->created_at}}</span>
#endforeach;
</a>
</li>
<li>
See all notifications
</li>
</ul>
</li>
here is my controller
public function notification()
{
$users = DB::table('users')->where("Active", 0)->count();
$users1 = DB::table('users')->where("Active", 0)->get();
return view('admin.layout.master')->with('users',$users)->with('users1',$users1);
//return view('');
}
public function notificationshow()
{
$users1 = DB::table('users')->where("Active", 0)->get();
return view('admin.dashboard.notificationshow')->with('users1',$users1);
}
public function updatenotification(Request $request)
{
DB::table('users')
->where($request)
->update(['Active' => 1]);
return view('admin.dashboard.notificationshow');
}
}
here is my another view which getting error notificationshow
<div class="row">
<div class="col-lg-12">
<div>
#foreach($users1 as $u)
<span class="label label-success"><i class="icon_like"></i>
{{$u->name}}</span>
<span class="small italic pull-right"><label> <h5>Requested at</h5></label>{{$u-
>created_at}}</span>
<label><h5>Waiting for Approval</h5></label>
#endforeach
<form class="login-form" role="form" method="POST" action="{{ url('/admin/')
}}">
{{ csrf_field() }}
<button class="btn btn-primary btn-lg btn-block" type="submit">Approve</button>
</div>
</form>
why its giving me error please help i checked using dd($user1); its give value but why i getting error when passing value in other view notificationshow

This is a case where code neatness, especially indentation, can help you identify bugs in your code. Looking at the code provided above, there are unmatched <div> and <form> tags, and line breaks in odd places that possibly are causing errors.

Related

How do i fix this error i'm getting "Sorry, the page you are looking for could not be found."

I have this application where I am trying to get the the author of a question to be able to mark an answer as the best answer.
I have an AcceptAnswerController created which has been registered in the routes/web.php file as below:
AcceptAnswerController.php
class AcceptAnswerController extends Controller
{
public function __invoke(Answer $answer)
{
$this->authorize('accept', $answer);
$answer->question->acceptBestAnswer($answer);
return back();
}
}
web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('questions', 'QuestionsController')->except('show');
Route::resource('questions.answers', 'AnswersController')->only(['store', 'edit', 'update', 'destroy']);
Route::get('questions/{slug}', 'QuestionsController#show')->name('questions.show');
Route::post('answers/{answer}/accept', 'AcceptAnswerController')->name('answers.accept');
In my Answers view, i have the following:
<div class="row mt-4">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<div class="card-title">
<h2>{{ $answersCount . " " . str_plural('Answer', $answersCount) }}</h2>
</div>
<hr>
#include ('layouts._messages')
#foreach ($answers as $answer)
<div class="media">
<div class="d-fex flex-column vote-controls">
<a title="This answer is useful" class="vote-up">
<i class="fas fa-caret-up fa-3x"></i>
</a>
<span class="votes-count">1230</span>
<a title="This answer is not useful" class="vote-down off">
<i class="fas fa-caret-down fa-3x"></i>
</a>
#can ('accept', $answer)
<a title="Mark this answer as best answer"
class="{{ $answer->status }} mt-2"
onclick="event.preventDefault(); document.getElementById('answer-{{ $answer->id }}').submit();"
>
<i class="fas fa-check fa-2x"></i>
</a>
<form id="answer-{{ $answer->id }}" action="{{ route('answers.accept', ['answer' => $answer->id]) }}" method="POST" style="display:none;">
#csrf
</form>
#else
#if ($answer->is_best)
<a title="The question owner accepted this answer as best answer"
class="{{ $answer->status }} mt-2"
>
<i class="fas fa-check fa-2x"></i>
</a>
#endif
#endcan
</div>
<div class="media-body">
{!! $answer->body_html !!}
<div class="row">
<div class="col-4">
<div class="ml-auto">
#can ('update', $answer)
Edit
#endcan
#can ('delete', $answer)
<form class="form-delete" method="post" action="{{ route('questions.answers.destroy', [$question->id, $answer->id]) }}">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure?')">Delete</button>
</form>
#endcan
</div>
</div>
<div class="col-4"></div>
<div class="col-4">
<span class="text-muted">Answered {{ $answer->created_date }}</span>
<div class="media mt-2">
<a href="{{ $answer->user->url }}" class="pr-2">
<img src="{{ $answer->user->avatar }}">
</a>
<div class="media-body mt-1">
{{ $answer->user->name }}
</div>
</div>
</div>
</div>
</div>
</div>
<hr>
#endforeach
</div>
</div>
</div>
</div>
Once the author of the question clicks on the check icon, it should mark the answer as the best answer but i get the following error:
Sorry, the page you are looking for could not be found
I see that the answer id is missing in the url as follows:
http://localhost:8000/answers//accept
when it should actually be something like this:
http://localhost:8000/answers/1/accept
I can't seem to figure out why as i passed it as a route parameter in the form action. Same thing happens if a user is trying to edit his answer.
Can I suggest tweaking your strategy a little bit here and bypassing the need for a form submission?
If you swap out the POST for a GET request, this is actually pretty straight forward.
Answers.blade.php
#foreach( $answers as $answer )
#can('accept', $answer)
<a href="answers/{{ $answer->id }}/accept" title="Mark this answer as best answer" class="{{ $answer->status }} mt-2">
<i class="fas fa-check fa-2x"></i>
</a>
#else
<p>Do your thing</p>
#endcan
#endforeach
routes.web.php
Route::get('answers/{answer}/accept', 'AcceptAnswerController');
So, after scrutinizing my code a bit more carefully everything else seemed okay the controller, routes, views. I was able to isolate the problem down to this region
#can ('accept', $answer)
<a title="Mark this answer as best answer"
class="{{ $answer->status }} mt-2"
onclick="event.preventDefault(); document.getElementById('answer-{{ $answer->id }}').submit();"
>
<i class="fas fa-check fa-2x"></i>
</a>
<form id="answer-{{ $answer->id }}" action="{{ route('answers.accept', ['answer' => $answer->id]) }}" method="POST" style="display:none;">
#csrf
</form>
#endcan
I was certian the $answer->id was supposed to return the correct id but i wasn't too sure about the $answer->status so I decided to check its accessor defined in the Answer model.
public function getStatusAttribute()
{
return $this->isBest() ? 'vote-accepted' : '';
}
public function isBest()
{
return $this->id = $this->question->best_answer_id; /** here is the problem **/
}
There the problem was staring right back at me. The isBest method above was supposed to return a boolean value but i was mistakingly assigning. This was the simple fix.
public function isBest()
{
return $this->id === $this->question->best_answer_id; /** here is the problem **/
}

Cannot Delete Post With Dropdown Link

I'm trying to delete a user post with a drop-down link "Delete Post." I feel I have the logic right as I want to remove the photo as well. I don't understand what I'm doing wrong. If anyone can instruct me, it would be appreciated.
PostsController:
<?php
public function destroy($id)
{
$post = Post::findOrFail($id);
unlink(public_path() . $post->photo->file);
$post->delete();
return redirect('/home');
}
web.php:
Route::delete('/home', 'PostsController#destroy')->name('deletePost');
home.blade.php:
<div class="card-header">
<div class="dropdown">
<button style="float: right;" type="button" class="btn btn-sm dropdown-toggle" data-toggle="dropdown">
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Edit Post</a>
<a class="dropdown-item" href="{{ route('deletePost') }}">Delete Post</a>
</div>
</div>
<div>{{$post->user->name}}</div>
<div id="post-date">{{$post->created_at->diffForHumans()}}</div>
</div>
just change your route to
Route::get('/home', 'PostsController#destroy')->name('deletePost');
since tag a make http GET request and you're listing on DELETE request

Make a function containing blade syntax

I have this blade in my view. Right now, I have 6 blocks of them in my view because I'm not sure how to refactor it.
<div class="row filemanager">
<div class="col-sm-12">
#foreach ($devices as $device)
#if( $device->vlan_id == 100 AND $device->device_activity == 'ACTIVE' )
<div class="col-xs-6 col-sm-4 col-md-2 text-center">
<div class="thmb">
<div class="btn-group fm-group" style="display: none;">
<button type="button" class="btn btn-default dropdown-toggle fm-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu fm-menu" role="menu">
<li id="device-menu">
<a class="changeDeviceNameBtn" href="#"><i class="fa fa-pencil"></i> Change Device Name </a>
</li>
</ul>
</div>
<div class="thmb-prev">
<a href="/{{$cpe_mac}}/device/{{$device->device_mac}}">
#if(isset($device->device_name))
{{-- Show base on device name --}}
<img src="/images/photos/devices/{{img($device->device_name)}}.jpg" class="draggable img-responsive" alt="">
#else
{{-- No Device Name Set --}}
#if($device->hostname != '')
{{-- Show base on hostname --}}
<img src="/images/photos/devices/{{img($device->hostname)}}.jpg" class="draggable img-responsive" alt="">
#else
{{-- Show default --}}
<img src="/images/photos/devices/no-img.jpg" class="draggable img-responsive" alt="">
#endif
#endif
</a>
</div>
<h5 class="fm-title device_name">
<a href="/{{$cpe_mac}}/device/{{$device->device_mac}}">
#if($device->hostname == '')
No Devicename
#else
{{ $device->hostname}}
#endif
</a>
</h5>
<h5 class="text-muted device_ip">{{$device->ip_address}}</h5>
<h5 class="text-muted device_mac">{{$device->device_mac}}</h5>
<?php
$status = ucfirst(strtolower($device->device_activity));
if ($status == 'Active'){
$color = '#1CAF9A';
}else{
$color = '#D9534F';
}
?>
<h5>{{ $status }}
<i class="fa fa-circle" style="color:{{$color}}; margin-left: 7px;"></i>
</h5>
</div>
</div>
#endif
#endforeach
</div>
</div>
I want to make a function containing that blade, and only replace my
$device->vlan_id, and my $device->device_activity.
Example,
public static deviceRow(100,ACTIVE){
... my blade ...
}
Now, I just that function 6 times, rather than duplicate that block of code 6 times.
Is it even possible ?
Any hints / suggestions on this will be much appreciated !
You can make a partial with your blade and send a variable as a parameter:
In your parent view do something like this:
#foreach($somelist as $item)
#include('view.partial', ['name' => $item->name])
#endforeach
And in a file called partial.blade.php, do something like this:
{{ $device->$name }}
It's the main idea. Tell me if it helps...
You could create a new view and send some parameters with it while including:
#include('my.view', ['device' => $myDevice, 'activity' => 'ACTIVE'])
The keys of the array will be available as variables in your view.
The variable $myDevice would be available as $device in the view my.view

Laravel 5: how to do multi threaded comments

I am using Laravel Commentable package which uses Nested Sets pattern with Baum.
I have managed to allow users to make comments on posts but they're not threaded, each comment has depth set to zero in the database.
So I'm wondering how does one go about making multi threaded comments like reddit for example?
These are my tables
users: id, name, email...
posts: id, user_id, subreddit_id...
comments: id, body, parent_id, lft, rgt, depth, commentable_id, commentable_type, user_id
My Models (Comment and User)
class Comment extends Model
{
use Commentable;
public function commentable()
{
return $this->morphTo();
}
public function user() {
return $this->belongsTo('App\User');
}
public function posts() {
return $this->belongsTo('App\Post');
}
}
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
public function posts() {
return $this->hasMany('App\Post');
}
public function comments() {
return $this->hasMany('App\Comment');
}
}
This is how I'm submitting comments in PostsController
public function createComment($id) {
$post = Post::with('user.votes')->with('subreddit.moderators')->where('id', $id)->first();
$comment = new Comment;
$comment->body = Input::get('comment');
$comment->user_id = Auth::id();
$post->comments()->save($comment);
}
And this is the view
<div class="post-comments">
{!! Form::open(['route' => ['comment', $post]]) !!}
<div class="form-group">
<label for="comment">Your Comment</label>
<textarea name="comment" class="form-control" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-default">Send</button>
{!! Form::close() !!}
<div class="comments-nav">
<ul class="nav nav-pills">
<li role="presentation" class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
there are {{ count($comments) }} comments <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li>Best</li>
<li>Hot</li>
</ul>
</li>
</ul>
</div>
<div class="row">
<div class="media">
<!-- first comment -->
#foreach($comments as $comment)
<div class="media-heading">
<button class="btn btn-default btn-xs" type="button" data-toggle="collapse" data-target="#{{ $comment->id }}" aria-expanded="false" aria-controls="collapseExample"><span class="glyphicon glyphicon-minus" aria-hidden="true"></span></button> <span class="label label-info">12314</span> {{ $comment->user->name }} 12 hours ago
</div>
<div class="panel-collapse collapse in" id="{{ $comment->id }}">
<div class="media-left">
<div class="vote-wrap">
<div class="vote up">
<i class="glyphicon glyphicon-menu-up"></i>
</div>
<div class="vote inactive">
<i class="glyphicon glyphicon-menu-down"></i>
</div>
</div>
<!-- vote-wrap -->
</div>
<!-- media-left -->
<div class="media-body">
<p>{{ $comment->body }}</p>
<div class="comment-meta">
<span>delete</span>
<span>report</span>
<span>hide</span>
<span>
<a class="" role="button" data-toggle="collapse" href="#replyCommentT" aria-expanded="false" aria-controls="collapseExample">reply</a>
</span>
<div class="collapse" id="replyCommentT">
<form>
<div class="form-group">
<label for="comment">Your Comment</label>
<textarea name="comment" class="form-control" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-default">Send</button>
</form>
</div>
</div>
<!-- comment-meta -->
</div>
</div>
<!-- comments -->
#endforeach
</div>
<!-- first comment -->
</div>
</div>
<!-- post-comments -->
I haven't used Laravel Commentable package, but the docs look pretty good.
I believe you need use Commentable; on your Post model and not your Comment model.
It looks like your Comment model needs to extend Baum\Node and not Model
Then what you have should work.
$post = Post::with('user.votes')->with('subreddit.moderators')->where('id', $id)->first();
$comment = new Comment;
$comment->body = Input::get('comment');
$comment->user_id = Auth::id();
$post->comments()->save($comment);
// or you could do
$comment->makeChildOf($post);
To make a comment on a comment, it looks like you do something like this. I would probably make a CommentsController.
public function addComment(Request $request){
$parent = Comment::find(Input::get('parent_id'));
$comment = new Comment;
$comment->body = Input::get('comment');
$comment->user_id = Auth::id();
$comment->makeChildOf($parent);
}
The Relations, Root and Leaf scopes, Accessing the ancestry/descendancy chain section of the docs have several examples on how to then retrieve the comments for comments.
Edit
It looks like the Comment model in the package already extends the Baum\Node so you don't need to do that. In order to use this package, it looks like you need to use his Comment model. I am sure you could use his model as a base and roll your own.
You could do something like this. You would have to set up a route.
<div class="collapse" id="replyCommentT">
{!! Form::open(['route' => ['comment', $comment]]) !!}
<input type="hidden" name="parent_id" value="{{ $comment->id }}"/>
<div class="form-group">
<label for="comment">Your Comment</label>
<textarea name="comment" class="form-control" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-default">Send</button>
{!! Form::close() !!}
</div>

Where move functions from template in Laravel

I would like show nested list in my blade template.
If I want quickly display nested list I have to do it by recursive. I created in my template function named renderNode(). But this global function im my template is not good idea and practise. I would like reorganize it.
Do you know good practise, how to organize my code?
<?php
function renderNode($node) {
echo "<li class='dd-item dd3-item' data-id='$node->id'>";
echo "<div class='dd-handle dd3-handle'>Drag</div>
<div class='dd3-content'>
$node->name
<span class='pull-right categories-actions'>
<i class='fa fa-plus-square-o delete-category' title=''></i>
<i class='fa fa-cogs delete-category' title=''></i>
<i class='fa fa-trash-o delete-category' title=''></i>
</span>
</div>";
if ( $node->children()->count() > 0 ) {
echo "<ol class='dd-list'>";
foreach($node->children as $child) renderNode($child);
echo "</ol>";
}
echo "</li>";
}
?>
<div id="content" class="col-lg-10 col-sm-11">
<div class="row">
<div class="col-lg-12">
<div>
<button id="add-new-category" class="btn btn- primary">#lang('categories.new_category')</button>
</div>
<div class="dd" id="nestable3">
<ol class="dd-list">
#if(isset($categories))
#foreach($categories as $category)
<?php renderNode($category); ?>
#endforeach
#endif
</ol>
</div>
</div><!--/col-->
</div><!--/row-->
You are right, this is not good practice. The best thing to do would be to put this code in a model of your choosing. Models > Category would be the most appropriate imo. Just make a public function in your Category model like so...
public function renderNode($node)
{
// place code here
return something here;
}
then call the model function in your blade template
<?php Category::renderNode( $node ) ?>
You can pass $node through the Make::view in your route using with.
However, you might want to consider a different approach. Like this...
#if ( $node->children()->count() > 0 )
<ol class='dd-list'>
#foreach ($node->children as $child)
{{ Category::renderNode($child) }}
#endforeach
</ol>
#endif
<ol class="dd-list">
#foreach($categories as $category)
<li class='dd-item dd3-item' data-id="$category->id">
<div class='dd-handle dd3-handle'>Drag</div>
<div class='dd3-content'>
{{ $node->name }}
<span class='pull-right categories-actions'>
<i class='fa fa-plus-square-o delete-category' title=''></i>
<i class='fa fa-cogs delete-category' title=''></i>
<i class='fa fa-trash-o delete-category' title=''></i>
</span>
</div>
#if ( $node->children()->count() > 0 )
#foreach ($node->children as $child)
<li class='dd-item dd3-item' data-id='$child->id'>
<div class='dd-handle dd3-handle'>Drag</div>
<div class='dd3-content'>
{{ $child->name }}
<span class='pull-right categories-actions'>
<i class='fa fa-plus-square-o delete-category' title=''></i>
<i class='fa fa-cogs delete-category' title=''></i>
<i class='fa fa-trash-o delete-category' title=''></i>
</span>
#endforeach
#endif
</div>
</li>
#endforeach
</ol>
something like this

Categories