2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravelのblade上でURLを短く記述出来るテクニック

2
Posted at

■前提

・掲示板のようなサービスを想定

ソースコード

通常bladeテンプレートでURLを記述する際は以下のように記述する。

<a href="{{ url('/threads/'.$thread->id ) }}">{{ $thread->title }}</a>

Modelに以下のようにpath()メソッドを定義することで、

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Thread extends Model
{
    public function path()
    {
    	return '/threads/'.$this->id;
    }
}


以下のように短く書ける。

<a href="{{ $thread->path() }}">{{ $thread->title }}</a>

以上。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?