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 關注
- 167 瀏覽
添加回答
舉報