Php 一对多的房地产关系在拉拉维尔展现在眼前

Php 一对多的房地产关系在拉拉维尔展现在眼前,php,laravel,Php,Laravel,我正在开发laravel电话簿应用程序我希望每个电话簿都有一个客户端,但客户端属于多个电话簿我的电话簿表中有一列名为client_id,现在当我设置关系时,我如何在控制器中使用它们并将其作为对象传递到视图中,下面是我的一些代码 电话簿控制器 public function index(){ $phonebooks = Phonebook::all(); $client = Phonebook::find(?dont know if its right place for it?)->cl

我正在开发laravel电话簿应用程序我希望每个电话簿都有一个客户端,但客户端属于多个电话簿我的电话簿表中有一列名为client_id,现在当我设置关系时,我如何在控制器中使用它们并将其作为对象传递到视图中,下面是我的一些代码

电话簿控制器

public function index(){

$phonebooks = Phonebook::all();
$client = Phonebook::find(?dont know if its right place for it?)->client;
return view('admin.phonebooks.index',compact('phonebooks',$phonebooks),compact('client',$client));}
电话簿模型

class Phonebook extends Model{protected $fillable = ['title','description','client_id','calldate','rememberdate'];public function client() {
return $this->hasOne('App\Client','id');}    }
电话簿数据库迁移

Schema::create('phonebooks', function (Blueprint $table) {
    $table->increments('id');
    $table->text('title');
    $table->longText('description');
    $table->integer('client_id');
    $table->dateTime('calldate');
    $table->dateTime('rememberdate');
    $table->timestamps();
});
Schema::create('clients', function (Blueprint $table) {
    $table->increments('id');
    $table->text('title');
    $table->longText('description');
    $table->integer('fax');
    $table->text('adrress1');
    $table->integer('telephone1');
    $table->timestamps();
});
以及客户端数据库迁移

Schema::create('phonebooks', function (Blueprint $table) {
    $table->increments('id');
    $table->text('title');
    $table->longText('description');
    $table->integer('client_id');
    $table->dateTime('calldate');
    $table->dateTime('rememberdate');
    $table->timestamps();
});
Schema::create('clients', function (Blueprint $table) {
    $table->increments('id');
    $table->text('title');
    $table->longText('description');
    $table->integer('fax');
    $table->text('adrress1');
    $table->integer('telephone1');
    $table->timestamps();
});
以及我想在其中展示的观点

@foreach($phonebooks as $phonebook)
<tr>
    <th scope="row">{{$phonebook->id}}</th>
    <th scope="row">{{$phonebook->title}}</th>
    <td><a href="/admin/phonebooks/{{$phonebook->id}}">{{$phonebook->description}}</a></td>
    <td>{{$phonebook->calldate}}</td>
    <td>{{$phonebook->created_at->toFormattedDateString()}}</td>

    <td>{{$client->title}}</td>
    <td>
        <div class="btn-group" role="group" aria-label="Basic example">
            <a href="{{ URL::to('admin/phonebooks/' . $phonebook->id . '/edit') }}">
                <button type="button" class="btn btn-warning">edit</button>
            </a>&nbsp;
            <form action="{{url('admin/phonebooks', [$phonebook->id])}}" method="POST">
                <input type="hidden" name="_method" value="DELETE">
                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                <input type="submit" class="btn btn-danger" value="delete"/>
            </form>
        </div>
    </td>
</tr>@endforeach
@foreach($phonebook作为$phonebook)
{{$phonebook->id}
{{$phonebook->title}
{{$phonebook->calldate}
{{$phonebook->created_at->toFormattedDataTestRing()}
{{$client->title}
@endforeach

如果电话簿中有客户端属性,请使用它

<td>{{$phonebook->client->title}}</td>
{{$phonebook->client->title}

我建议加载所有客户机,以便更高效,您可以在

您可以在控制器上执行此操作:

public function index()
{
    $phonebooks = Phonebook::with('client')->get();
    return view('admin.phonebooks.index',compact('phonebooks',$phonebooks);
}
然后,在您的视图中,您可以访问电话簿客户端,这要归功于您定义的关系:

@foreach ($phonebooks as $phonebook) 
    {{ $phonebook->client->title }}
@endforeach

您得到的输出是什么。有错误吗?
$phonebooks=Phonebook::with('client')->all()?这将按1到n的顺序显示客户端,与数据库中的客户端数量相同,并且它不会使用客户端id作为关系,因此当我有3个客户端时,它会在视图中列出它们,但我希望每个客户端都显示在带有客户端id列的相关行中,以便我能够理解此电话簿中有此客户端。对不起,我不确定是否有此客户端明白你想说什么。您的电话簿只有一个客户端,因此使用我发布的代码,将加载与该电话簿相关的客户端,您希望加载哪些客户端?如果一个电话簿有多个客户端,那么您应该在您的模型中执行此操作,返回$this->hasMany('App\client','id');然后你可以在你的视图上使用foreach($phonebook->clients as client)返回$this->hasOne('App\client');可能是因为您试图在默认情况下指定'id',所以没有必要我会给它一个try亲爱的@pol抱歉我对laravel有点陌生,无法很好地表达这一点我尝试时会让您知道谢谢您的回答:)当我使用它时,它只会显示客户端表中的所有数据,没有关系,并从中排序自上而下:)尝试更改公共函数client(){return$this->hasOne('App\client');