Laravel 5.2:如何以一对一的雄辩关系从数据库中检索数据

时间:2016-04-03 11:33:47

标签: php laravel laravel-5.2

我想访问特定用户的数据并更新数据。由于这个原因数据库是空的,当我想进入更新页面更新数据时,它显示以下错误。

  

14cfe21a93ffc779c1e985ab027ee400af4e7d8b.php中的ErrorException   12:试图获得非对象的属性(查看:   C:\ XAMPP \ htdocs中\ protfolio \资源\视图\ update.blade.php)

我正在使用一对一雄辩的关系。 单个用户可以创建自己的个人资料并更新个人资料。

我的用户模型:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

    protected $fillable = [
        'name', 'email', 'password',
    ];


    protected $hidden = [
        'password', 'remember_token',
    ];

     public function post()
    {
        return $this->hasOne('App\Post'); //Profile is your profile model
    }
}

我的帖子模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
      protected $fillable = [
    'first_name', 'middle_name', 'last_name','gender',                            'dob','nationality','nid','email','phone_no','about_me'
];
     public function user()
    {
       return $this->belongsTo('App\User'); //Profile is your profile model
    }
}

这是路线:

<?php

    Route::group(['middleware' => ['web']], function () {

    Route::get('/', function () {
    return view('welcome');
    })->name('home');

    Route::get('/dashboard', [
    'uses' =>'PostController@getDashboard',
    'as' => 'dashboard',
    'middleware' => 'auth'
    ]);

    Route::post('/update',[
    'uses'=>'PostController@postUpdate',
    'as' => 'update',
    'middleware' =>'auth'
    ]);

    Route::get('/account',[
    'uses' => 'UserController@getAccount',
    'as' =>'account',
     'middleware' =>'auth'
    ]); 

  Route::get('/create',[
    'uses' => 'UserController@getCreate',
    'as' =>'create',
    'middleware' =>'auth'
    ]);

  Route::post('/save',[
    'uses'=>'PostController@postCreate',
    'as' =>'save'

    ]);

});

我的帖子控制器:

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use Illuminate\Http\Response;
    use App\Http\Requests;
    use App\Post;
    use App\User;
    use Illuminate\Support\Facades\Auth;
    use Illuminate\Support\Facades\Storage;
    use Illuminate\Support\Facades\File;

    class PostController extends Controller
    {
        public function getDashboard()
   {        
      $posts = DB::table('posts')->where('id', Auth::user()->id);     
          return view('dashboard',['posts'=>$posts]);  
    }
        public function postUpdate(Request $request)
    {
          $this->validate($request,[
            'first_name'=> 'required|max:120',
            'middle_name'=> 'required|max:120',
            'last_name' => 'required|max:120',
            'gender'=> 'required',
            'dob'=>'required',
            'nationality'=>'required',
            'nid'=>'required',
            'email' => 'required|email|unique:users',
            'phone_no'=>'required',
            'about_me'=>'required',
            ]);
     //   $data =fill($request->all());
      $data = $request->only('first_name', 'middle_name', 'last_name',
       'gender', 'dob', 'nationality', 'nid', 'email', 'phone_no', 'about_me');
       $request->user()->post()->update($data) ;  
        return redirect()->route('dashboard');
       }        
    }

这是update.blade.php

@extends('layouts.master')

@section('content')
     @include('includes.message-block')
     <div class="container" >
            <h3> Update Profile </h3>
        {!! Form::open(array('route' => 'update','class'=>'form-horizontal','method'=>'POST'))  !!}
    {{--    {!! Form::token(); !!}
        {!!   csrf_field() ; !!} --}}
            <div class="form-group">
                <label>First Name</label>
                <input type="text" name="first_name" class="form-control"   value="{{$posts->first()->first_name}}">
            </div>
            <div class="form-group">
                <label>Middle Name</label>
                <input type="text" name="middle_name" class="form-control"  value="{{$posts->first()->middle_name}}">
            </div>
            <div class="form-group">
                <label>Last Name</label>
                <input type="text" name="last_name" class="form-control"   value="{{$posts->first()->last_name}}">
            </div>

            <div class="form-group">
                <label>Gender</label>
                <select class="form-control" name="gender" value="{{$posts->first()->gender}}">
                    <option>Male</option>
                    <option>Female</option>     
                </select>
            </div>
            <div class="form-group">
                <label> Date Of Birth</label>  
                <input type="date" name="dob" class="form-control"  value="{{$posts->first()->dob}}">
            </div>

            <div class="form-group">
                <label>Nationality</label>
                <input type="text" name="nationality" class="form-control"   value="{{$posts->first()->nationality}}">
            </div>

            <div class="form-group">
                <label>NID</label>
                <input type="text" name="nid" class="form-control"   value="{{$posts->first()->nid}}">
            </div>

            <div class="form-group">
                <label>Email</label>
                <input type="email" name="email" class="form-control"   value="{{$posts->first()->email}}">
            </div>

            <div class="form-group">
                <label>Phone</label>
                <input type="text" name="phone_no" class="form-control"   value="{{$posts->first()->phone_no}}">
            </div>

            <div class="form-group">
                <label>About Me</label>
                <textarea class="form-control" name="about_me"  rows="3">{{$posts->first()->about_me}}</textarea>
            </div>


            <button type="submit" class="btn btn-default">Submit</button>
        {!! Form::close() !!}
        </div>

 @endsection

1 个答案:

答案 0 :(得分:0)

而不是使用它:

{!! Form::open(array('route' => 'update','class'=>'form-horizontal','method'=>'POST'))  !!}

使用此:

{!! Form::model($posts, array('route' => 'update','class'=>'form-horizontal','method'=>'POST'))  !!}

如果您使用上述内容,则无需手动将其设置为form。现在从所有输入字段中删除value属性。

在控制器中:

public function getDashboard()
{        
    $posts = \DB::table('posts')->where('user_id', Auth::user()->id)->first();     
    return view('dashboard.update', compact('posts'));  //If the `update.blade.php` is into the `dashboard` directory.
}