我使用Laravel建立了一个博客,但是我还没有弄清楚如何在其中添加标签。我想避免使用软件包。这是我到目前为止的代码:
标签控制器-
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tag;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Session;
class TagsController extends Controller
{
public function index() {
$tags = Tags::paginate(4);
return view('dashboard/tags/index', compact('tags'));
}
public function create() {
return view('dashboard/tags/create');
}
public function store() {
$tags = new Tags;
$tags->name= $request->input('name');
$tags->save();
Session::flash('flash_message', 'Tag successfully added!');
return Redirect('/dashboard/tags');
}
}
Run Code Online (Sandbox Code Playgroud)
标签模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
public function posts()
{
return $this->belongsToMany('App\Post');
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定如何添加多个标签,因此可以将其放在帖子上。
我使用Laravel 5.3。