Laravel 404 Not Found - But Route Exists - laravel

Hello im trying to get information about user on my view.
Here is my UserController
public function getUser(Request $request, $id)
{
$user = User::findOrFail($id);
return view('admin.user', ['user' -> $user]);
}
here is my web.php
Route::get('admin/user/{id}', "UsersController#getUser");
and my user view
#extends('admin.layouts.app')
#section('contents')
<h1>User {{ $user }} </h1>
#endsection
I am trying to display user information in this view, like name etc, but im recives 404
Not Found page. What im doing wrong. Im using Laravel 6

404 error may refer to a User not being found, since you have a findOrFail() query. It may have nothing to do with your routes.
Just double check with:
php artisan route:list
just to make sure the route is being registered correctly.

I think you firstly use any prefix for this route.For this it will give you an error.To check route list.
php artisan route:list
it will give you all route.
And Here you don't need (Request $request) because here you just need the id.it not the problem..i give you just this suggestion
public function getUser($id)
{
$user = User::findOrFail($id);
return view('admin.user', ['user'=> $user]);
}
why you use '-> ' you should use '=>'

Related

Is there any way to find out the Laravel default session die path URL in Laravel project?

Actually, I am trying to get the default Laravel session die path URL but I can't find the default Laravel session die path URL. Is there anyone who can help me to find out the default Laravel session die URL path?
you can hit url for example
www.abc.com/logout
and if you want to change redirection or something and you have laravel
basicc setup you can find laravel logout function in LoginController.php File
Something like thisenter code here
public function logout(Request $request)
{
$user = auth()->user();
activity($user->name)
->performedOn($user)
->causedBy($user)
->log('LoggedOut');
$this->guard()->logout();
$request->session()->invalidate();
return redirect('/');
}

Apps route redirecting the wrong URL

------Platform Laravel 7x. --------
I am stuck in a simple problem. I can't find the error. While I am updating a form, it redirects to a wrong URL which I don't want and data doesn't update.
Form action url:
method="POST" action="{{'city/update/'. $editCity->id}}"
form image
Route:
Route::post('city/update/{id}','Admin\CityController#update');
web route
Function from controller:
public function update(Request $request, $id)
{
$editCity=City::find($id);
$editCity->city_name=$request->city_name;
$editCity->save();
return redirect()->back();
}
function from controller
When I click on update it goes to this URL and shows 404 error which I don't want:
public/panel/city/edit/city/update/33
Help me to find out the problem where is the mistake I have done. I want to make it updated when I click on the update button and return back.
Use name route instead.
So your code will look like :
blade.php
method="POST" action="{{ route('city.update', $editCity->id) }}"
web.php
Route::post('city/update/{id}','Admin\CityController#update')->name('city.update');
In case your CityController is a resource controller, this what you should try:
Route: web.php
Route::resource('city', 'Admin\CityController');
Form: HTML
<form action="{{route('city.update',$editCity->id)}}" method="post">
Controller: CityController.php
public function update(Request $request, $id)
{
$editCity=City::find($id);
$editCity->city_name=$request->city_name;
$editCity->save();
return back()->with('success','city added successfully!');
}
I hope it helps !
While creating your controller did you run such command ?:
php artisan make:controller Admin\CityController --resource
You should create the controller as a resource controller, then declare it
in your routes like this:
Route::resource('city', 'Admin\CityController');
Ps: Make sure to remove in web.php, your old route:
Route::post('city/update/{id}','Admin\CityController#update')->name('city.update');

data not displayed when passed from controller to view

