1
0
Fork 0
mirror of https://github.com/archtechx/laravel-tips.git synced 2025-12-11 21:14:02 +00:00

improve thread links

This commit is contained in:
Samuel Štancl 2021-04-09 18:53:27 +02:00
parent 57ab16675e
commit 4f2b5dc999
4 changed files with 19 additions and 6 deletions

View file

@ -2,13 +2,21 @@
namespace App\Models;
use App\Twitter\Tweet;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Collection;
use Orbit\Concerns\Orbital;
/**
* @property string $slug
* @property string $title
* @property string|null $tweet_id
* @property string $author_username
* @property string $content
* @property array $links
*/
class Thread extends Model
{
use Orbital;
@ -59,4 +67,9 @@ class Thread extends Model
{
return false;
}
public function links(): Collection
{
return collect($this->links)->map(fn (array $link, string $key) => route('thread.link', ['thread' => $this, 'link' => $key]));
}
}

View file

@ -42,9 +42,9 @@
<dt class="text-sm text-gray-500">Links</dt>
<dd>
@foreach($links as $link)
<x-link href="{{ $link['url'] }}" target="_blank">
{{ ucfirst($link['name']) }}
@foreach($links as $text => $url)
<x-link href="{{ $url }}" target="_blank">
{{ ucfirst($text) }}
</x-link>
@endforeach
</dd>

View file

@ -24,7 +24,7 @@
<section class="py-12 bg-white md:py-24">
<x:container>
<x:author-card :tip="$thread" :links="$thread->links" />
<x:author-card :tip="$thread" :links="$thread->links()" />
</x:container>
</section>
</main>

View file

@ -22,7 +22,7 @@ Route::middleware('static')->group(function () {
Route::get('/threads/{thread}/{link}', function (Thread $thread, string $link) {
return redirect($thread->links[$link]['url']);
})->name('thread.show');
})->name('thread.link');
Route::get('/threads/{thread}', function (Thread $thread) {
return view('threads.show', [