我试图通过此链接重现与Jeffrey Way在Laravel 5基础知识中显示的完全相同的内容https://laracasts.com/series/laravel-5-fundamentals/episodes/22
密切关注10:08
但我的编辑表单在创建表单时没有显示所选标签...我可能错过了一些东西,但我看不到它。
这是我的文章模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Article extends Model
{
protected $fillable = [
'title',
'body',
'published_at'
];
protected $dates = ['published_at'];
public function user()
{
return $this->belongsTo(User::class);
}
public function tags()
{
return $this->belongsToMany(Tag::class);
}
public function getTagListAttribute()
{
return $this->tags->pluck('id');
}
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFromFormat('d.m.Y H:i', $date)->format('Y-m-d H:i:s');
}
public function getPublishedAtAttribute($date)
{
return Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('d.m.Y H:i');
}
public function scopePublished($query)
{
$query->where('published_at', '<=', Carbon::now());
}
public function scopeUnpublished($query)
{
$query->where('published_at', '>', Carbon::now());
}
}
?>
标签型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
public function articles()
{
return $this->belongsToMany(Article::class);
}
}
ArticlesController
<?php
namespace App\Http\Controllers;
use App\Article;
use App\Http\Requests\ArticleRequest;
use Auth;
use App\Tag;
class ArticlesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$articles = Article::latest('published_at')->unpublished()->get();
return view('articles.index', compact('articles'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$tags = Tag::pluck('name', 'id');
return view('articles.create', compact('tags'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(ArticleRequest $request)
{
//dd($request->all());
$article = Auth::user()->articles()->create($request->all());
$article->tags()->sync($request->input('tag_list'));
return redirect('articles')->with('success', "Successfully created a new Article!");
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Article $article)
{
return view('articles.show', compact('article'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Article $article)
{
$tags = Tag::pluck('name', 'id');
return view('articles.edit', compact('article', 'tags'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(ArticleRequest $request, Article $article)
{
$article->update($request->all());
$article->tags()->sync($request->input('tag_list'));
return redirect('articles')->with('success', "Successfully updated the Article!");
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Article $article)
{
//
}
}
?>
edit.blade.php view
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading"><h1>Edit {{ $article->title }}</h1></div>
<div class="panel-body">
{!! Form::model($article, ['method' => 'PATCH', 'action' => ['ArticlesController@update', $article->id]]) !!}
@include('articles._form', ['submitButtonText' => 'Update Article'])
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
@endsection
_form.blade.php partial
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('body', 'Body:') !!}
{!! Form::textarea('body', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('published_at', 'Published On:') !!}
{!! Form::text('published_at', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('tag_list', 'Published On:') !!}
{!! Form::select('tag_list[]', $tags, null, ['class' => 'form-control', 'multiple']) !!}
</div>
<div class="form-group">
{!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-control']) !!}
</div>
所以当我去编辑文章时,没有选择多个选择标签:
它根本不起作用,我不知道出了什么问题....如果你需要任何其他源代码,我会提供它。
答案 0 :(得分:2)
你可以做两件事
在您的模型更改中
public function getTagListAttribute() { 返回$ this-&gt; tags-&gt; pluck('id'); }
要:
public function getTagListAttribute()
{
$tags = $this->tags->pluck('id');
return $tags->all()
}
或者 2.从
更改模型public function getTagListAttribute()
{
return $this->tags->pluck('id');
}
要
public function getTagListAttribute()
{
return array_pluck($this->tags,'id);
}
答案 1 :(得分:0)
您可以尝试:
{!! Form::select('tag_list', $tags, $article->tags->pluck('id')->all(), ['class' => 'form-control', 'multiple']) !!}