I am trying to display data sent from Controller to view in Laravel 5.8 but when I do in my controller:
return view('index', ['user'=> 'mark']
and I try to display it in the view using:
{{$user}}
nothing is displayed, why and how to solve this please?
when I do dd($user) in view I get 'mark' but it is not displaying using previous method
I tried what is in Laravel 5.8 documentations
you can try this:
//controller
$data['user'] = 'mark';
return view('index', $data);
//your view
{{ $user }}
In your controller, put:
$user = 'mark';
return view('index', compact('user'));
In your view, you can now access the user through Blade templating:
{{ $user }}

Laravel 5.8 error trying to get the 'id' property of non-object

I'm developing an application with Laravel 5.8. In my application, I have a controller that handles backend articles, and it works. I want to display my user-side information in such a way that a user can click on a link and see the detail of an article. For that, I have created a new controller a with a new namespace for the function show my redirection of navigation in different page does not focus that it is en route or URL with Laravel 5.8. Below is the function.
namespace App\Http\Controllers\Cybernaut;
use App\History;
use App\Http\Controllers\Controller;
class HistoryController extends Controller
{
public function show($id)
{
$history = History::find($id);
return view('show_history', compact('history'));
}
}
At the level of the home page I wanted to have my links like these:
<li><a data-hover="history" href="{{route('history.show',$history→id)}}"><span>history</span></a></li>
Error
ErrorException (E_ERROR) Property [id] does not exist on this
collection instance. (View:
C:\laragon\www\venome\resources\views\layouts\partial\header.blade.php)
And here is the route used for the show function.
Route::group(['namespace'=>'cybernaut'], function (){
Route::get('/history/{slug}','HistoryController#show')->name('history.show');
});
Try after modifying the thing I have these at the route level now.
Route::get('/', 'FrontController#index')->name('index');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/admin/dashboard', 'DashboardController#index')->name('admin.dashboard');
Route::group([], function () {
Route::get('/history', 'HistoryController#index')->name('history.index');
Route::get('/history', 'HistoryController#create')->name('history.create');
Route::get('/history/edit', 'HistoryController#update')->name('history.update');
Route::get('/history', 'HistoryController#destroy')->name('history.destroy');
});
Route::group(['namespace' => 'cybernaut'], function () {
Route::get('/history/{history}', [
'as' => 'show',
'uses' => 'HistoryController#show'
]);
});
At the level of the homepage I wanted to put my link like those here now;
#foreach($history as $history)
<li><a data-hover="history" href="{{url('/history/'.$history->id)}}"><span>history</span></a></li>
#endforeach
I have this error now:
Trying to get property 'id' of non-object (View:
C:\laragon\www\venome\resources\views\layouts\partial\header.blade.php)
I want an internaut to be able to navigate between the pages.
You have a conflict of variables on your homepage.
#foreach($history as $history)
should be
#foreach($histories as $history)
where $histories is filled in in your FrontController.
$histories = History::all();
When actually getting your single history object, I agree with Sapnesh's answer that you best doublecheck whether or not the object actually exists.
The error occurs because find() returns NULL when a model is not found.
find($id) takes an id and returns a single model. If no matching model
exist, it returns null.
findOrFail($id) takes an id and returns a single model. If no matching
model exists, it throws an error.
In your show() method,
Change:
$history = History::find($id);
To:
$history = History::findOrFail($id);

Method Illuminate\Auth\SessionGuard::users does not exist

I'm having a problem with Auth. I'm just learning about Laravel, I'm doing login. I don't know how to fix it it says:
Method Illuminate\Auth\SessionGuard::users does not exist.
this is my code in login function
public function getlogin(Request $request){
$this->validate($request, [
'email'=> 'required|max:32',
'password'=> 'required|max:32|min:8',
]);
if (Auth::attempt(['email'=>$request->email,'password'=>$request->password])) {
$user = users::where('email','=',$request->email)->first();
return redirect('/messenger')->with('usersignin');
}
return "ooopps something wrong";
}
and this is where the name from the database will be display
<div class="">
<h1>Welcome
#if(session('user'))
{{session('user')}}
#elseif(session('usersignin'))
{{ucwords(Auth::users()->fname)}}
#endif</h1>
</div>
You need to use user instead of users, user is provided with Auth and will get the current logged in user id.
$id = \Auth::user()->id;
Or you want to get the user
$user = \Auth::user();
I solved this error by running the below command
php artisan jwt:secret
php artisan cache:clear
php artisan config:cache
in your if statement just use it as below
if (auth()->attempt(['email'=>$request->email,'password'=>$request->password])) {
$user = users::where('email','=',$request->email)->first();
return redirect('/messenger')->with('usersignin');
}

Resources