Unable to Delete Using Laravel's Policy - php

I am logged in as user2 and would like to see the delete button on user2's review (screenshot). However, the button did not appear. I am using Laravel's policy.
I have attached my blade code below:
#foreach($reviews as $review)
{{-- <div id="review_card" class="card" style="width: 18rem;"> --}}
<div id="review_card" class="card">
<div class="card-body">
<h5 class="card-title">{{ $review->title }}</h5>
<h6 class="card-subtitle mb-2 text-muted">User {{ $review->user_id }}</h6>
<p class="card-text">{{ $review->review }}</p>
Edit
{{-- Another link --}}
#can('delete', $review)
Delete
#endcan
</div>
</div>
Here is my ReviewPolicy code snippet:
public function delete(User $user, Review $review)
{
return $user->id === $review->user_id;
}
Here is my route:
Route::get('/delete/{id}', 'App\Http\Controllers\UserController#deleteReview')->middleware('user');
And lastly, here's the AuthServiceProvider:
protected $policies = [
'App\Models\Review' => 'App\Policies\ReviewPolicy',
];
To my understanding, the deletability of the review lies in ReviewPolicy and that was where I tried to tweak the code, but to no success. Not exactly sure where I have gone wrong.

Related

Undefined variable $payments laravel 9.x

I am trying to print out the lists of payment methods using the foreach loop, however, i get an undefined variable.. error.
Here is my controller class
class HomeController extends Controller
{
//
public function index()
{
$payments = Payment::where('status', 1)->get();
return view('user.account.index', [
'payments' => $payments
]);
}
}
and here is my section in my view page(index) i want to loop through
#foreach ($payments as $payment)
<!-- {{ Cryptocap::getSingleAsset($payment->name) }} -->
<div class="card col-md-6 no-padding ">
<div class="card-body">
<div class="h1 text-muted text-right mb-4">
<i class="pe-7s-wallet"></i>
</div>
<div class="h4 mb-0">
<span class="">0.0012930403</span>
</div>
<small class="text-muted text-uppercase font-weight-bold">BTC</small>
<div class="progress progress-xs mt-3 mb-0 bg-flat-color-1" style="width: 40%; height: 5px;"></div>
</div>
</div>
#endforeach
The above approach works on other areas of my application, as this is the approach i have been using throughout the application. But it doesn't seem to work on this particular controller and view.
Can you give this a try
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
For more info visit this link
Passing data from controller to view in Laravel

Passing data from controller to modal

I'm am trying to get data from a database table and passing it to the modal but it is saying the array I am passing in undefined. Here is my Controller:
public function displayLocNotesForModal() {
$notesLoc = Note::all();
return view('/components/callCenter/modalLocNotes', ['notesLoc' => $notesLoc]);
}
Here is my Route:
Route::get('/components/callCenter/modalLocNotes', 'App\Http\Controllers\CallCenter\NoteController#displayLocNotesForModal');
Here is my modal:
<div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
<h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-headline">
Location #{{ $title }} Notes
</h3>
<div class="mt-2">
<p class="text-sm leading-5 text-gray-500">
{{-- {{ $slot }} --}}
#foreach($notesLoc as $notes)
#if($notes == $title)
works
#endif
#endforeach
</p>
</div>
</div>
I think it should be like below, assuming the components folder is in your resources/views folder
return view('components.callCenter.modalLocNotes', ['notesLoc' => $notesLoc]);
Also providing a link to docs Laravel nested view directories

Why do I get Trying to get property 'username' of non-object while "owner_user" has information in it?

HTML Code
#foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">{{ $post->owner_user->username }}</p>
</div>
</div>
#endforeach
Laravel code
public function testpage()
{
return Posts::with(['OwnerUser','like'])->get();
}
when I just return Posts table, it has column owner_user with user information in it.
relations work, but somehow I get "Null" as a result in {{ $post->owner_user->username }} when I return it in HTML code.
because that is not a single object try
#foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">#(foreach $post->owner_user as $user){{$user->username }}#endforeach</p>
</div>
</div>
#endforeach

Laravel 5.2 Undefined variable theme

