Php 如何通过关系模型显示数据库中的变量?
我有一份书单和一份作者名单。我建立了模型关系,所以我的书模型说,Php 如何通过关系模型显示数据库中的变量?,php,laravel,laravel-4,eloquent,Php,Laravel,Laravel 4,Eloquent,我有一份书单和一份作者名单。我建立了模型关系,所以我的书模型说,books->belongTo('Author'),而我的作者模型说,authors->hasMany('Book') 因此,通常我可以通过以下方式访问a变量: $books=Book::all() 然后在视图中: @foreach($books as $book) <div>{{$book->id}}</div> <div>{{$boo
books->belongTo('Author')
,而我的作者模型说,authors->hasMany('Book')
因此,通常我可以通过以下方式访问a变量:
$books=Book::all()
然后在视图中:
@foreach($books as $book)
<div>{{$book->id}}</div>
<div>{{$book->title}}</div>
<div>{{$book->authors->firstname}}</div>
@endforeach
Author.php
class Book extends \Eloquent {
protected $guarded = [];
public function religions()
{
return $this->belongsTo('Religion');
}
public function branches()
{
return $this->belongsTo('Branch');
}
public function authors()
{
return $this->belongsTo('Author');
}
public function quotes()
{
return $this->hasMany('Quote');
}
public function chapters()
{
return $this->hasMany('Chapter');
}
}
class Author extends \Eloquent {
protected $guarded = [];
public function books()
{
return $this->hasMany('Book');
}
public function quotes()
{
return $this->hasMany('Quote');
}
public function branches()
{
return $this->belongsTo('Branch');
}
public function religions()
{
return $this->belongsTo('Religion');
}
}
@extends('layout.main')
@section('content')
<h1>Books List!!</h1>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
</tr>
@foreach($books as $book)
<tr>
<td>{{$book->id}}</td>
<td>{{$book->title}}</td>
<td>{{$book->authors->firstname}}</td>
</tr>
@endforeach
</table>
@stop
然后是我的控制器:
ReligionBranchBookController
class ReligionBranchBookController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index($religionId, $branchId)
{
//
// $books = Book::where('religion_id', $religionId)->where('branch_id', $branchId)->get();
$books = Book::all();
$authors = Author::all();
// dd($books->toArray());
return View::make('books.index')
->with('religionId', $religionId)
->with('branchId', $branchId)
->with('books', $books)
->with('authors', $authors);
}
}
我的看法:
index.blade.php
class Book extends \Eloquent {
protected $guarded = [];
public function religions()
{
return $this->belongsTo('Religion');
}
public function branches()
{
return $this->belongsTo('Branch');
}
public function authors()
{
return $this->belongsTo('Author');
}
public function quotes()
{
return $this->hasMany('Quote');
}
public function chapters()
{
return $this->hasMany('Chapter');
}
}
class Author extends \Eloquent {
protected $guarded = [];
public function books()
{
return $this->hasMany('Book');
}
public function quotes()
{
return $this->hasMany('Quote');
}
public function branches()
{
return $this->belongsTo('Branch');
}
public function religions()
{
return $this->belongsTo('Religion');
}
}
@extends('layout.main')
@section('content')
<h1>Books List!!</h1>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
</tr>
@foreach($books as $book)
<tr>
<td>{{$book->id}}</td>
<td>{{$book->title}}</td>
<td>{{$book->authors->firstname}}</td>
</tr>
@endforeach
</table>
@stop
@extends('layout.main'))
@节(“内容”)
图书目录!!
身份证件
标题
作者
@foreach($books作为$book)
{{$book->id}
{{$book->title}
{{$book->authors->firstname}
@endforeach
@停止
我知道它应该正常工作,我只用书籍和作者来重建它,而且在那里工作得很好
那么,有人知道我错在哪里吗
谢谢
George在您的
书中
模型将作者
方法更改为作者
,如下所示:
public function author()
{
return $this->belongsTo('Author');
}
因为,根据您的关系,一本书属于一位作者,当您调用authors
thebelongsTo->getResults()
get called并运行错误的query
soauthors
get null时,您会得到错误。要记住的事情:
- 对于
和hasOne
关系,使用单数形式的关系方法,即belongsTo
而非作者
作者
或hasOne
返回单个模型对象belongsTo
- 对于
和有许多
关系,使用复数形式的关系方法,即,属于多个
非作者
作者
和hasMany
返回模型对象的集合belongtomany