product detail page is not showing - laravel

hi m trying to get product details by clicking on product at product listing page, when I click on product link then at next page URL is fine http://127.0.0.1:8000/product/2 but product is not showing
controller:
public function productdetail(Request $request, Product $product)
{
return view('product.detail', compact('product'));
}
route:
Route::get('/product/{product}','Admin\ProductController#productdetail')->name('product.productdetail');
detail.blade.php
<form method="POST" action="{{ route('product.productdetail', $product->id) }}" enctype="multipart/form-data">
#csrf
{{ $product->product_name }}
</form>
at listening page m using this link for detail page:
<a href="{{ route('product.productdetail', $product->id) }}" class="block2-name dis-block s-text3 p-b-5">
{{ $product->product_name }}
</a>

Ok, I found where problem is when you provide your controller and routes.
You got route resource:
Route::resource('product','ProductController');
And your route for details:
Route::get('/product/{product}','Admin\ProductController#productdetail')->name('product.productdetail');
Resource will provide same route for show, so you need to change your route with additional parameter like:
Route::get('/product/{product}/details','Admin\ProductController#productdetail')->name('product.productdetail');
Now you can point to that route and I think now will work.

Related

How to check which controller data object being executed?

I have faced a typical problem. The problem is which controller data object being executed in view page. Here the data object is $items. I have looked most of controller but can not find data object $items . Please help me to find it. Thank in advance. The portion of view page :
#foreach($items as $item)
<div class="partner-logo">
<div class="inner-div red-box">
<p>
<img src="{{ ($item->image) ? asset('file/images/' . $item->image) : "" }}" alt="" />
</p>
<h3>
<a
href="{{ isset($item->url) ?route('page.content.show', $item->url) : "#" }}"> {{ $item->title }}
</a>
</h3>
</div>
</div>
#endforeach
In Laravel, controllers usually pass their data to views in this format:
return view('nameOfView', [
'firstParam' => $firstParam,
'secondParam' => $secondParam,
'etc' => $etc
]);
So you can search in your controllers for the view name and then for the parameter name (in this case $items). If there are multiple controller functions with the $items param and the same view, add a test param to each and see which one your view renders. You can also check in routes/web.php for the current URL you are on, and find the correct controller function that way.

route is unable to run Controller

I'm new to laravel... created a route and given its a controller which has a method to update a database.. but once it reads the route the app is unable to get to the controller
Route::post('/workorder/store/third/{$id}',
[
'uses'=>'WorkOrdersController#storeThird',
'as'=>'workorder.store.third'
]);
//method in WorkOrderController
public function storeThird(Request $request,$id)
{
$this->validate($request,[
'address_region'=>'required|string',
'address_no'=>'required|string',
]);
$workorder = WorkOrder::find($id);
$workorder->address_region = $request->address_region;
$workorder->address_no = $request->address_no;
$workorder->save();
return view('third-workorder',compact('workorder'));
}
results in browser ...
in the address bar.. "http://localhost:8000/workorder/store/third/9"
and in the browser .."Sorry, the page you are looking for could not be found."
this.. view.blade looks like
<div class="modal" id="createThirdWorkshopModal">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" action="{{ route('workorder.store.third',['id'=>$workorder->id]) }}" >
{{ csrf_field() }}
Navigating directly to http://localhost:8000/workorder/store/third/9 in your browser is a GET request, but you have the route defined as a route that handles a POST request. You need to submit something for that route to be "found":
<form method="POST" action="{{ url("/workorder/store/third/".$id) }}" ...>
...
</form>
or define the route as
Route::any("/workorder/store/third/{$id}", ...);
to handle this.
Note: ::any() handles all HTTP verbs (GET, POST, PUT, DELETE, etc.)
Router::post('/workorder/store/third/{$id}', WorkOrdersController#storeThird)->name('workorder.store.third');
and use:
<a href="{{route('workorder.store.third', $id)}}">
...
</a>

Laravel NotFoundHttpException although route exits

I added a new route as:
Route::post('friendSend/{uid}','FriendController#sendFriendRequest')->name('friends.add');
and called it as a hyperlink to submit form:
<a href="{{route('friends.add',$user->uid)}}"
onclick="event.preventDefault();
document.getElementById('addfriend-form).submit();">
<i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>
<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
{{ csrf_field() }}
</form>
However when I click on the said link, I get redirected to /friendSend with the said error.
the route is visible in:
php artisan route:list
which makes sense since I called it via it's name 'friends.add'. It doesn't even go the controller.
I've already tried the following:
Laravel NotFoundHttpException although route exists
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Friend;
use Auth;
class FriendController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return redirect()->route('home');
}
public function sendFriendRequest($id)
{
echo "hello world";
}
}
Update:
Manually entering the url as /friendSend/2 (or any number for that matter) works.
You are missing a ' in the onclick javascript.
<a href="{{route('friends.add',$user->uid)}}"
onclick="event.preventDefault();
document.getElementById('addfriend-form').submit();">
<i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>
<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
{{ csrf_field() }}
</form>
If this doesn't work, what is the href on the generated page? Do you have multiple of these forms on a single page?
I thing you are doing wrong on set the url in form submit.You are adding route like
href="{{route('friends.add',$user->uid)}}"
Just modified it as
href="{{route('friends.add',['uid' => $user->uid])}}"
You can refer Laravel Named Routes

