2016-11-08 106 views
1

這是我的職位表試圖讓非對象的財產laravel 5.2

<?php 
    use Illuminate\Database\Schema\Blueprint; 
    use Illuminate\Database\Migrations\Migration; 
    class CreatePostsTable extends Migration 
    { 

    public function up() 
    { 
     Schema::create('posts', function (Blueprint $table) { 
      $table->increments('id'); 

      $table->integer('user_id')->unsigned(); 
      $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 

      $table->integer('prf_id')->unsigned(); 
      $table->foreign('prf_id')->references('id')->on('profiles')->onDelete('cascade'); 

      $table->longText('status'); 
      $table->timestamps(); 
     }); 
    } 


    public function down() 
    { 
     Schema::drop('posts'); 
    } 
} 

這是我的Post模型:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Post extends Model 
{ 
    protected $table='posts'; 
    protected $fillable = ['status']; 


    protected $hidden = []; 

    public function profile(){ 
     return $this->belongsTo('App\Profile'); 
    } 

    public function user(){ 
     return $this->belongsTo('App\User'); 
    } 


} 

這是剖面模型

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Profile extends Model 
{ 
    protected $table='profiles'; 

    protected $fillable = ['user_id','name','position','roles','username','college','phone','location','graduation','skill']; 


    protected $hidden = []; 

    public function posts(){ 
     return $this->hasMany('App\Post'); 
    } 

} 

這是用戶模式

<?php 

namespace App; 

use Illuminate\Foundation\Auth\User as Authenticatable; 

class User extends Authenticatable 
{ 

    protected $fillable = [ 
     'fname','lname', 'email','sex', 'password','user_id','roles' 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 


    public function posts(){ 
     return $this->hasMany('App\Post'); 
    } 

} 

當我嘗試{{$狀態 - >用戶> FNAME}} this.its顯示正確的值,但是當我嘗試{{$狀態 - >嘗試獲取非對象的屬性(視圖:C:\ xampp \ htdocs \ abc \ resources \ views \ pages \ profile.blade.php) i真的不知道爲什麼:(

+1

顯示代碼取出'$ status'的位置? –

+0

'public function myprofile(Request $ request){ $ id = $ request-> prf; $ user = User :: where('id','=',$ id) - > first(); $ u_id = $ user-> user_id; $ all = Profile :: where('id','=',$ id) - > first(); $ tasks = Task :: where('user_id','=',$ u_id) - > get(); $ user = User :: where('id','=',$ id) - > first(); $ statuses = Post :: all(); return view('pages.profile',compact('all','tasks','user','status')); } ' – leo

+0

@leo作爲編輯問題請。 – apokryfos

回答

1

在你的Post類嘗試下面的代碼:

public function profile(){ 
    return $this->belongsTo('App\Profile', 'prf_id'); 
} 
+0

謝謝sooo much.its working – leo

+0

樂意提供幫助。如果此答案解決了您的問題,請將其標記爲已接受。 –

相關問題