I'm making a forum and the if the user clicks on a theme, he gets redirected to the page where all the topics are that belong to that theme. What I'm trying to do, is that the user is able to click on the topic and gets redirected to it.
The problem is, is that I get an error saying Undefined variable: theme
As you can see in the code below, I have a foreach loop in my theme.blade.php which loops all the topics that belong to the theme he/she clicked on. In the beginning of the loop there is a <a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}"1 which is supposed to bring the user to the topic that he/she clicked on. The URL that is supposed to show is forum.dev/theme/{theme_id}/topics/{topic_id} But that doesn't work.
And it gives me that error code. All the code is below, If i miss some code, Please inform me about it and i'll update the question. Thanks in advance
theme.blade.php
<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="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}" 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 = 125, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Posted by: {{ $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">Last reply</h6>
<p class="center-align"></p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{theme_id}/topics', 'ThemesController#show')->name('showtheme');
Route::get('/theme/{theme_id}/topics/{topic_id}', 'TopicsController#topic')->name('showtopic');
Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function() {
//THEMES
Route::get('/theme/{theme_id}/edit', 'ThemesController#edit')->name('edittheme');
Route::patch('/theme/{theme_id}/edit', 'ThemesController#update')->name('updatetheme');
Route::get('/theme/create', 'ThemesController#create')->name('createtheme');
Route::post('/theme/create', 'ThemesController#save')->name('savetheme');
Route::delete('/theme/{theme_id}/delete', 'ThemesController#destroy')->name('deletetheme');
//TOPICS
Route::get('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#edit')->name('edittopic');
Route::patch('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#update')->name('updatetopic');
Route::get('/theme/{theme_id}/topics/create', 'TopicsController#create')->name('createtopic');
Route::post('/theme/{theme_id}/topics/create', 'TopicsController#save')->name('savetopic');
Route::delete('/theme/{theme_id}/topics/{topic_id}/delete', 'TopicsController#destroy')->name('deletetopic');
});
Route::get('user/profile', 'UserController#profile')->name('showprofile');
Route::post('user/profile', 'UserController#update_avatar');
TopicsController.php (Show method)
public function show($id)
{
$theme = Theme::find($id);
$topics = Topic::find($id);
return view('topics/topic')->with('topics', $topics)->with('theme', $theme);
}
I don't know if you can use the double '->with' option.
What will work is the following:
return view('topics/topic', ['topics' => $topics, 'theme'=> $theme]);
You pass an array as the second argument of the view. You can add as many keys and variables as you like.

Laravel get data from 2 tables where not null

I'm trying to get some data from two different tables,
So it does look like this now:
But I don't want the empty fields (Globale-Moderator, Moderator and Proef-Moderator) to be shown. How can I do this whit this code;
Controller:
public function ForumTeam()
{
$roles = Role::where('id', '>', '4')->orderBy('id', 'DESC')->get();
return View::make('team')->with('roles', $roles);
}
View:
<div class="panel-group col-sm-offset-1 col-sm-10">
#foreach($roles as $role)
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ $role->name }}</h3>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($role->user as $user)
<li class="list-group-item">
<img src="{{ Config::get('app.url') }}/public/img/avatar.jpg" style="float:left;margin-right:15px;padding-bottom:5px;" class="img-circle" alt="{{ $user->username }}" width="75" height="75">
<h4 style="color:{{ $role->colour }};"><strong>{{ $user->username }}</strong></h4>
<p>{{ $user->usertitle }}</p>
</li>
#endforeach
</ul>
</div>
</div>
#endforeach
</div>
So I want to hide the not filled in fields, how should I do dis propperly?
My other option is to show some text inside of the 'empty' field. Like 'There is nobody with this rank'.
Thanks!
You can/should be able to use:
#foreach($roles as $role)
#if($role->user != null || !empty($role->user->name))
#endif
#endforeach
You can then check to see if the role->user is NOT NULL OR the name of the role is not empty.WHERE name is the name of the role, i.e. "Admin", "Moderator"
Alternatively, try:
#foreach($roles as $role)
#if(count($role->user) >= 1)
// Do stuff
#endif
#endforeach
Since you are getting User which has a one-to-many relationship, therefore there will be more than one user.
You could add an if statement after the first #foreach, to check if the role
has users,
#foreach($roles as $role)
#if(isset($role->user))
...
#endif
#endforeach

Categories