phpmysqllaravellaravel-6laravel-admin

How to make dynamic link from array value in laravel?


The purpose that I want to make a link from an array value is the link will show detail's modal.

So I have this array:

array:1 [▼
  42 => "00285",
  43 =? "00123"
]

I found some method like this:

foreach ($certificate->pluck('number', 'id')->toArray() as $href => $text) {
    if ($href != $text) {
        echo '<a href="?page=' . $href . '">' . $text . '</a>, ';
    } else {
        echo $text;
    }
}

But in some case, that my controller will break to the next grid "echo" must be "return",

Example grid that I wanted:

enter image description here

But the above code will show like this:

enter image description here

Not showing the values inside the grid at first picture

EDIT:

My code from picture 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;
});

Actually, that code only shows the result at picture number 1 without link like picture number 2

Blade code:

https://github.com/z-song/laravel-admin/blob/master/resources/views/grid/table.blade.php


Solution

  • I found the solution to make this problem fixed.

    Try getting array value then return it back with modification like this:

    My Controller:

    $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;
    });
    

    Result:

    The Certificate Colum linkable