dphj737575 2016-11-04 16:55
浏览 169

使用Laravel 5将数据插入数据库

I am trying to Update user Profile after they Login, It works well and says PROFILE UPDATED SUCCESSFULLY but nothing inserted on the Database Table, so in essence, the View shows nothing of the Updated form fields. Please anyone with an idea of where i am getting this wrong will be highly appreciated, Thanks.

CONTROLLER Update FUNCTION

    public function update(Request $request)
    {

        $rules = [
            'name' => 'required',
            'email' => 'required',
            'phone' => 'required|numeric',
            'country' => 'required',
            'gender' => 'required',
            'birthday' => 'required',
            'fb' => 'url',
            'twitter' => 'url',
            'gp' => 'url',
            'instagram' => 'url',
            'personal_site' => 'url',
            'aboutme' => 'url',
            'linkedin' => 'url',
            'pinterest' => 'url'

        ];

        $data= $request->all();
        $validator = Validator::make($data, $rules);
        if($validator->fails()){
            return Redirect::back()->withInput()->withErrors($validator);
        }
        $user = Auth::user();

        $user->name = $data['name'];
        $user->email = $data['email'];
        $user->phone = $data['phone'];
        $user->country = $data['country'];
        $user->birthday = $data['birthday'];
        $user->address = $data['address'];
        if($user->save()) {
            $profile_id = $user->id;
            $profile = Profile::find($profile_id);
            if(count($profile) > 0) {
            $profile->gender = $data['gender'];
            $profile->city = $data['city'];
            $profile->state = $data['state'];
            $profile->aboutmyself = $data['aboutmyself'];
            $profile->fb = $data['fb'];
            $profile->twitter = $data['twitter'];
            $profile->gp = $data['gp'];
            $profile->instagram = $data['instagram'];
            $profile->personal_site = $data['personal_site'];
            $profile->aboutme = $data['aboutme'];
            $profile->linkedin = $data['linkedin'];
            $profile->pinterest = $data['pinterest'];
            // $profile = $user->profile()->save($profile);
            $profile->save();

}
        } else {
            return redirect()->back()->withInput()->withInfo("Something went wrong. Please, try again");
        }
        return redirect()->route('profile')->withSuccess("Your Profile Successfully Updated.");

    }

