How to set URL so it loads the user page - php

In route.php I have the following...
Route::get('/user/{username}', array(
'as' => 'profile-user',
'uses' => 'ProfileController#user'
));
In ProfileController I have the following...
class ProfileController extends BaseController {
public function user($username) {
$user = User::where('username', '=', $username);
if($user->count()) { // if the corresponding user exists...
$user = $user->first();
return View::make('profile.user')
->with('user', $user);
}
return App::abort(404);
}
}
In navigation.blade.php I have the following...
<li>User Profile</li>
How can I make it so that navigation.blade.php will provide the correct link to the user profile? At the moment the link looks like the following in html...
http://website.dev/user/%7Busername%7D
I'd like it to look like this:
http://website.dev/user/currentlyLoggedInUserName

You can try using HTML::link. Example:
{{ HTML::link('/user/<?=$user_name?>', 'User Profile')}}

You should use
<li>User Profile</li>
because it is a Laravel PHP function url() that must resolve first, and the result is passed to Handlebars.

This is how I solved it. I'll leave the answer here for other people.
<li>User Profile</li>

Related

Undefined variable problem on Laravel 9.x

I'm trying to get a my title variable from my control page and display it on the about page.
I don't think I have a typo, but it might me. I'm not sure.
Here is my control page code;
class PagesController extends Controller
{
public function index(){
$title = 'Welcome to Laravel';
return view ('pages.index')->with('title', $title);
}
public function about(){
$title = 'About us';
return view ('pages.about')->with('title', $title);
}
public function services(){
$title = 'The services';
return view ('pages.services')->with('title', $title);
}
}
In this page, the index and services functions work fine, but I can't get the about page.
Here is my display pages;
This is Index page
#extends('layouts.app')
#section('content')
<h1>{{$title}}</h1>
<p>This is the Laravel Application</p>
#endsection
This is the about page:
#extends('layouts.app')
#section('content')
<h1>{{$title}}</h1>
<p>This is the About page</p>
#endsection
The error I have
Do this:
return view ('pages.index', compact('title'));
or:
return view ('pages.index', [
'title' => $title
]);
Since you are returning just the title, there isn't any need to call any verbs. Rather you should directly call the view:
route::view('/about', 'Pagecontroller#about');
or
pass the parameter by compact:
return view ('pages.index', compact('title'));
or
return view ('pages.index', ['title' => $title]);
Since this is a test application from a lesson, I forgot to delete some extra code in my route file.
This is my route file:
Route::get('/', 'App\Http\Controllers\PagesController#index');
Route::get('/about', 'App\Http\Controllers\PagesController#about');
Route::get('/services', 'App\Http\Controllers\PagesController#services');
The commented area shouldn't be here. That was the whole problem over here...
// Route::get('/about', function(){
// return view ('pages.about');
// });
This form of passing variables is a short-lived entry of a variable into the session. Then accessing the variable on the page should look like this:
{{ session('title') }}
If you want to pass data to the view, then you need to use the method
return view('pages.services', ['title' => $title]);
Laravel views

Laravel undefined variable in view

I'm new to laravel. Using version 5.3 and tried to search but don't see what I'm doing wrong. I keep getting an "Undefined variable: user" in my view. I'm also doing form model binding. Model binding works properly when manually entering URL. Just can't click on link to bring up edit view.
My routes:
Route::get('/profile/edit/{id}', 'ProfileController#getEdit');
Route::post('/profile/edit/{id}', 'ProfileController#postEdit');
My controller:
public function getEdit($id){
$user= User::findOrFail($id);
return view('profile.edit', compact('user'));
}
My view:
<li>Update profile</li>
My form:
{!! Form::model($user,['method' => 'POST', 'action'=> ['ProfileController#postEdit', $user->id]]) !!}
public function getEdit($id){
$user= User::findOrFail($id);
return view('profile.edit', ['user' => $user]);
}
public function postEdit($id){
$user= User::findOrFail($id);
return view('profile.edit', ['user' => $user]);
}
Try this for pass data array in view
public function getEdit($id){
$data['user'] = User::findOrFail($id);
return view('profile.edit')->withdata($data);
}
in view page try to print $data
{{ print_r($data) }}
Please edit your getEdit method
public function getEdit($id){
$user= User::findOrFail($id);
dd($user);
return view('profile.edit', compact('user'));
}
check if there is any data user variable.
In my opinion, the statement in your view
<li>Update profile</li>
is causing the issue. You may confirm this by looking at the URL in the address bar after clicking on the Update profile link.
Try changing it to this
<li>Update profile</li>

pass auth::user as data variable with route