Logging out via a link in Laravel

I have a "Logout" link in my top navigation bar. I'm wondering how I can make it so that while I'm logged in, it'll log me out when I click on it and return me to the homepage.
To be specific, what changes to which files do I make in Laravel? Also, what code do I need to write in the view, which currently contains just HTML, to trigger this?
When you run php artisan make:auth, the default app.php in Laravel 5.5 does it like this:
<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
Edited 28/12/2019: It's work, but This answer contains a serious security issue. Please consider before using it. The Answer by Lucas Bustamante maybe a better choice. Refer to the comment section of this answer.
1) if you are using the auth scaffold that laravel contains. You can do this, in your navigation bar add this:
logout
then add this to your web.php file
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController#logout');
Done. This will logout you out and redirect to homepage. To get the auth scaffold, from command line, cd into your project root directory and run
php artisan make:auth
2) add this to your navigation bar:
logout
then add this in your web.php file
Route::get('/logout', 'YourController#logout');
then in the YourController.php file, add this
public function logout () {
//logout user
auth()->logout();
// redirect to homepage
return redirect('/');
}
Done.
Read:
https://mattstauffer.co/blog/the-auth-scaffold-in-laravel-5-2
https://www.cloudways.com/blog/laravel-login-authentication/
Use the logout() method:
auth()->logout();
Or:
Auth::logout();
To log users out of your application, you may use the logout method on the Auth facade. This will clear the authentication information in the user's session.
if you want to use jQuery instead of JavaScript:
<a href="javascript:void" onclick="$('#logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
As the accepted answer mentions that logging out via GET has side effects you should use the default POST route already created by Laravel auth.
Simply create a little form and submit it via link or button HTML tag:
<form action="{{ route('logout') }}" method="POST">
#csrf
<button type="submit">
{{ __('Logout') }}
</button>
</form>
If you use guard you can logout using this line of code :
Auth::guard('you-guard')->logout();
in laravel 8.x
#csrf
<x-jet-dropdown-link href="{{ route('logout') }}"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Logout') }}
</x-jet-dropdown-link>
</form>

Laravel route not defined error even though it is

I have the following blade code:
<a href="{{ route('settings') }}">
<span class="title">Settings</span>
</a>
I have the following defined at the top of my routes.php file:
Route::post('settings/update', 'SettingsController#update');
Route::resource('settings', 'SettingsController');
When I try to go to any page with route('settings') I get a Route [settings] not defined error.
If I do php artisan routes I can see that the settings routes are all there as expected.
With route('settings') you refer to a route named settings but you don't have such a route. RESTful routes will automatically receive a route name though.
For the index method it is resource.index for the show method resource.show and so on.
Change your code to this:
<a href="{{ route('settings.index') }}">
<span class="title">Settings</span>
</a>
Change {{ route('settings') }} to {{ URL::to('settings') }}

Resources