如何从 laravel 中的数组值生成动态 link?
How to make dynamic link from array value in laravel?
我想从数组值中创建 link 的目的是 link 将显示细节的模态。
所以我有这个数组:
array:1 [▼
42 => "00285",
43 =? "00123"
]
我找到了这样的方法:
foreach ($certificate->pluck('number', 'id')->toArray() as $href => $text) {
if ($href != $text) {
echo '<a href="?page=' . $href . '">' . $text . '</a>, ';
} else {
echo $text;
}
}
但在某些情况下,我的控制器将中断到下一个网格 "echo" 必须是 "return",
我想要的示例网格:
但是上面的代码会这样显示:
第一张图片未显示网格内的值
编辑:
我的图片 1 中的代码 - [laravel-admin]:
$grid->column('Sertifikat')->display(function () {
$skips = ["[", "]", "\""];
$certificate = LandCertificate::where('data_order_id', $this->id);
$costumerName = Costumer::where('id', $this->costumer_id);
$certificateNumberArray = $certificate->pluck('number') ?? null;
$temp1 = str_replace($skips, '', $certificateNumberArray);
$certificateNumber = str_replace(',', ', ', $temp1);
if ($certificate->pluck('owner') != $costumerName->pluck('name')) {
return "<span class='text-danger'>$certificateNumber</span>";
} else
return $certificateNumber;
});
实际上,该代码仅显示图1的结果而没有link像图2
Blade代码:
https://github.com/z-song/laravel-admin/blob/master/resources/views/grid/table.blade.php
你必须使用html_entity_decode
if ($certificate->pluck('owner') != $costumerName->pluck('name')) {
return html_entity_decode("<span class='text-danger'>$certificateNumber</span>");
} else {
return $certificateNumber;
}
我找到了解决这个问题的方法。
尝试获取数组值,然后 return 通过如下修改返回:
我的控制器:
$grid->column('Sertifikat')->display(function () {
$skips = ["[", "]", "\""];
$certificate = LandCertificate::where('data_order_id', $this->id);
$costumerName = Costumer::where('id', $this->costumer_id);
foreach ($certificate->pluck('number', 'id')->toArray() as $href => $text) {
if ($href != $text) {
if ($certificate->pluck('owner') != $costumerName->pluck('name')) {
$certificateDetail[] = "<a href='#' data-toggle='modal' data-target='#messageModal' data-id='{$href}' data-from='{$href}' data-title='{$href}' data-message='{$href}' data-time='{$href}'><span class='text-danger'>$text";
} else
$certificateDetail[] = "<a href='#' data-toggle='modal' data-target='#messageModal' data-id='{$href}' data-from='{$href}' data-title='{$href}' data-message='{$href}' data-time='{$href}'>$text";
} else {
$certificateDetail[] = $text;
}
}
$certificateNumberArray = $certificate->pluck('number') ?? null;
$temp1 = str_replace($skips, '', collect($certificateDetail));
$certificateNumber = str_replace(',', ', ', $temp1);
return $certificateNumber;
});
结果:
我想从数组值中创建 link 的目的是 link 将显示细节的模态。
所以我有这个数组:
array:1 [▼
42 => "00285",
43 =? "00123"
]
我找到了这样的方法:
foreach ($certificate->pluck('number', 'id')->toArray() as $href => $text) {
if ($href != $text) {
echo '<a href="?page=' . $href . '">' . $text . '</a>, ';
} else {
echo $text;
}
}
但在某些情况下,我的控制器将中断到下一个网格 "echo" 必须是 "return",
我想要的示例网格:
但是上面的代码会这样显示:
第一张图片未显示网格内的值
编辑:
我的图片 1 中的代码 - [laravel-admin]:
$grid->column('Sertifikat')->display(function () {
$skips = ["[", "]", "\""];
$certificate = LandCertificate::where('data_order_id', $this->id);
$costumerName = Costumer::where('id', $this->costumer_id);
$certificateNumberArray = $certificate->pluck('number') ?? null;
$temp1 = str_replace($skips, '', $certificateNumberArray);
$certificateNumber = str_replace(',', ', ', $temp1);
if ($certificate->pluck('owner') != $costumerName->pluck('name')) {
return "<span class='text-danger'>$certificateNumber</span>";
} else
return $certificateNumber;
});
实际上,该代码仅显示图1的结果而没有link像图2
Blade代码:
https://github.com/z-song/laravel-admin/blob/master/resources/views/grid/table.blade.php
你必须使用html_entity_decode
if ($certificate->pluck('owner') != $costumerName->pluck('name')) {
return html_entity_decode("<span class='text-danger'>$certificateNumber</span>");
} else {
return $certificateNumber;
}
我找到了解决这个问题的方法。
尝试获取数组值,然后 return 通过如下修改返回:
我的控制器:
$grid->column('Sertifikat')->display(function () {
$skips = ["[", "]", "\""];
$certificate = LandCertificate::where('data_order_id', $this->id);
$costumerName = Costumer::where('id', $this->costumer_id);
foreach ($certificate->pluck('number', 'id')->toArray() as $href => $text) {
if ($href != $text) {
if ($certificate->pluck('owner') != $costumerName->pluck('name')) {
$certificateDetail[] = "<a href='#' data-toggle='modal' data-target='#messageModal' data-id='{$href}' data-from='{$href}' data-title='{$href}' data-message='{$href}' data-time='{$href}'><span class='text-danger'>$text";
} else
$certificateDetail[] = "<a href='#' data-toggle='modal' data-target='#messageModal' data-id='{$href}' data-from='{$href}' data-title='{$href}' data-message='{$href}' data-time='{$href}'>$text";
} else {
$certificateDetail[] = $text;
}
}
$certificateNumberArray = $certificate->pluck('number') ?? null;
$temp1 = str_replace($skips, '', collect($certificateDetail));
$certificateNumber = str_replace(',', ', ', $temp1);
return $certificateNumber;
});
结果: