函数 edit() 的语法文件夹存储库
Syntax folder repository for the function edit()
我有 2 个外键,它们是 fk_author
和 fk_bookcase
,我试图通过文件夹 Repositorie
创建我的函数 edit() 但我又被语法困住了.
这是我通过文件 BookRepository
编写的代码
public function edit($id)
{
$books = Book::find($id);
$authors = Author::all();
$bookcases = Bookcase::all();
return Book::find($id);
}
然后,在我的控制器中我有这个...
public function edit($id)
{
$books = $this->books->edit($id);
return view('admin.books.edit', compact('books', 'authors', 'bookcases'));
}
你知道这个问题吗?
此致
如果要检索与 'author' 和 'bookcase' 相关的图书,您必须在模型中定义关系。例如:
图书型号
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 对象。
图书资料库
public function detail($id)
{
return Book::with([
'author',
'bookcase',
])
->find($id);
}
然后,在 Controller 中,是的,您有一个编辑功能,可以从存储库获取详细信息,return 将对象添加到编辑视图。
/**
* @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'));
}
如果在任何情况下你还想 return 所有作者和书架,我认为最好为每个人创建一个存储库,这样你也可以从其他控制器或 类.
AuthorRepository
public function getAll()
{
return Author::all();
}
BookcaseRepository
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'));
}
我有 2 个外键,它们是 fk_author
和 fk_bookcase
,我试图通过文件夹 Repositorie
创建我的函数 edit() 但我又被语法困住了.
这是我通过文件 BookRepository
public function edit($id)
{
$books = Book::find($id);
$authors = Author::all();
$bookcases = Bookcase::all();
return Book::find($id);
}
然后,在我的控制器中我有这个...
public function edit($id)
{
$books = $this->books->edit($id);
return view('admin.books.edit', compact('books', 'authors', 'bookcases'));
}
你知道这个问题吗?
此致
如果要检索与 'author' 和 'bookcase' 相关的图书,您必须在模型中定义关系。例如:
图书型号
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 对象。
图书资料库
public function detail($id)
{
return Book::with([
'author',
'bookcase',
])
->find($id);
}
然后,在 Controller 中,是的,您有一个编辑功能,可以从存储库获取详细信息,return 将对象添加到编辑视图。
/**
* @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'));
}
如果在任何情况下你还想 return 所有作者和书架,我认为最好为每个人创建一个存储库,这样你也可以从其他控制器或 类.
AuthorRepository
public function getAll()
{
return Author::all();
}
BookcaseRepository
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'));
}