[英]How to pass the id from a view to a controller to be used as the foreign key in another table?
As the title how to pass id to controller to get related data to that id. 作为标题,如何将ID传递给控制器以获取与该ID相关的数据。
@foreach($persion_data as $person)
{{$person['id']}}
@endforeach
to make it even clearer 使其更加清晰
in procedural programming I will do it like this 在过程编程中,我会这样做
while($persion = sqlsrv_fetch_array($persion_data ,SQLSRV_FETCH_ASSOC))
$id = $persion ['id'];
$customer = "SELECT * FROM [ccl].[customer] WHERE id = '$id' ";
$get_customer_info = sqlsrv_query($conn, $customer);
$get_customer_id = sqlsrv_fetch_array($get_customer_info,SQLSRV_FETCH_ASSOC);
As you see I passed the id to query to get data from another table has relation with the id 如您所见,我将id传递给查询以从另一个表获取数据与id有关系
how can I do this in laravel ?? 我如何在laravel中做到这一点?
I tried to use route but it has to be in url and someone has click it 我尝试使用route,但必须在url中,并且有人单击了它
and it won't work as the example above 它不能像上面的例子一样工作
Please suggest a solution. 请提出解决方案。
You can create a relationship between the 2 models and in your view do 您可以在两个模型之间创建关系,并在视图中
{{ $person->card->name }} //will display 'name' from the card
If the person has many cards, you create a one to many relationship in the person model. 如果此人有很多卡,则可以在该人模型中创建一对多关系。
public function cards() {
return $this->hasMany(Card::class, 'person_id');
}
and in your view, you loop through the person and the person's cards 在您看来,您可以遍历此人和该人的卡片
@foreach($person_data as $person)
@foreach($person->cards as $card)
{{ $card->name }}
@endforeach
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.