這是我的職位表試圖讓非對象的財產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真的不知道爲什麼:(
顯示代碼取出'$ status'的位置? –
'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
@leo作爲編輯問題請。 – apokryfos