2 回答
TA贡献1780条经验 获得超5个赞
我建议您创建一个模型: php artisan make:model Book -a,其中 -a 将为您创建除模型之外的迁移和控制器。
在您的迁移中:
public function up()
{
Schema::table('books', function (Blueprint $table) {
$table->increments('id');
$table->string('author');
$table->string('title');
});
}
在你的模型上:
class Book extends Model
{
protected $table = 'books';
protected $fillable = [
'author', 'title'
];
}
在您的控制器上:
public function create()
{
$book1 = Book::create([
'author' => 'Henry',
'title' => 'Smith',
]);
$book2 = Book::create([
'author' => 'Antony',
'title' => 'Gjj',
]);
return view('home', compact(['book1', 'book2']));
}
在你的刀片上:
<h3>{{ $book1->title }}</h3>
<h3>{{ $book1->author }}</h3>
TA贡献1951条经验 获得超3个赞
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;
}
public function getBookNameAttribute()
{
return $this->bookName;
}
public function getBookAuthorAttribute()
{
return $this->bookAuthor;
}
}
现在刀片中的代码应该可以工作:
<h3>{{ $book1->bookName }}</h3>
<h3>{{ $book1->bookAuthor }}</h3>
- 2 回答
- 0 关注
- 130 浏览
添加回答
举报