2016-04-03 4 views
0

特定のユーザーのデータにアクセスしてデータを更新したいのですが、その理由でデータベースが空であるため、データを更新すると、次のエラーが表示されます。 14cfe21a93ffc779c1e985ab027ee400af4e7d8b.phpラインのLaravel 5.2:1対1の雄弁な関係でデータベースからデータを取り出す方法

ErrorException 12:(: C:\ xamppの\ htdocsにprotfolioの\リソース\ビュー\ update.blade.php \ビュー)

非オブジェクトのプロパティを取得しようとすると、

私は1対1の雄弁な関係を使用しています。 シングルユーザーは、自分のプロファイルを作成してプロファイルを更新できます。

マイユーザーモデル:

<?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' =>'[email protected]', 
    'as' => 'dashboard', 
    'middleware' => 'auth' 
    ]); 

    Route::post('/update',[ 
    'uses'=>'[email protected]', 
    'as' => 'update', 
    'middleware' =>'auth' 
    ]); 

    Route::get('/account',[ 
    'uses' => '[email protected]', 
    'as' =>'account', 
    'middleware' =>'auth' 
    ]); 

    Route::get('/create',[ 
    'uses' => '[email protected]', 
    'as' =>'create', 
    'middleware' =>'auth' 
    ]); 

    Route::post('/save',[ 
    'uses'=>'[email protected]', 
    '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 
+0

あなた 'update.blade.php'ビューのための方法はありますか? 'routes.php'ファイルのコードを投稿してください。 –

+0

あなたはポストテーブルからクエリを実行していますか、ポストテーブルにはfirst_name、last_nameなどがありますか?フォームに使用していますか?間違ったインデックスを作成している可能性があります –

答えて

0

です:あなたは上記のように使用している場合は、設定する必要が

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

{!! Form::open(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. 
} 
+0

問題は同じままです! 私はそのユーザーのためのデータベースを持っていない参照してください。それは空の値を表示する必要があります。その代わりに上記のエラーを示しています。 – Hola

+0

'One to One'関係を定義するための' users'の表が必要です。 –

+0

私のモデルでのみ定義されています。 – Hola