我正在使用https://github.com/mitulgolakiya/laravel-api-generator
我想从另一个模型中得到数据。插入post时,选择类别数据。
PostController.php
class PostController extends AppBaseController
{
/** @var PostRepository */
private $postRepository;
/** @var CategoriesRepository */
private $categoriesRepository;
function __construct(PostRepository $postRepo, CategoriesRepository $categoriesRepo)
{
$this->postRepository = $postRepo;
$this->categoriesRepository = $categoriesRepo;
}
/**
* Display a listing of the Post.
*
* @return Response
*/
public function index()
{
$posts = $this->postRepository->paginate(10);
$categories = $this->categoriesRepository->all('id', 'desc')->get();
return view('posts.index')
->with('posts', $posts)
->with('categories', $categories);
}fields.blade.php
{!! Form::select('category_id', Categories::lists('name', 'id'), ['class' => 'form-control']) !!}我该怎么做?谢谢!
发布于 2015-10-04 02:57:48
您应该在控制器中创建列表并将其传递给视图。
public function index()
{
$posts = $this->postRepository->paginate(10);
$categories = $this->categoriesRepository->all('id', 'desc')->get();
$categoryList = [ '' => '--select--' ] + $categories->lists('name', 'id');
return view('posts.index')
->with('posts', $posts)
->with('categories', $categories)
->with('categoryList', $categoryList);
}并使用此变量生成select。
{!! Form::select('category_id', $categoryList, ['class' => 'form-control']) !!}发布于 2015-10-04 07:45:35
我认为您应该使用ViewComposers并在fields.blade.php中传递类别列表数组。因此,它不会直接影响您的模板或生成器。
每当您需要在任何视图中传递另一个模型的字段时,我认为您应该使用view。
https://stackoverflow.com/questions/32929197
复制相似问题