Laravel Error : ErrorException Missing argument 1? - php

I am using Laravel 4 and I am getting ERROR: when I visit admin/profile/: Missing argument 1 for AdminController::getProfile()
My AdminController code :
public function getProfile($id) {
if(is_null($id)) Redirect::to('admin/dashboard');
$user = User::find($id);
$this->layout->content = View::make('admin.profile', array('user' => $user));
}
My routes.php :
Route::controller('admin', 'AdminController');
My admin/profile (blade) view :
#if(!is_null($user->id))
{{ $user->id }}
#endif
How could I fix this? I want when they go to admin/profile without ($id) to redirect to dashboard.

You told Laravel that your getProfile method has one parameter:
public function getProfile($id) {
}
If you want to a request to succeed, you have to pass it in your URL:
http://appdev.local/admin/profile/1
If you want to see it fail (redirect to dashboard), you'll have to add a default value to your function argument:
public function getProfile($id = null) { ... }
But you better add this value to it anyway, since you can have bots (or even people) trying to access that route without the parameter.
Your view is too generic too, you have to check if your $user is set:
#if(isset($user) && !is_null($user->id))
{{ $user->id }}
#endif
As noted in the comments, the line
if(is_null($id)) Redirect::to('admin/dashboard');
Must have a return:
if(is_null($id)) return Redirect::to('admin/dashboard');
About sharing the user to your layout, the problem is that your getProfile($id) is already passing a $user to your view, so what you could do is to add this to your __construct():
if (Auth::check())
{
$user = Auth::getUser();
View::share('loggedUser', $user);
}
And in your view:
#if(isset($user) && !is_null($user->id))
{{ $user->id }}
#else
{{ $loggedUser->id }}
#endif
About the user not found problem, you have many options, this is one:
public function getProfile($id) {
if (is_null($id))
{
return Redirect::to('admin/dashboard');
}
if ($user = User::find($id))
{
$this->layout->content = View::make('admin.profile', array('user' => $user));
}
else
{
return Redirect::to('admin/dashboard')->withMessage('User not found');
}

Try setting a default null value to $id like this :
public function getProfile($id = null) {
...
}

Related

Why Is Laravel Giving Me This Error, and How Do I Fix it? Argument #2 ($post) must be of type App\Models\Post, App\Models\User given

I am trying to use a Gate to see if the currently authenticated user matches the user_id column of the "posts" table.
However, when attempting to use the Gate inside my controller, it is giving me the following error, and I am at a loss.
App\Providers\AuthServiceProvider::App\Providers\{closure}(): Argument #2 ($post) must be of type App\Models\Post, App\Models\User given, called in [path]\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php on line 535
Thanks.
My Controller:
class updatePost extends Controller
{
public function updatePost(Request $request, Post $post) {
if (Gate::allows('updatePost', auth()->user(), $post)) {
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect()->route('readPost', ['id' => $post->id]);
} else {
echo 'ERROR';
}
}
}
My Gate:
Gate::define('updatePost', function (User $user, Post $post) {
return $user->id === $post->user_id;
});
The define callback always receives the logged in user as first parameter followed but the parameters given by allows().
Try this
if (Gate::allows('updatePost', $post)) {
// your code
}

Undefined variable $user->username (Laravel 5.7)

I can't get the data from the database. Getting an error:
ErrorException (E_ERROR)
Undefined variable: user
(View:/Users/alex/Desktop/sites/tj/resources/views/user/submissions.blade.php)
Controller:
public function __construct()
{
$this->middleware('auth', ['except' => ['getById',
'getByUsername', 'submissions', 'comments', 'showSubmissions',
'showComments']]);
}
and
public function showSubmissions($username)
{
$user = new UserResource(
User::withTrashed()->where('username', $username)->firstOrFail(),
true
);
$submissions = SubmissionResource::collection(
Submission::whereUserId($user->id)
->withTrashed()
->orderBy('created_at', 'desc')
->simplePaginate(15)
);
return view('user.submissions', compact('user', 'submissions'));
}
View:
{{ $user->username }}
API:
Route::get('/user', 'UserController#getByUsername');
I need get information about user (username).
What is the problem and where is the error?
Based on your comment you have this route:
Route::get('/submission', function () {
return view('user.submissions');
});
When you are loading this view, you are not passing the user object to it. Then when the view is running, it is trying to access a variable that does not exist.
To fix this, you need to pass a variable to the view you are loading. For example, you could do something like this:
Route::get('/submission', function () {
return view('user.submissions', ['user' => auth()->user()]);
});
Note that you can change how you get the user instance depending on your use case. I am just getting the authenticated user to demonstrate the principle.

How to select an id in route pass it into the controller and fetch data against that id and redirect it view again in laravel 5.6

How to pass id from veiw into controller in then into another view again??
First of all this a basic question you should be able to search it anywhere on the internet. any how below is the solution.
web/routes.php:
Route::get('user/{id}','USerController#find')->name('user.get');
Or by passing user object in the route:
Route::get('user/{user}','USerController#find')->name('user.get');
UserController:
the below function accepts user object as a parameter in the route we defined above.
public function find(user $user)
{
return view('user.detail',compact('user'))
}
or
public function find($id)
{
$user = USer::find($id);
return view('user.detail',compact('user'))
}
resources/view/user/detail.blade.php:
{{ $user -> name }}
{{ $user -> email }}
and in order visit the route user.find use the below line:
View Detail
No you can use this code as reference to your project.
first solution is .
change route
Route::get('/show_vedio/{$id}', 'VedioController#show')->name(show_videos);
and use this {{ route('show_vedios', $vedio->id )}} in href
it's not entirely clear what you are trying to do, maybe i will help you:
public function show(int $id): View
{
$video = Video::find($id);
return view('video.show', ['video' => $video]);
}

How to pass variables needed in view through redirect route in laravel?

I have modified default auth method in controller which redirects user after custom login to set_password page. The problem is I can redirect it well to the desired page but I need to simultaneously pass two dynamic variables which are returned through querying database, which I am unable to pass with redirect.
My modified controller method is as follows:-
protected function authenticated(Request $request, $user)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return redirect()->route('set_password',['activated_up' => $activated_up, 'showuser' => $showuser]);
}
I know that to pass a variable to an view, I need to use the compact method like follows:-
return view('set_password', compact('activated_up', 'showuser'); but it cant be done with redirect.
The way I have redirected means I am passing parameters to route in the controller method, but I need to pass variables to the redirected view instead of parameters. How to achieve that?
protected function authenticated(Request $request, $user)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return redirect()->route('set_password',['activated_up' => $activated_up, 'showuser' => $showuser]);
}
you can use With
return redirect()->route('set_password')->with('data', ['some kind of
data']);
in your view
#if (session::has('data'))
The data is {{ session::get('data') }}
#endif
Tried as described in the answer by Kuldeep Mishra but could not achieve it though, anyways I found a workaround to achieve my desired output. What I did is changed my authenticated method to this:-
protected function authenticated(Request $request, $user)
{
return redirect()->route('set_password');
}
I only redirected to the set_password route from the above method and made new method in the controller to show the view with the compacted variables like this:-
public function setPasswordForm(Request $request)
{
$activated_up = User::where('id_user',Auth::user()->id_user)
->where(function($query) {
$query->where('activated_up', '=', '1')
->orWhere('activated_up', '=','0');
})
->get(['activated_up']);
$showuser = UserProfile::where('id_user',Auth::user()->id_user)->first();
return view('set_password', compact('activated_up', 'showuser'));
}
And a small change in route web.php file:-
Route::get('/set_password', 'Controller#setPasswordForm')->name('set_password');
So finally I was able to redirect to desired page with the desired view loaded with dynamic variables.

Laravel call route from controller

I am calling getting_started route after successfully login :
protected $redirectTo = '/getting_started';
Here is my getting_started route code :
Route::get('/getting_started','UserController#getting_started');
And controller code :
public function getting_started()
{
$id= Auth::id();
$user = DB::table('user_profiles')->where('user_id', '=', $id)->first();
if($user->dashboard_access == 0)
{
DB::table('user_profiles')
->where('user_id', $id)
->update(['dashboard_access' => 1]);
return view('user.getting_started');
}
return view('user.dashboard');
}
It works perfectly and show in url :
http://localhost:8080/getting_started
Now I actually want that if user.dashboard view is call it show in url like :
http://localhost:8080/dashboard`
And on getting_started view show :
http://localhost:8080/getting_started
It is possible to call dashboard route instead of :
return view('user.dashboard');
My dashobard route is :
Route::get('/dashboard',['middleware' => 'auth', function () {
return view('user.dashboard');
}]);
What I understand it is that you are looking for is this function
return redirect()->route('dashboard');
It's my understanding of your question which can be wrong. Maybe you are asking something else.
That called Redirection and especially you want to Returning A Redirect To A Named Route, you route called user.dashboard so you could redirect to it using redirect()->route(route_name) :
return redirect()->route('user.dashboard');
Hope this helps.

Categories