[英]one to many realtionship in laravel showing in view
i am working on laravel phonebook application i want every phonebook to hasOne clients but the clients belongs to many phone books i have a column in my phonebook table called client_id now when i set relations how can i use them in controller and pass it into view as on object and here is some of my code 我正在laravel电话簿应用程序上工作,我希望每个电话簿都具有一个客户端,但这些客户端属于许多电话簿,现在当我设置关系时,如何在控制器中使用它们并将其作为视图传递给我,因此我在电话簿表中有一列称为client_id关于对象,这是我的一些代码
phonebook controller 电话簿控制器
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));}
phonebook model 电话簿模型
class Phonebook extends Model{protected $fillable = ['title','description','client_id','calldate','rememberdate'];public function client() {
return $this->hasOne('App\Client','id');} }
phonebook db migration 电话簿数据库迁移
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();
});
and the client db migration 和客户端数据库迁移
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();
});
and the view that i want to show in it 和我想在其中显示的视图
@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>
<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
您在电话簿中具有客户端属性,请使用它
<td>{{$phonebook->client->title}}</td>
I would suggest loading all the clients with eager loading so it will be more efficient, you can read why on the docs 我建议先加载所有客户端,这样它会更高效,您可以在文档中阅读原因
You could do this on your controller: 您可以在控制器上执行此操作:
public function index()
{
$phonebooks = Phonebook::with('client')->get();
return view('admin.phonebooks.index',compact('phonebooks',$phonebooks);
}
Then in your view you can access the phonebook client thanks to the relationship you have defined doing this: 然后,在您看来,由于您已定义的关系,您可以访问电话簿客户端:
@foreach ($phonebooks as $phonebook)
{{ $phonebook->client->title }}
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.