第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這段代碼有什么用,“ protected $attributes = []; ”?

這段代碼有什么用,“ protected $attributes = []; ”?

PHP
回首憶惘然 2021-06-16 10:09:21
我是 Laravel 的初學(xué)者。我從 laravel 的網(wǎng)站https://laravel.com/docs/5.8/eloquent#default-attribute-values閱讀了一些信息,它說我們可以在模型中設(shè)置一些默認(rèn)屬性。詳細(xì)說的是:默認(rèn)屬性值如果您想為模型的某些屬性定義默認(rèn)值,您可以在模型上定義 $attributes 屬性:<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Flight extends Model{    /**     * The model's default values for attributes.     * @var array     */    protected $attributes = [        'delayed' => false,    ];}現(xiàn)在,我已經(jīng)在 Laravel 中創(chuàng)建了 CRUD 函數(shù)。并在數(shù)據(jù)庫中設(shè)置一些示例/默認(rèn)值,它是 "id"=1,"element1"="ABC","element2"="abc"。最后,我在顯示表中找不到任何東西。Database Table:...public function up()  {    Schema::create('cruds', function (Blueprint $table) {      $table->bigIncrements('id');      $table->string('element1');      $table->string('element2');  });}...Model:CRUD<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class crud extends Model{    protected $timestramp = false;    protected $primarykey = "id";    protected $attributes =[        'id'        => 1,        'element1'  => "ABC",        'element2'  => "abc",    ];}View.blade.php...<tbody>@foreach ($CRUDitems as $item)  <tr>    <th scope="row">{{ ($item->$id) }}</th>    <td>{{ ($item->$element1) }}</td>    <td>{{ ($item->$element2) }}</td>  </tr>@endforeach</tbody>...CRUDController.php...public function index()  {    $CRUDitems = crud::all();    return view('CRUD.viewTable',compact('CRUDitems')) ;  }...web.php<?phpRoute::resource('/CRUD', 'CRUDController');我想設(shè)置一些默認(rèn)值怎么辦?謝謝你!
查看完整描述

1 回答

?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗 獲得超9個贊

你必須從你的模型中制作一個對象。為此(出于測試目的)讓我們添加這樣的路由:


Route::get('test', 'CRUDController@test');

在你的控制器中添加一個test這樣的方法:


public function test(){

    $crud = new Crud();

    $crud->save();

}

你也可以tinker用來測試你的代碼。


輸入php artisan tinker命令/控制臺,然后創(chuàng)建模型的新實例:


$crud = new App\Crud;

$crud->save();


查看完整回答
反對 回復(fù) 2021-06-25
  • 1 回答
  • 0 關(guān)注
  • 270 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號