MY VIEW (profile-edit.blade.php)

 <div class="form-group row">
              {!! Form::model($user, array('route' => 'post.edit.profile', 'method' => 'post', 'class' => 'form-horizontal')) !!}

                 {!! Form::label('name', "Full Name", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-8">
                  <div class="row">
                    <div class="col-md-9">
                      {!! Form::text('name', null, array('class' => 'form-control', 'placeholder' => 'Your Full Name', 'required' => 'required')) !!}
                    </div>
                  </div>
                </div>
              </div>
              <div class="form-group row">
                 {!! Form::label('email', "Email Address", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-6">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon1">
                                <i class="material-icons md-18 text-muted">mail</i>
                            </span>
                   {!! Form::email('email', null, array('class' => 'form-control', 'placeholder' => '', 'required' => 'required')) !!}
                  </div>
                </div>
              </div>
              <div class="form-group row">
                 {!! Form::label('phone', "Phone Number", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-6">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon1">
                                <i class="material-icons md-18 text-muted">mail</i>
                            </span>
                  {!! Form::text('phone', null, array('class' => 'form-control', 'placeholder' => 'e.g. +8801711223344', 'required' => 'required')) !!}
                  </div>
                </div>
              </div>


              <div class="form-group row">
                {!! Form::label('gender', "Gender", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                    {!! Form::select('gender', $gender, array('class' => 'c-select form-control', 'id' => '', 'required' => 'required')) !!}
                </div>
              </div>


              <div class="form-group row">
               {!! Form::label('birthday', "Birthday", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>

                    {!! Form::text('birthday', null, array('class' => 'datepicker form-control', 'placeholder' => '01/28/2016','id' => 'birthday', 'required' => 'required')) !!}
                  </div>
                </div>
              </div>
                            <div class="form-group row">
               {!! Form::label('address', "Address", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>
                    {!! Form::text('address', null, array('class' => 'form-control', 'placeholder' => 'Street No., Area...','id' => 'address', 'required' => 'required')) !!}
                  </div>
                </div>
              </div>
                                        <div class="form-group row">
              {!! Form::label('city', "City", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>
                    {!! Form::text('city', $user->profile->city, array('class' => 'form-control', 'placeholder' => 'City', 'required' => 'required')) !!}
                  </div>
                </div>
              </div>
               <div class="form-group row">
             {!! Form::label('state', "State", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>
                    {!! Form::text('state', $user->profile->state,  array('class' => 'form-control', 'placeholder' => 'State', 'required' => 'required')) !!}
                  </div>
                </div>
              </div>


              <div class="form-group row">
                {!! Form::label('country', "Country", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-8">
                  <div class="row">
                    <div class="col-md-6">
                     {!! Form::text('country', null, array('class' => 'form-control', 'placeholder' => 'Country','id' => '')) !!}

                    </div>
                  </div>
                </div>
              </div>




                           <div class="form-group row">
              {!! Form::label('aboutmyself', "About Me", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>
                   {!! Form::textarea('aboutmyself', Auth::user()->profile->aboutmyself, array('class' => 'form-control', 'rows' => 4, 'placeholder' => 'About Yourself')) !!}
                  </div>
                </div>
              </div>

                <div class="form-group row">
             {!! Form::label('fb', "Facebook Link", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>
                   {!! Form::text('fb', $user->profile->fb, array('class' => 'form-control', 'placeholder' => 'https://facebook.com/username')) !!}
                  </div>
                </div>
              </div>

              <div class="form-group row">
             {!! Form::label('twitter', "Twitter Link", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>
                   {!! Form::text('twitter', $user->profile->twitter, array('class' => 'form-control', 'placeholder' => 'https://twitter.com/username')) !!}
                  </div>
                </div>
              </div>

                          <div class="form-group row">
            {!! Form::label('gp', "Google+ Link", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>
                   {!! Form::text('gp', $user->profile->gp, array('class' => 'form-control', 'placeholder' => 'https://plus.google.com/+username')) !!}
                  </div>
                </div>
              </div>

              <div class="form-group row">
           {!! Form::label('personal_site', "Personal Site", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>
                    {!! Form::text('personal_site', $user->profile->personal_site, array('class' => 'form-control', 'placeholder' => 'http://www.mywebsite.me')) !!}
                  </div>
                </div>
              </div>

               <div class="form-group row">
          {!! Form::label('instagram', "Instagram Link", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>
                    {!! Form::text('instagram', $user->profile->instagram, array('class' => 'form-control', 'placeholder' => 'https://www.instagram.com/username')) !!}
                  </div>
                </div>
              </div>

              <div class="form-group row">
         {!! Form::label('linkedin', "LinkedIn Link", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>
                    {!! Form::text('linkedin', $user->profile->linkedin, array('class' => 'form-control', 'placeholder' => 'https://www.linkedin.com/username')) !!}
                  </div>
                </div>
              </div>

              <div class="form-group row">
          {!! Form::label('pinterest', "Pinterest Link", array('class' => 'col-sm-3 control-label')) !!}
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon2">
                                <i class="material-icons md-18 text-muted">language</i>
                            </span>
                    {!! Form::text('pinterest', $user->profile->pinterest, array('class' => 'form-control', 'placeholder' => 'https://www.pinterest.com/username')) !!}
                  </div>
                </div>
              </div>

             <!-- <div class="form-group row">
                <label for="password" class="col-sm-3 form-control-label">Change Password</label>
                <div class="col-sm-6 col-md-4">
                  <div class="input-group">
                    <span class="input-group-addon" id="basic-addon3">
                                <i class="material-icons md-18 text-muted">lock</i>
                            </span>
                    <input type="text" class="form-control" placeholder="Enter new password">
                  </div>
                </div>
              </div> -->
              <div class="form-group row">
                <div class="col-sm-8 col-sm-offset-3">
                  <div class="media">
                    <div class="media-left">
                     {!! Form::submit('Save Changes', array('class' => 'btn btn-success')) !!}
                    </div>
                   <!-- <div class="media-body media-middle p-l-1">
                      <label class="c-input c-checkbox">
                        <input type="checkbox" checked>
                        <span class="c-indicator"></span> Subscribe to Newsletter
                      </label>
                    </div> -->
                  </div>
                </div>
              </div>
            {!! Form::close() !!}

User MODEL (User.php)

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword,EntrustUserTrait {
        EntrustUserTrait::can as may;
        Authorizable::can insteadof EntrustUserTrait;
    }

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function profile(){
        return $this->hasOne('App\Profile','user_id','id');
    }

    public function pending(){
        return $this->hasMany('App\PendingTransfers', 'user_id', 'id');
    }

    public function transaction(){
        return $this->hasMany('App\Transaction', 'user_id', 'id');
    }
}

ROUTE

Route::group(array('middleware' => 'auth'), function()
{

    Route::get('logout', ['as' => 'logout', 'uses' => 'Auth\AuthController@logout']);
    Route::get('profile', ['as' => 'profile', 'uses' => 'UsersController@profile']);
    Route::get('edit-profile', ['as' => 'edit.profile', 'uses' => 'UsersController@edit']);
    Route::post('edit-profile', ['as' => 'post.edit.profile', 'uses' => 'UsersController@update']);
    Route::post('edit-photo', ['as' => 'post.edit.photo', 'uses' => 'UsersController@photoUpdate']);
    Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'Auth\AuthController@dashboard'));
    Route::get('change-password', array('as' => 'password.change', 'uses' => 'Auth\AuthController@changePassword'));
    Route::post('change-password', array('as' => 'password.doChange', 'uses' => 'Auth\AuthController@doChangePassword'));

UPDATE: Thanks for the Prompt response but that didn't fix it though, Am thinking the Fault might be from the CONTROLLER FUNCTION at this point:

if($user->save()) {
            $profile_id = $user->id;
            $profile = Profile::find($profile_id);
            if(count($profile) > 0) {
            $profile->gender = $data['gender'];
            $profile->city = $data['city'];
            $profile->state = $data['state'];
            $profile->aboutmyself = $data['aboutmyself'];
            $profile->fb = $data['fb'];
            $profile->twitter = $data['twitter'];
            $profile->gp = $data['gp'];
            $profile->instagram = $data['instagram'];
            $profile->personal_site = $data['personal_site'];
            $profile->aboutme = $data['aboutme'];
            $profile->linkedin = $data['linkedin'];
            $profile->pinterest = $data['pinterest'];
            // $profile = $user->profile()->save($profile);
            $profile->save();

OR Probably from the VIEW Opening Form route/variable $user

 {!! Form::model($user, array('route' => 'post.edit.profile', 'method' => 'post', 'class' => 'form-horizontal')) !!}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)