"Missing required parameters for [Route: profile.update] [URI: profile/{profile}]

Published 1 week ago by mlazuardy

I'M Creating User Profile, and want to edit the profile function like this

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Profile;
class ProfileController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Profile $profile)
    {
        //
      $users = $profile->users()->get();
        return view('profile.index',compact('profiles'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

         public function show ($username)
         {

           $users = User::with('profiles');

           if(!$users){
             abort(404);}
             return view('profile.show',compact('profile','user'));
         }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $profile = Profile::find($id);
        return view('profile.edit',compact('profile'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $this->validate($request,[
          'city' => 'required',
          'moto' => 'required',
          'facebook' =>'required',
          'instagram' => 'required',
          'github' => 'required',
        ]);

        Profile::find($id)->update($request->all());
        return redirect()->route('profile.edit')->with('success','Profile Updated');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

and the view is

  <form class="form-horizontal" role="form" method="POST" action="{{action('[email protected]')}}">

please help me fix it

Best Answer (As Selected By mlazuardy)
mlazuardy

Thank You @ftrillo @Snapey and @jlrdw for your participate

finally i can make the update until search more than 10 pages who give the tuts to make Profile, and this is my final ProfileController and it works, but if you can fix my useless code i wrote, please just Reply me

ProfilesController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Profile;
use App\User;
use Illuminate\Support\Facades\Input;
class ProfilesController extends Controller
{


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($username)
    {
        //
        $user = $this->getUserByUsername($username);
        return view('profile.show')->withUser($user);
    }
    public function getUserByUsername($username)
        {
            return User::with('profile')->whereUsername($username)->firstOrFail();
        }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
     public function edit($username)
        {
            $user = $this->getUserByUsername($username);
            return view('profile.edit')->withUser($user);
        }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $username)
    {
      $user = $this->getUserByUsername($username);
      $input = Input::only('location','bio','twitter_username','github_username','facebook_username','instagram_username');
      if ($user->profile == null) {
            $profile = new Profile();
            $profile->fill($input);
            $user->profile()->save($profile);
        } else {
            $user->profile->fill($input)->save();
        }



        $user->save();

          return redirect('profile/'.$user->username.'/edit')->with('success','profile updateSuccess');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
ftrillo
ftrillo
1 week ago (12,140 XP)

You have to send the id of the user you're updating in your action.

// for example
action('[email protected]', ['id' => $user->id]);
jlrdw
jlrdw
1 week ago (195,930 XP)

And I am still trying to figure out compact('profiles'), I'm missing something somewhere.

mlazuardy

@ftrillo where to put your code sir?

ftrillo
ftrillo
1 week ago (12,140 XP)

@jlrdw is right. compact('profiles') in the index controller method should probably be compact('users').

Though that wont affect your problems with update,

mlazuardy

@ftrillo can you help me to completed the index function?

ftrillo
ftrillo
1 week ago (12,140 XP)

Oh, I see what he meant now. There is more than one problem in the code...

First the view

// Wrong
<form class="form-horizontal" role="form" method="POST" action="{{action('[email protected]')}}">

//Right
<form class="form-horizontal" role="form" method="POST" action="{{action('[email protected]',  ['id' => $user->id])}}">

Then your show controller method

//  normally $id instead of $username
public function show ($id)
{
    $user = User::findOrFail($id);
    return view('profile.show',compact('user'));
}

mlazuardy

@ftrillo whoops say "Undefined variable: user"

Snapey
Snapey
1 week ago (647,375 XP)

some basics

assuming User has one Profile, and Profile belongs to User then the relationship should be user not users

index method. index of users or index of profiles? Not sure what your intent is there. Index method should not receive a profile? But understand, you don't have to have an index method.

if you want to edit the user profile you could pass the user id or the profile id but you DO need to check that the logged in user is allowed to edit the profile

jlrdw
jlrdw
1 week ago (195,930 XP)

Actually OP needs two things:

  • Dig into them docs
  • Watch some of Jeffrey's videos

And perhaps study this great example application by our own @bestmomo

https://github.com/bestmomo/laravel5-5-example

mlazuardy

@Snapey is it the problem from my admin dashboard? because im not checking it for user can CRUD the all the profile page

ftrillo
ftrillo
1 week ago (12,140 XP)

Yeah, you guys are right. I just read the title and looked directly at the update part of the controller. Didn't even notice all the weird things going on in the rest of the code.

mlazuardy

or you guys can making me a tutorial or refer me to page that teach me how to make a user profile for normal user,

Snapey
Snapey
1 week ago (647,375 XP)

is this for the user to update their own profile or for an administrator to do it?

mlazuardy

@Snapey for normal user sir. please help me to completed it, i must complete this task to pass my exam.

ftrillo
ftrillo
1 week ago (12,140 XP)

@shawnmilke1 Are you on an exam like, right now? For how long? lol

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