How to Route back with passing parameters using Laravel? - php

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>

Related

how to add button on .php file by editing it

{!! view_render_event('bagisto.shop.layout.header.account-item.before') !!}
<login-header></login-header>
{!! view_render_event('bagisto.shop.layout.header.account-item.after') !!}
<script type="text/x-template" id="login-header-template">
<div class="dropdown">
<div id="account">
<div class="welcome-content pull-right" #click="togglePopup">
<i class="material-icons align-vertical-top">perm_identity</i>
<span class="text-center">
#guest('customer')
{{ __('velocity::app.header.welcome-message', ['customer_name' => trans('velocity::app.header.guest')]) }}!
#endguest
#auth('customer')
{{ __('velocity::app.header.welcome-message', ['customer_name' => auth()->guard('customer')->user()->first_name]) }}
#endauth
</span>
<span class="select-icon rango-arrow-down"></span>
</div>
</div>
<div class="account-modal sensitive-modal hide mt5">
<!--Content-->
#guest('customer')
<div class="modal-content">
<!--Header-->
<div class="modal-header no-border pb0">
<label class="fs18 grey">{{ __('shop::app.header.title') }}</label>
<button type="button" class="close disable-box-shadow" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" class="white-text fs20" #click="togglePopup">×</span>
</button>
</div>
<!--Body-->
<div class="pl10 fs14">
<p>{{ __('shop::app.header.dropdown-text') }}</p>
</div>
<!--Footer-->
<div class="modal-footer">
<div>
<a href="{{ route('customer.session.index') }}">
<button
type="button"
class="theme-btn fs14 fw6">
{{ __('shop::app.header.sign-in') }}
</button>
</a>
</div>
<div>
<a href="{{ route('customer.register.index') }}">
<button
type="button"
class="theme-btn fs14 fw6">
{{ __('shop::app.header.sign-up') }}
</button>
</a>
</div>
</div>
</div>
#endguest
#auth('customer')
<div class="modal-content customer-options">
<div class="customer-session">
<label class="">
{{ auth()->guard('customer')->user()->first_name }}
</label>
</div>
<ul type="none">
<li>
{{ __('shop::app.header.profile') }}
</li>
<li>
{{ __('velocity::app.shop.general.orders') }}
</li>
<li>
{{ __('shop::app.header.wishlist') }}
</li>
<li>
{{ __('velocity::app.customer.compare.text') }}
</li>
<li>
{{ __('shop::app.header.logout') }}
</li>
</ul>
</div>
#endauth
<!--/.Content-->
</div>
</div>
</script>
#push('scripts')
<script type="text/javascript">
Vue.component('login-header', {
template: '#login-header-template',
methods: {
togglePopup: function (event) {
let accountModal = this.$el.querySelector('.account-modal');
let modal = $('#cart-modal-content')[0];
if (modal)
modal.classList.add('hide');
accountModal.classList.toggle('hide');
event.stopPropagation();
}
}
})
</script>
#endpush
I have a php file for creating login page
it look like this page view
how to add a new login button like login with google by editing in it.
Please be detail while giving your answer, it is very helpful for those who are new in php.
It is better to provide general answers that is helpful for those who are working in other domain related to php.
This code belong to bagisto framework which is base on laravel.Bagisto is greate framework for creating ecommerce sites.
This is the repo of bagisto https://github.com/bagisto/bagisto.git
I am new to stackoverflow so feel free to improve my question for more clarity friends if need.
We have added social login in our master branch, you may take a pull from there. Below is the provided PR link
https://github.com/bagisto/bagisto/pull/3367
PS - as the user asked the questions regarding the platform bagisto that's why I am answering for that purpose.

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 **/
}

I can't get the route with full slug/post

I am trying to create a blog, when trying to bring the previous record and the next record in sight this returns the "post" but not the full path that should be / single / {slug}
CONTROLLER
public function single($slug)
{
$post = Post::where('slug', $slug)->first();
$previous = Post::where('id', '<', $post->id)->orderBy('id', 'asc')->where('status', 'PUBLISHED')->first();
$next = Post::where('id', '>', $post->id)->orderBy('id')->where('status', 'PUBLISHED')->first();
return view('web.single')->with(compact('post', 'previous', 'next'));
}
view
#if (isset($previous))
<div class="alert alert-success">
<a href="{{ url($previous->slug) }}">
<div class="btn-content">
<div class="btn-content-title"><i class="fa fa-arrow-left"></i> Previous Post</div>
<p class="btn-content-subtitle">{{ $previous->title }}</p>
</div>
</a>
</div>
#endif
</div>
<div class="col-md-6">
#if (isset($next))
<div class="alert alert-success">
<a href="{{ url($next->slug) }}">
<div class="btn-content">
<div class="btn-content-title">Next Post <i class="fa fa-arrow-right"></i></div>
<p class="btn-content-subtitle">{{ $next->title }}</p>
</div>
</a>
</div>
#endif
Route
Route::get('/single/{slug}', 'Web\WorkController#single')->name('single');
When clicking on previous or next, the route shown by the browser is http://127.0.0.1:8000/et-autem-tempora
and should be
http://127.0.0.1:8000/single/et-autem-tempora
You are creating your url doing this:
<a href="{{ url($next->slug) }}">
...
</a>
The url('path') helper returns something like this: http://{base_url}/{path}.
Try to use your named route instead:
<a href="{{ route('single', ['slug' => $next->slug]) }}">
...
</a>
I think this should also work:
<a href="{{ route('single', $next->slug) }}">
...
</a>

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

Laravel 5.5 Method Links does not exist (Pagination) when applied to user model

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.

Categories