I want that when the user click the profile page i want to pass Auth::user()->username as argument to my userController's show method.I have the profile link as following:
<li>Profile</li>
And in my route i have the following route
Route::get('/profile/{username}',function(){
return View::make('user.show')->with($username);
});
my question is how i can set username in my '/profile/{username}' as Auth::user()->username when i click the profile link?currently the profile link does not attach any parameter with it
First of all
{{URL::to('/profile')}} is not pointing to Route::get('/profile/{username}) url,there are two different routes
So what you need to do is either change the link , i.e.
{{URL::to('/profile/' . \Auth::user()->username)}}
and then in your route file
Route::get('/profile/{username}',function($username){
return View::make('user.show')->with(['username' => $username]);
});
//note that you need to pass the array in with() method
or you can do this
Route::get('/profile/{username}',function($username){
return View::make('user.show',compact('username'));
});
When the user clicks on profile link:
<li>
My Profile
</li>
The UserController#show method is called.
<?php
// routes.php
Route::get('profile/{username}', 'UserController#show')->name('user.show');
// UserController.php
public function show($username)
{
$user = User::whereUsername($username)->first();
return view('user.show', compact('user'));
}
and a View response is returned to the user.
#update
If you need is just redirect the control to the UserController#show method, you can do this:
<li>
My Profile
</li>
<?php
// routes.php
Route::get('profile/{username}', function ($username) {
return redirect()->route('user.show', Auth::id());
})->name('user.profile');
Now if you want customize the UserController#show action:
<li>
My Profile
</li>
The UserController#show method is called.
<?php
// routes.php
Route::resource('user', 'UserController', ['except' => ['show']);
Route::get('profile/{username}', 'UserController#profile')->name('user.profile');
Now you can delete the UserController#show method if you want or change the profile method name to show.
// UserController.php
public function profile($username)
{
$user = User::whereUsername($username)->first();
return view('user.show', compact('user'));
}
A quick way is to setup a redirect from /profile and it won't break the functionality if they want to view someone else's profile.
Route::get('/profile',function(){
return Redirect::to('/profile/'.Auth::user()->username);
}
However, I'd recommend doing an Auth::check() before the redirect.
i did something like the following
<li>Profile</li>
and in route.php:
Route::get('/profile',function(){
return redirect()->route('user.show',[Auth::user()->username]);
});

How to pass value inside href to laravel controller?

This is code snippet from my view file.
#foreach($infolist as $info)
{{$info->prisw}} / {{$info->secsw}}
#endforeach
Here is my route which I defined inside route file
Route::get('switchinfo','SwitchinfoController');
I want to pass two values inside href tag to above route and retrieve them in controller. Can someone provide code to do this thing?
Since you are trying to pass two parameters to your controller,
You controller could look like this:
<?php namespace App\Http\Controllers;
class SwitchinfoController extends Controller{
public function switchInfo($prisw, $secsw){
//do stuffs here with $prisw and $secsw
}
}
Your router could look like this
$router->get('/switchinfo/{prisw}/{secsw}',[
'uses' => 'SwitchinfoController#switchInfo',
'as' => 'switch'
]);
Then in your Blade
#foreach($infolist as $info)
Link
#endforeach
Name your route:
Route::get('switchinfo/{parameter}', [
'as'=> 'test',
'uses'=>'SwitchinfoController#function'
]);
Pass an array with the parameters you want
<a href="{{route('test', ['parameter' => 1])}}">
{{$info->prisw}} / {{$info->secsw}}
</a>
and in controller function use
function ($parameter) {
// do stuff
}
Or if don't want to bind parameter to url and want just $_GET parameter like url/?parameter=1
You may use it like this
Route::get('switchinfo', [
'as'=> 'test',
'uses'=>'SwitchinfoController#function'
]);
function (){
Input::get('parameter');
}
Docs
You can simply pass parameter in your url like
#foreach($infolist as $info)
<a href="{{ url('switchinfo/'.$info->prisw.'/'.$info->secsw.'/') }}">
{{$info->prisw}} / {{$info->secsw}}
</a>
#endforeach
and route
Route::get('switchinfo/{prisw}/{secsw}', 'SwitchinfoController#functionname');
and function in controller
public functionname($prisw, $secsw){
// your code here
}

Controller Method not defined error - Laravel 5.1

I am trying to display the currently logged in username, as a link to the user info, in my main navigation. I can get the name to display, so I am pulling the info from the db, and passing it to the view OK. But, when I try to make it a link, I get the method not defined error.
Here is how I pass the user info to the navigation (as the var $userInfo):
public function index()
{
$Clients = \Auth::user()->clients()->get();
$userInfo = \Auth::user();
return view('clients.index', compact('Clients', 'userInfo'));
}
Here is the relevant bit from my navigation:
<ul class="nav navbar-nav">
<li>{!! link_to_action('AuthController#show', $userInfo->username, [$userInfo->id]) !!}</li>
</ul>
The method from my controller:
protected function show($id)
{
$userInfo = User::findOrFail($id);
return view('users.show', compact('userInfo'));
}
And, the route definition:
// User Display routes
Route::get('auth/{id}', 'Auth\AuthController#show');
Here is the error I get:
Action App\Http\Controllers\AuthController#show not defined.
Can anyone tell me what I am missing?
First, you need to make your AuthController::show() method public:
public function show($id)
{
$userInfo = User::findOrFail($id);
return view('users.show', compact('userInfo'));
}
Second, as your controllere is in App\Http\Controllers\Auth namespace, you need to use the **Auth** prefix in the view:
<ul class="nav navbar-nav">
<li>{!! link_to_action('Auth\AuthController#show', $userInfo->username, [$userInfo->id]) !!}</li>
</ul>

Categories