Mekaboo's avatar

How to fix this error please "Too few arguments to function App\Http\Controllers\ProfileController::show(), 0 passed and exactly 1 expected "

Route:

Route::post('profile/user', 'ProfileController@show')->name('profile.user');

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProfileController extends Controller
{



	/**
	 * /username
	 *
	 * @param $username
	 * @return Response
	 */
	public function show($username)
	{
		$user = $this->getUserByUsername($username);

		return View::make('profile.profile')->withUser($user);
	}

	/**
	 * /profiles/username/edit
	 *
	 * @param $username
	 * @return mixed
	 */
	public function edit($username)
	{
		$user = $this->getUserByUsername($username);

		return View::make('profile.editpro')->withUser($user);
	}

	/**
	 * Update a user's profile
	 *
	 * @param $username
	 * @return mixed
	 * @throws Laracasts\Validation\FormValidationException
	 */
	public function update($username)
	{
		$user = $this->getUserByUsername($username);
		$input = Input::only('location', 'bio', 'twitter_username', 'facebook_username', 'facebook_username');

		$this->profileForm->validate($input);

		$user->profile->fill($input)->save();

		return Redirect::route('profile.editpro', $user->username);
	}

	/**
	 * Fetch user
	 * (You can extract this to repository method)
	 *
	 * @param $username
	 * @return mixed
	 */
	public function getUserByUsername($username)
	{
		return User::with('profile')->whereUsername($username)->firstOrFail();
	}

}

Thank you!!

0 likes
11 replies
MarianoMoreyra's avatar

Hi @mekaboo

Try adding the username parameter to your route:

Route::post('profile/user/{username}', 'ProfileController@show')->name('profile.user');

Of course, I'm assuming that you are passing that you are calling that route passing the username from your view

1 like
Mekaboo's avatar

Hello Sir @marianomoreyra

Thank ya so much your answer worked :) !!! Now Im getting 404 not found error..I have a regular profile and Edit profile views..dont know where the error is showing from:

Profile view;

@extends('layouts/layout')

@section('content')
	@if ($user->profile)
		<h1>{{ $user->username }} <small>{{ $user->profile->location }}</small></h1>
		<div class="bio">
			<p>
				{{ $user->profile->bio }}
			</p>
		</div>

		<ul class="links">
			<li>{{ link_to('http://twitter.com/' . $user->profile->twitter_username, 'Find Me On Twitter') }}</li>
			<li>{{ link_to('http://facebook.com/' . $user->profile->facebook_username, 'Find Me On Facebook') }}</li>
      <li>{{ link_to('http://instagram.com/' . $user->profile->instagram_username, 'Find Me On Instagram') }}</li>
		</ul>

		@if ($user->isCurrent())
			{{ link_to_route('profile.edit', 'Edit Your Profile', $user->username) }}
		@endif
	@else
		<p>No profile yet.</p>
	@endif
@stop

Edit profile view:

@extends('layouts/layout')

@section('content')
	<h1>Edit Profile</h1>

	{{ Form::model($user->profile, ['method' => 'PATCH', 'route' => ['profile.update', $user->username]]) }}
		<!-- Location Field -->
		<div class="form-group">
			{{ Form::label('location', 'Location:') }}
			{{ Form::text('location', null, ['class' => 'form-control']) }}
			{{ errors_for('location', $errors) }}
		</div>

		<!-- Bio Field -->
		<div class="form-group">
			{{ Form::label('bio', 'Bio:') }}
			{{ Form::textarea('bio', null, ['class' => 'form-control']) }}
			{{ errors_for('bio', $errors) }}
		</div>

		<!-- Twitter_username Field -->
		<div class="form-group">
			{{ Form::label('twitter_username', 'Twitter_username:') }}
			{{ Form::text('twitter_username', null, ['class' => 'form-control']) }}
			{{ errors_for('twitter_username', $errors) }}

		</div>

		<!-- Github_username Field -->
		<div class="form-group">
			{{ Form::label('github_username', 'Github_username:') }}
			{{ Form::text('github_username', null, ['class' => 'form-control']) }}
			{{ errors_for('github_username', $errors) }}
		</div>

		<!-- Update Profile Field -->
		<div class="form-group">
			{{ Form::submit('Update Profile', ['class' => 'btn btn-primary']) }}
		</div>
	{{ Form::close() }}

@stop

MarianoMoreyra's avatar

@mekaboo I'm glad it worked!

Now...when do you get that error? When trying to show the Profile or trying to Edit?

If you get it when trying to show, please, post the view from where you are calling the Profile show route

1 like
Mekaboo's avatar

Profile.blade.php

@extends('layouts/layout')

@section('content')
	@if ($user->profile)
		<h1>{{ $user->username }} <small>{{ $user->profile->location }}</small></h1>
		<div class="bio">
			<p>
				{{ $user->profile->bio }}
			</p>
		</div>

		<ul class="links">
			<li>{{ link_to('http://twitter.com/' . $user->profile->twitter_username, 'Find Me On Twitter') }}</li>
			<li>{{ link_to('http://facebook.com/' . $user->profile->facebook_username, 'Find Me On Facebook') }}</li>
      <li>{{ link_to('http://instagram.com/' . $user->profile->instagram_username, 'Find Me On Instagram') }}</li>
		</ul>

		@if ($user->isCurrent())
			{{ link_to_route('profile.edit', 'Edit Your Profile', $user->username) }}
		@endif
	@else
		<p>No profile yet.</p>
	@endif
@stop

MarianoMoreyra's avatar
Level 25

@mekaboo That's the view your are trying to show if I'm not wrong. I mean the link or button from where you are linking to the Profile.

Anyway, try changing your getUserByUsername function at your Controller to this:

	public function getUserByUsername($username)
	{
		return User::with('profile')->where('username', $username)->firstOrFail();
	}

As you are probably getting that 404 error from that firstOrFail() method.

1 like
Mekaboo's avatar

Hey all!!

Soon after fixing issues I got a new one:

syntax error, unexpected end of file, expecting function (T_FUNCTION) or const (T_CONST)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProfileController extends Controller
{



	/**
	 * /username
	 *
	 * @param $username
	 * @return Response
	 */
	 public function construct($user) {
            $this->user = $user;
        }

	public function show($user)
	{
		$user = $this->getUserByUser($user);

		return View::make('profile')->withUser($user);
	}

	/**
	 * /profiles/username/edit
	 *
	 * @param $username
	 * @return mixed
	 */
	public function edit($user)
	{
		$user = $this->getUserByUsername($user);

		return View::make('editpro')->withUser($user);
	}

	/**
	 * Update a user's profile
	 *
	 * @param $username
	 * @return mixed
	 * @throws Laracasts\Validation\FormValidationException
	 */
	public function update($user)
	{
		$user = $this->getUserByUsername($user);
		$input = Input::only('location', 'bio', 'twitter_username', 'facebook_username', 'facebook_username');

		$this->profileForm->validate($input);

		$user->profile->fill($input)->save();

		return Redirect::route('editpro', $user->user);
	}

	/**
	 * Fetch user
	 * (You can extract this to repository method)
	 *
	 * @param $username
	 * @return mixed
	 */
	 public function getUserByUsername($user)
	 	{
	 		return User::with('profile')->where('user', $user)->firstOrFail();
	 	}

egarcia's avatar

Is there a missing } at the end of file?

1 like
Sinnbeck's avatar

Consider making a new thread. As this one is solved and the issue is unrelated, people are not likely to help much.

1 like
Mekaboo's avatar

I thought so but when I add it an error came up

Please or to participate in this conversation.