首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从另一个模型laravel-api生成器获取数据。

从另一个模型laravel-api生成器获取数据。
EN

Stack Overflow用户
提问于 2015-10-04 01:37:50
回答 2查看 196关注 0票数 1

我正在使用https://github.com/mitulgolakiya/laravel-api-generator

我想从另一个模型中得到数据。插入post时,选择类别数据。

PostController.php

代码语言:javascript
复制
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

代码语言:javascript
复制
{!! Form::select('category_id', Categories::lists('name', 'id'), ['class' => 'form-control']) !!}

我该怎么做?谢谢!

EN

回答 2

Stack Overflow用户

发布于 2015-10-04 02:57:48

您应该在控制器中创建列表并将其传递给视图。

代码语言:javascript
复制
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。

代码语言:javascript
复制
{!! Form::select('category_id', $categoryList, ['class' => 'form-control']) !!}
票数 2
EN

Stack Overflow用户

发布于 2015-10-04 07:45:35

我认为您应该使用ViewComposers并在fields.blade.php中传递类别列表数组。因此,它不会直接影响您的模板或生成器。

每当您需要在任何视图中传递另一个模型的字段时,我认为您应该使用view。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32929197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档