Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何通过关系模型显示数据库中的变量?_Php_Laravel_Laravel 4_Eloquent - Fatal编程技术网

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
the
belongsTo->getResults()
get called并运行错误的
query
so
authors
get null时,您会得到错误。要记住的事情:

  • 对于
    hasOne
    belongsTo
    关系,使用单数形式的关系方法,即
    作者
    而非
    作者
  • hasOne
    belongsTo
    返回单个模型对象

  • 对于
    有许多
    属于多个
    关系,使用复数形式的关系方法,即,
    作者
    作者

  • hasMany
    belongtomany
    返回模型对象的集合

你有没有没有没有书的作者?如果您这样做,您遇到的错误将被触发。不知道问题是什么,但现在已经解决了。我刚刚将模型的方法改为奇异,现在它可以工作了。奇怪,并不是说你不能用其他的形式。方法名称用于查找外键,因此需要在belongsTo()中将其作为第二个参数重写。无论如何,这是正确的答案ofc@deczo,谢谢大家竖起大拇指,祝一切顺利:-)谢谢阿尔法狼人欢迎@LoveAndHappiness:-)