函数edit()的语法文件夹存储库 [英] Syntax folder repository for the function edit()
问题描述
我有2个外键,分别是 fk_author
和 fk_bookcase
,我试图通过文件夹 Repositorie
创建我的函数edit(),但是我再次陷入语法.
I have 2 foreign keys which are fk_author
and fk_bookcase
, I am trying to create my function edit() via a folder Repositorie
but I am stuck on the syntax again.
这是我的代码,通过文件 BookRepository
Here is my code via the file BookRepository
public function edit($id)
{
$books = Book::find($id);
$authors = Author::all();
$bookcases = Bookcase::all();
return Book::find($id);
}
然后,在我的控制器中,我有这个...
Then, in my Controller I have this...
public function edit($id)
{
$books = $this->books->edit($id);
return view('admin.books.edit', compact('books', 'authors', 'bookcases'));
}
您对这个问题有想法吗?
Do you have an idea of the problem?
致谢
推荐答案
如果要检索具有相关作者"和书架"的书,则必须在模型中定义关系.例如:
If you want to retrieve the book with the related 'author' and 'bookcase', you must have defined the relations in the models. For ex:
图书型号
public function author()
{
return $this->belongsTo(Author::class, 'fk_author'); // change fk_author for the key you are using
}
public function bookcase()
{
return $this->belongsTo(Bookcase::class, 'fk_bookcase');
}
作者模型
public function books()
{
return $this->hasMany(Book::class);
}
书架型号
public function books()
{
return $this->hasMany(Book::class);
}
您无需在存储库中使用 edit()
函数,只需一个 detail()
(或所需名称)即可检索Book对象与关系.
And you doesn't need and edit()
function in your repository, just a detail()
(or the name what you want) which retrive the Book Object with the relations.
BookRepository
public function detail($id)
{
return Book::with([
'author',
'bookcase',
])
->find($id);
}
然后,在 Controller 中,是的,您具有编辑功能,该功能可从存储库获取详细信息并将对象返回到编辑视图.
Then, in the Controller, yes, you have an edit function which get the detail from the repository and return the object to the edit view.
/**
* @var BookRepository
*/
private $books;
public function __construct(BookRepository $books)
{
$this->books = $books;
}
public function edit($id)
{
$book = $this->books->detail($id);
return view('admin.books.edit', compact('book'));
}
无论如何,如果您还想归还所有作者和书架,我认为最好为每个作者和书架都建立一个存储库,这样您也可以在其他Controllers或Classes中使用它们.
If in any case you want to also return all the authors and bookcases, I think it is better to make a repository for each one, so you can also use them from other Controllers or Classes.
AuthorRepository
public function getAll()
{
return Author::all();
}
书架存储库
public function getAll()
{
return Bookcase::all();
}
然后,在控制器
/**
* @var BookRepository
*/
private $books;
/**
* @var AuthorRepository
*/
private $authors;
/**
* @var BookcaseRepository
*/
private $bookcases;
public function __construct(BookRepository $books, AuthorRepository $authors, BookcaseRepository $bookcases)
{
$this->books = $books;
$this->authors = $authors;
$this->bookscases = $bookcases;
}
public function edit($id)
{
$book = $this->books->detail($id);
$authors = $this->authors->getAll();
$bookcases = $this->bookcases->getAll();
return view('admin.books.edit', compact('book', 'authors', 'bookcases'));
}
这篇关于函数edit()的语法文件夹存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!