mlazuardy

@ftrillo this thursday sir, i just have 4 days again, and its 4.00 am here, i need a rest. while i sleep, please help to to completed this User and the Profile, or just make 1 Model/Controller for just User without profile, whatever it is . i swear i'll learn your tuts. please.

Snapey
Snapey
1 week ago (647,375 XP)

i'll give you some bullet points to how I would approach it, but i'm not going to write the code for you.

  • make sure that profile() method on User model is working correctly. Tinker is your friend.
  • create a route for /account that accepts the user id
  • in the /account/{id}/edit route load User with profile
  • populate form with existing data using the old() helper
  • validate the data sent back from the form when posted to /account/id
  • get the data from request and save to user model
  • conventionally you should pass the id into the methods but you should just use the id of the logged in user to avoid tampering by the user
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)
    {
        //
    }
}

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