当我尝试评估来自类别模型的数据时,在Laravel中我得到了一个“试图获取属性'display_name‘的非对象”错误。
我有两个模型: Post和Post;我有相应的表格:
Posts('id', 'title', 'body', 'cover_image')
和
Categories('id', 'display_name','name', 'key).
枢轴表是: category_post
这是我的邮政模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//Table Name
protected $table = 'posts';
//Model Relationships
public function tag(){
return $this->belongsToMany('App\Tag', 'id');
}
public function category() {
return $this->belongsTo('App\Category','id');
}
}这是我的分类模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories';
//Model Relationships
public function post()
{
return $this->belongsTo('App\Post', 'id');
}
}另外,这是我的PagesController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PagesController extends Controller
{
public function index()
{
if ( !is_null(Post::class) ) {
$posts = Post::latest()->with('category')->paginate(5);
return view('pages.index')->with(['posts' => $posts]);
}
else {
return view('pages.empty');
}
}这是我的刀片文件
@if(count($posts) > 0)
@foreach($posts as $post)
<span> <i class="fa fa-folder-open"></i> {{ $post->category->display_name }} </span>
@endforeach
@endif更新
这些是我的迁移: Posts表:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug')->nullable();
$table->mediumText('body');
$table->string('cover_image')->default('noimage.jpg')->nullable();
$table->integer('user_id');
$table->timestamps();
$table->integer('category_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}我的分类表:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('display_name');
$table->string('description')->nullable();
$table->string('password')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}预期的结果是从from表中获取display_name,并以列表方式显示它们。这就是我一直在犯的错误
试图获取非对象的属性“display_name”(视图:display_name)
我试过这样称呼它:
<span> <i class="fa fa-folder-open"></i> {{ $post->category()->display_name }} </span>但也不起作用。
编辑:
在将$post变量转储到控制器中之后:
Post {#331 ▼
#table: "posts"
#fillable: array:7 [▼
0 => "id"
1 => "title"
2 => "slug"
3 => "body"
4 => "cover_image"
5 => "user_id"
6 => "category_id"
]
+primaryKey: "id"
+timestamps: true
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▼
"id" => 1
"title" => "This is a Base Announcement"
"slug" => null
"body" => "Please feel free to delete this announcement and create a new one afterwards."
"cover_image" => "noimage.jpg"
"user_id" => 1
"created_at" => "2019-05-20 14:22:45"
"updated_at" => "2019-05-20 14:22:45"
"category_id" => null
]
#original: array:9 [▼
"id" => 1
"title" => "This is a Base Announcement"
"slug" => null
"body" => "Please feel free to delete this announcement and create a new one afterwards."
"cover_image" => "noimage.jpg"
"user_id" => 1
"created_at" => "2019-05-20 14:22:45"
"updated_at" => "2019-05-20 14:22:45"
"category_id" => null
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▼
0 => "*"
]
}发布于 2019-05-20 12:58:16
问题是,您没有在posts表中保存CATEGORY_ID,请保存好类别ID,然后尝试如下:
更新帖子模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//Table Name
protected $table = 'posts';
// Setup fields of table "posts"
protected $fillable = ['id', 'title', 'body', 'cover_image','category_id'];
//Model Relationships
public function tag(){
return $this->belongsToMany('App\Tag', 'id');
}
// Relation category to foreign key
public function category() {
return $this->belongsTo('App\Category','category_id');
}
}范畴模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories';
// Setup fields of table "category"
protected $fillable = ['id', 'display_name', 'name', 'key'];
//Model Relationships
public function posts()
{
return $this->hasMany('App\Post');
}
}视图
@if(count($posts) > 0)
@foreach($posts as $post)
<span> <i class="fa fa-folder-open"></i> {{ $post->category->display_name }} </span>
@endforeach
@endif迁移职位-外键category_id
Schema::create('posts', function (Blueprint $table) {
// Your other fields.....
// You must setup category like this
$table->integer('category_id')->nullable()->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
});控制器
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PagesController extends Controller
{
public function index()
{
$posts = Post::all();
if (!empty($posts)) {
// Please let me know this test
dd($posts[0]->category->display_name);
return view('pages.index', ['posts' => $posts]);
}
else {
return view('pages.empty');
}
}发布于 2019-05-20 13:01:13
我认为您在post()方法中犯了Category模型中的错误。尝试像这样修改这个方法:
类别模式:
public function post()
{
return $this->hasMany('App\Post', 'id');
}来源:雄辩的一对多关系
https://stackoverflow.com/questions/56220774
复制相似问题