Php 对象注入错误
我正在从事一个Laravel 5.4项目。我面临一个问题。我想更多地了解这个问题 下面的代码正在工作 PostController.phpPhp 对象注入错误,php,laravel,laravel-5,laravel-5.4,Php,Laravel,Laravel 5,Laravel 5.4,我正在从事一个Laravel 5.4项目。我面临一个问题。我想更多地了解这个问题 下面的代码正在工作 PostController.php <?php namespace App\Http\Controllers; use DB; use View; use App\Model\Book; use App\Model\Chapter; use App\Model\Post; class PostController extends Controller { public
<?php
namespace App\Http\Controllers;
use DB;
use View;
use App\Model\Book;
use App\Model\Chapter;
use App\Model\Post;
class PostController extends Controller
{
public function index(Book $book,Chapter $chapter)
{
$books = Book::all();
$posts = Post::get_post($book,$chapter);
$data = array(
'posts' => $posts,
'books' => $books,
'section_id' => ''
);
return View::make('posts')->with($data);
}
}
<?php
namespace App\Http\Controllers;
use DB;
use View;
use App\Model\Book;
use App\Model\Chapter;
use App\Model\Post;
class PostController extends Controller
{
public function index(Book $book,Chapter $chapter)
{
$books = Book::all();
$posts = Post::get_post(Book $book,Chapter $chapter); //issue is in this line
$data = array(
'posts' => $posts,
'books' => $books,
'section_id' => ''
);
return View::make('posts')->with($data);
}
}
此行的问题:
$posts = Post::get_post(Book $book,Chapter $chapter); //issue is in this line
调用函数时,不会键入提示类型。您应该像这样运行函数:
$posts = Post::get_post($book, $chapter);
public static function get_post(Book $book,Chapter $chapter)
{
// do whatever you want here
}
是的,您可以这样声明get_post函数:
$posts = Post::get_post($book, $chapter);
public static function get_post(Book $book,Chapter $chapter)
{
// do whatever you want here
}
但是现在,当你打电话时,你不再使用Book
或Chapter
这一行的问题:
$posts = Post::get_post(Book $book,Chapter $chapter); //issue is in this line
调用函数时,不会键入提示类型。您应该像这样运行函数:
$posts = Post::get_post($book, $chapter);
public static function get_post(Book $book,Chapter $chapter)
{
// do whatever you want here
}
是的,您可以这样声明get_post函数:
$posts = Post::get_post($book, $chapter);
public static function get_post(Book $book,Chapter $chapter)
{
// do whatever you want here
}
但是现在当你打电话时,你不再在这个函数中使用Book
或Chapter
,公共函数索引(Book$Book,Chapter$Chapter)()了,你从那里得到$Book我从模型中得到了这个$Book
。谢谢如果您调用的模型正确,那么无需再次通过..公共函数索引(Book$Book,Chapter$Chapter)()在这个函数中,您从那里获取$Book我从模型获得这个$Book
。谢谢,如果你打对了模特,那么就不用再打了。。