What am i missing.

Published 8 months ago by madsynn

I run into this problem all the time. Any help would be appreciated. I need to get past this.

I have my controller edit function as below:

    public function edit(User $user)
    {
        $user = $this->userRepository->findWithoutFail($id);
        if (empty($user)) {
            Flash::error('User not found');
            return redirect(route('users.index'));
        }
        return view('users.edit')->with('user', $user);
    }

and my index.blade.php has a link to edit pretty much the normal way.

{!! Form::open(['route' => ['users.destroy', $user->id], 'method' => 'delete']) !!}
                <div class='btn-group'>
                    <a href="{!! route('users.show', [$user->id]) !!}" class='btn btn-success btn-xs'><i class="fa fa-eye fa-2x"></i></a>
                    <a href="{!! route('users.edit', [$user->id]) !!}" class='btn btn-warning btn-xs'><i class="fa fa-pencil-square-o fa-2x"></i></a>
                    {!! Form::button('<i class="fa fa-trash fa-2x"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
                </div>
                {!! Form::close() !!}

but when I click on edit i get the following error!

Undefined variable: id

Also if i change my edit function to this:

    public function edit($id)
    {
        $user = $this->userRepository->findWithoutFail($id);
        if (empty($user)) {
            Flash::error('User not found');
            return redirect(route('users.index'));
        }
        return view('users.edit')->with('user', $user);
    }

The error changes to this:

Property [id] does not exist on this collection instance. 

Can anyone help me, i cannot figure out what i missed.

Best Answer (As Selected By madsynn)
godbout

@madsynn you should bind the model in your Route Service Provider, not in the the route files. Have a look there: https://laravel.com/docs/5.5/routing#explicit-binding

Make sure you import your user class correctly in the Route Service Provider.

Then in your route files (web.php), you call your route with the {user} tag, for example:

Route::get('myroute/users/{user}', '[email protected]');

Then, MAGIC!!!

Drfraker

Is your index controller method that is loading that view passing the user in?

madsynn

@Drfraker

Here is my whole controller:

class UserController extends AppBaseController
{
    /** @var  UserRepository */
    private $userRepository;
    private $profileRepository;

    public function __construct(UserRepository $userRepo, ProfileRepository $profileRepo)
    {
        $this->userRepository = $userRepo;
        $this->profileRepository = $profileRepo;
    }

    /**
     * Display a listing of the User.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->userRepository->pushCriteria(new RequestCriteria($request));
        $users = $this->userRepository->all();

        //return $users;
         return view('users.index')->with('users', $users);
    }

    /**
     * Show the form for creating a new User.
     *
     * @return Response
     */
    public function create()
    {
        return view('users.create');
    }

    /**
     * Store a newly created User in storage.
     *
     * @param CreateUserRequest $request
     *
     * @return Response
     */
    public function store(CreateUserRequest $request)
    {
        $input = $request->all();

        $user = $this->userRepository->create($input);

        

        Flash::success('User saved successfully.');

        return redirect(route('users.index'));
    }

    /**
     * Display the specified User.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $user = $this->userRepository->findWithoutFail($id);

        if (empty($user)) {
            Flash::error('User not found');

            return redirect(route('users.index'));
        }

        return view('users.show')->with('user', $user);
    }

    /**
     * Show the form for editing the specified User.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $user = $this->userRepository->findWithoutFail(id);

        // dd($user);

        if (empty($user)) {
            Flash::error('User not found');

            return redirect(route('users.index'));
        }

        return view('users.edit', compact('user'));
    }

    /**
     * Update the specified User in storage.
     *
     * @param  int              $id
     * @param UpdateUserRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateUserRequest $request)
    {
        $user = $this->userRepository->findWithoutFail($id);

        if (empty($user)) {
            Flash::error('User not found');

            return redirect(route('users.index'));
        }

        $user = $this->userRepository->update($request->all(), $id);

        Flash::success('User updated successfully.');

        return redirect(route('users.index'));
    }

    /**
     * Remove the specified User from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $user = $this->userRepository->findWithoutFail($id);

        if (empty($user)) {
            Flash::error('User not found');

            return redirect(route('users.index'));
        }

        $this->userRepository->delete($id);

        Flash::success('User deleted successfully.');

        return redirect(route('users.index'));
    }

}
madsynn

@Drfraker maybe you can help me with this also.

on my userController i need to make sure this runs and stores the data from it while a new user is being created and i am having issues getting it to work. any ideas i appreciate the help.

$this->attributes['confirmation_code'] = \Hash::make( $this->email . time() );

i tried to set the attribute in my model but it doesnt run when the user is created.

    public function getConfirmationCodeAttribute()
    {
        return \Hash::make( $this->email . time() );
    }

godbout

Well, $id doesn't exist in your method.

public function edit(User $user)
    {
        $user = $this->userRepository->findWithoutFail($id);
        if (empty($user)) {
            Flash::error('User not found');
            return redirect(route('users.index'));
        }
        return view('users.edit')->with('user', $user);
    }

But you're passing the User, so Laravel should provide it for you. Not sure why there's a call to userRepository here.

If you bind your route with your model, then Laravel takes care of all the check for you, so if the method is entered, that means that you're already having a valid user.

Therefore, your method can become way simpler:

public function edit(User $user)
    {
        return view('users.edit')->with('user', $user);
    }

madsynn

@godbout

Thanks for getting back to me.

The repo bit is because im using repositories as datacontroller.

As for bind your route with your model, i have already done this but im still getting the same problem how would you do this and ill try it your way.

I have this in my routes web.php

Route::model('user', App\Models\User::class);

And in my repo i have this

    /**
     * Configure the Model
     **/
    public function model()
    {
        return User::class;
    }

Once again if im missing something please let me know.

Thanks in advance.

godbout

@madsynn you should bind the model in your Route Service Provider, not in the the route files. Have a look there: https://laravel.com/docs/5.5/routing#explicit-binding

Make sure you import your user class correctly in the Route Service Provider.

Then in your route files (web.php), you call your route with the {user} tag, for example:

Route::get('myroute/users/{user}', '[email protected]');

Then, MAGIC!!!

Please sign in or create an account to participate in this conversation.