【发布时间】:2020-07-13 13:52:59
【问题描述】:
这是我的代码。我正在尝试访问bookName 和bookAuthor。但是变量是静态设置的。我不想将其更改为公开。但我想访问这些值。我该怎么做?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class Book
{
private $bookName;
private $bookAuthor;
public function __construct($name, $author)
{
$this->bookName = $name;
$this->bookAuthor = $author;
}
public function getNameAndAuthor()
{
return $this->bookName . ' - ' . $this->bookAuthor;
}
}
class BookFactory
{
public static function create($name, $author)
{
return new Book($name, $author);
}
}
class FactoryController extends Controller
{
public function index()
{
$book1 = BookFactory::create('Laravel', 'Imrul');
$book2 = BookFactory::create('ReactJS', 'Hasan');
$book1->getNameAndAuthor();
$book2->getNameAndAuthor();
// dump($book1);
// dd($book1);
return view('home', compact(['book1', 'book2']));
}
}
home.blade.php
<h3>{{ $book1->bookName }}</h3>
<h3>{{ $book1->bookAuthor }}</h3>
【问题讨论】:
-
制作一个模型,它会更容易,你试图访问一个模型的属性,实际上你想要访问的是你的变量:bookName,bookName。如果您尝试从 tee 控制器访问模型的属性,它会显示什么吗?
标签: php laravel static laravel-blade