laravel- brain jaming error未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ id

时间:2015-07-06 03:16:35

标签: laravel laravel-4

UserController.php

public function edit_profile(){
    $input = Input::all();
    $res = array();
    $_m = "";
    $rules = array(
        'userName' => 'required',
        'fullName' => 'required',
        'shopName' => 'required',
        'userType' => 'required',
        'email' => 'required',
        'aboutMe' =>'required',
        'address' =>'required',
        'city' =>'required',
        'country' =>'required',
        'contactNo' =>'required|regex:/[0-9]{10,11}/',
    );
    $validator = Validator::make($input, $rules);
    if (!$validator->fails())
    {
        $user = User::find(Auth::user()->get()->id);
        $user->fullname = $input('fullName');
        $user->username = $input('userName');
        $user->businessname = $input('shopName');
        $user->usertype = $input('userType');
        $user->email = $input('email');
        $user->aboutme = $input('aboutMe');
        $user->address = $input('address');
        $user->city = $input('city');
        $user->country = $input('country');
        $user->phone = $input('contactNo');
        $user->save();
        $res['success']  = true;
        $res['message'] = "NO";
        return Redirect::to('user.profile');
    }
    else
    {
        return Redirect::to('login');
    }
}

profile.blade.php

<div class="tab-pane fade" id="p2">
    <h2>Account details</h2>
    <div class="hr hr-12 hr-double"></div>
    {{ Form::open(array('url'=>'profile','class' =>'form-horizontal')) }}
        <div class="form-group">
            <label class="col-sm-3 control-label">Full Name:</label>
            <div class="col-sm-3">
                {{ Form::text('fullName',ucwords(Auth::user()->fullname),array('class'=>'form-control', 'placeholder'=> 'Full Name Here' )) }}
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">User Name:</label>
            <div class="col-sm-3">
                {{ Form::text('userName',ucwords(Auth::user()->username),array('class'=>'form-control', 'placeholder'=>'e.g ali.ali')) }}
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">Shop Name:</label>
            <div class="col-sm-5">
                {{ Form::text('shopName',ucwords(Auth::user()->businessname),array('class'=>'form-control', 'placeholder'=>'e.g Niazi Traders')) }}
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">Email</label>
            <div class="col-sm-4">
                <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                    {{ Form::email('email',ucwords(Auth::user()->email),array('class'=>'form-control', 'placeholder'=>'e.g john.smith@example.com')) }}
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">User Type:</label>
            <div class="col-sm-4">
                <div class="input-group">
                    <span class="input-group-addon"></span>
                    {{ Form::select('userType',array('D'=>'Distributor', 'W'=>'Whole Saler','R' =>'Retailer'),ucwords(Auth::user()->usertype)) }}
                </div>
            </div>
        </div>

        <hr class="separator">

        <div class="form-group">
            <label class="col-sm-3 control-label">About Me:</label>
            <div class="col-sm-9">
                {{ Form::textarea('aboutMe',ucwords(Auth::user()->aboutme),array('class'=>'form-control','id'=>'about-editor','placeholder'=>'Some thing about yourself......')) }}
            </div>
        </div>                                                  
        <div class="form-group">
            <label class="col-sm-3 control-label">Address:</label>
            <div class="col-sm-7">
                {{ Form::text('address',ucwords(Auth::user()->address),array('class'=>'form-control','placeholder'=>'')) }}
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">City:</label>
            <div class="col-sm-3">
                {{ Form::text('city',ucwords(Auth::user()->city),array('class'=>'form-control','placeholder'=>'e.g Islamabad')) }}
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">Country:</label>
            <div class="col-sm-4">
                {{ Form::text('country',ucwords(Auth::user()->country),array('class'=>'form-control','placeholder'=>'e.g Pakistan')) }}
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">Phone Number:</label>
            <div class="col-sm-3">
                {{ Form::text('contactNo',ucwords(Auth::user()->phone),array('class'=>'form-control','placeholder'=>'e.g 0300xxxxxx')) }}
            </div>
        </div>

        <div class="form-actions">
            <div class="form-group">
                <div class="col-sm-offset-3 col-sm-9">
                    {{ Form::submit('Submit',array('class'=>'btn btn-primary')) }}
                    {{ Form::submit('Cancel',array('class'=>'btn btn-inverse')) }}
                </div>
            </div>
        </div>
    {{ Form::close() }}
</div>

routes.php文件

Route::get('logout','UserController@get_logout');
Route::get('profile', 'UserController@get_profile');
Route::post('profile', 'UserController@edit_profile');

user.php的

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

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

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

}

1 个答案:

答案 0 :(得分:0)

错误在于此行

   $user = User::find(Auth::user()->get()->id);

get是Builder类的函数,用于获取记录集合,Auth :: user()返回单个用户,因此您只需要访问其id属性

将上一行更改为

   $user = User::find(Auth::user()->id);

但Auth :: user你给当前用户所以你不需要再找到它,你可以直接访问它的属性

$user = Auth::user();