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
Related
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 **/
}
Im working on my Laravel project, and i wanna make some filter buttons for my data table.
Those buttons look like this : https://i.stack.imgur.com/Swpbw.png
I want to display the selected option in dropdown title. For example, the Author title should be changed to any others name like Admin or User etc... after selecting the option from the dropdown button.
Author Dropdown Button (original) :
<div class="dropdown">
<button class="btn btn-outline-dark btn-sm dropdown-toggle mr-2" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Author
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
#foreach($users as $user)
<a class="dropdown-item" href="/posts?user_id={{$user->id}}">
{{ $user->name }}
</a>
#endforeach
</div>
</div>
I found some solutions, they all look like this :
$(".dropdown-menu").on('click', 'a', function(){
$(".btn:first-child").text($(this).text());
$(".btn:first-child").val($(this).text());
});
But the problem is :
The solution above only works if the href (url) from dropdown-menu is fixed (href="#").
In my project, those dropdowns have different href, so the title can only be changed for a short moment after clicked, then back to default after the url changed.
I think we need to catch the url then make some conditions to display the correct title.
For example, my Category filter only have 3 fixed options so i can do it like this.
Category Dropdown Button :
#php
$request = Request::getRequestUri();
#endphp
<div class="dropdown">
<button class="btn btn-outline-dark btn-sm dropdown-toggle mr-2" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
#switch(true)
#case(strstr($request, 'category_id=1'))
Pc
#break
#case(strstr($request, 'category_id=2'))
Console
#break
#case(strstr($request, 'category_id=3'))
Handheld
#break
#default
Category
#endswitch
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="{{strstr($request, 'category_id=1') ? 'dropdown-item active' : 'dropdown-item'}}" href="/posts?category_id=1">
Pc
</a>
<a class="{{strstr($request, 'category_id=2') ? 'dropdown-item active' : 'dropdown-item'}}" href="/posts?category_id=2">
Console
</a>
<a class="{{strstr($request, 'category_id=3') ? 'dropdown-item active' : 'dropdown-item'}}" href="/posts?category_id=3">
Handheld
</a>
</div>
</div>
Meanwhile, i have a foreach loop in the Author filter, cause the number of users is not fixed. So i cannot use the same code like i did with Category.
I had tried this but it just doesn't work.
Author Dropdown Button (not working) :
<div class="dropdown">
<button class="btn btn-outline-dark btn-sm dropdown-toggle mr-2" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
#if($request == '/posts' OR strstr($request, 'posts?page='))
Author
#else
#foreach($users as $user)
#if(strstr($request, 'user_id={{$user->id}}'))
{{ $user->name }}
#endif
#endforeach
#endif
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
#foreach($users as $user)
<a class="dropdown-item" href="/posts?user_id={{$user->id}}">
{{ $user->name }}
</a>
#endforeach
</div>
</div>
It cannot display the user name on title when clicked (blank result).
I also tried this one, but not working either.
#foreach($users as $user)
#if($request == "/posts?user_id={{$user->id}}"))
{{ $user->name }}
#endif
#endforeach
Please help guys !
you still can use the JS solution by preventing the redirection of the link
$(".dropdown-menu").on('click', 'a', function(event){
event.preventDefault();
event.stopPropagation();
$(".btn:first-child").text($(this).text());
$(".btn:first-child").val($(this).text());
});
I've found the solution.
The if condition (inside the foreach) is right, but somehow it doesn't work in blade template.
So, just put it in the php section.
Author Dropdown Button (working) :
<div class="dropdown">
<button class="btn btn-outline-dark btn-sm dropdown-toggle mr-2" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
#if($request == '/posts'
OR strstr($request, 'posts?search=')
OR strstr($request, 'posts?page='))
Author
#else
#foreach($users as $user)
#php
if(strstr($request, 'user_id='.$user->id)){
echo $user->name;
}
#endphp
#endforeach
#endif
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
#foreach($users as $user)
<a class="dropdown-item" href="/posts?user_id={{$user->id}}">
{{ $user->name }}
</a>
#endforeach
</div>
</div>
I am trying to figure out why my pagination stops working as soon as I apply it to a user model. Here is the scenario: I am making a website which is a note takes, a user can take notes save them and all that. On a page /notes all notes made can be seen used for me to see weather each user is able to save notes. Pagination works there without a problem. Once I made a user Dashboard and I show notes they made with their user id I get this error:
Method links does not exist
…\vendor\laravel\framework\src\Illuminate\Support\Traits\Macroable.php
96
I have no clue why this is happening. When I delete the code from view: {{$notebooks->links()}} - the error goes away and so does pagination. The moment I put it in the error happens all over again.
Here is the code from my view and controllers.
DashboardController:
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
$notebooks = DB::table('notebooks', $user->notebooks)->paginate(4);
return view('dashboard')->with('notebooks', $user->notebooks);
}
}
Dashboard View (with paginate links code):
#extends('layouts.app')
#section('content')
<!-- Main component for call to action -->
<div class="container text-center">
<!-- heading -->
<h1 class="pull-xs-left">Your Dashboard</h1>
<br>
<div class="pull-xs-right">
<a class="btn btn-primary" href="/notebook/create" role="button"> New Note +</a>
</div>
<div class="pull-xs-right">
<a class="btn btn-primary" href="/contacts/create" role="button"> New Contact +</a>
</div>
<div class="pull-xs-right">
<!--the dropdown menu -->
<div class="dropdown show">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Sort Notebooks By
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="{{action('NotesController#ascendingId')}}">Note Number A - Z</a>
<a class="dropdown-item" href="{{action('NotesController#descendingId')}}">Note Number Z - A</a>
<a class="dropdown-item" href="{{action('NotesController#descendingTime')}}">Latest Time Posted</a>
<a class="dropdown-item" href="{{action('NotesController#ascendingTime')}}">Oldest Time Posted</a>
</div>
</div>
</div>
<div class="clearfix">
</div>
<br>
#if(count($notebooks) > 0)
#foreach ($notebooks as $notebook)
<div class="col-sm-3 col-md-3 col-sm-6">
<div class="card" style="width: 26rem;">
<div class="card-header" >
Client: {{$notebook->client_name}}
</div>
<div class="card-block" >
<h4 class="card-title " style="height: 5rem;"><strong><a href="/notebook/{{$notebook->id}}" >{{$notebook->name}}</a></strong></h4>
<p class="card-text" style="height: 7rem;">{{$notebook->note_description}}</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item"><b>Last Updated:</b> {{$notebook->updated_at->format('d-m-Y')}}</li>
<li class="list-group-item">
Edit Note
</li>
</ul>
<div class="card-block">
{!!Form::open(['action' => ['NotesController#destroy', $notebook->id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
</div>
</div>
</div>
#endforeach
</div>
<!-- /container -->
<hr>
<div class="container text-center">
{{$notebooks->links()}}
</div>
</div>
#else
<div>
<div class="col-lg-12 no-notes">
<p>No notes found, go ahead and make your first note! :)</p>
<a class="btn btn-primary" href="/notebook/create" role="button"> Make My First Note</a>
</div>
</div>
#endif
#endsection
Just a reminder this works perfectly without the user model. I am lost as how to fit it. Using Laravel 5.5.26.
Change the code to:
$notebooks = auth()->user()->notebooks()->paginate(4);
return view('dashboard')->with('notebooks', $notebooks);
Also, you do not need to get a user from DB since you can access auth()->user() instance in a view.
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.
I am working on Laravel 5.0. I want to return back from my current page to the previous page with passing parameter. I have an id of all images and galleries in my urls. After clicking on a particular image, i move to another page with the id of that image in my url, Now, i want to return back to my gallery page to which that image belongs to.
My controller:
public function viewgallerypics($id){
$gallery= Gallery::findorFail($id);
return view('dropzone' , ['gallery'=>$gallery]);
}
public function bigimage($id){
$gallery=Gallery::all();
$image=image::findorFail($id);
return view('bigimage' , ['image'=>$image,'gallery'=>$gallery]);
}
My routes:
Route::get('/image/big/{id}' ,[
'uses'=>'GalleryController#bigimage',
'as'=>'bigimage'
]);
Route::get('/gallery/view/{id}' ,[
'uses'=>'GalleryController#viewgallerypics',
'as'=>'viewpics'
]);
My view:
<section class="col-md-1">
<a class="btn btn-primary btn-lg"
href="HERE, WHAT I HAVE TO PASS TO GET MY DESIRED
PAGE????">Back</a>
</section>
My desired page where i want to return back depending on the id pass through the route:
<div class="row">
<div class="col-md-offset-1 col-md-10">
<div id="gallery-images">
<ul>
#foreach($gallery->images as $image)
<li>
<a href="{{ URL('/image/big/'.$image->id) }}">
<img id="jumboimage2" src="{{ url($image->file_path) }}"></a>
</li>
<li>
<a href="{{ URL('/image/delete/'.$image->id) }}" id="margin">
<span id="margin" class="glyphicon glyphicon-remove-sign"></span></a>
</li>
#endforeach
</ul>
</div>
</div>
</div>
use {{ URL::previous() }}
<section class="col-md-1">
<a class="btn btn-primary btn-lg" href="{{ URL::previous() }}">Back</a>
</section>