在覆盖表名称之后创建不起作用

问题描述 投票:0回答:1

__ construct]上覆盖表名之后,未按预期创建它。它仅存储唯一的默认值。

Controller:“ TestMeController”

use App\TestMe;
use Illuminate\Http\Request; 
use Log;

class TestMeController extends Controller
{
    public function setCreateData() {

        config(['app.temp_db' => "new_test_me"]);
        $test_me_data = ["data" => "new table data"];
        $data = TestMe::create( $test_me_data );

        dd($data);
    }
}

Model:“ TestMe”

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class TestMe extends Model
{

    protected $table = "test_me";

    protected $guarded = ['id']; 

    protected $connection = "mysql";

    public function __construct() {


        if( config('app.temp_db') != "") {
            $this->table = config('app.temp_db');
        }  

    }
}

输出

.....
#original: array:3 [
    "updated_at" => "2020-01-06 13:34:18"
    "created_at" => "2020-01-06 13:34:18"
    "id" => 100
]
.....

它仅添加了默认值,我必须尝试获取Exception,但是它没有例外。

在__construct上覆盖表名后,未按预期创建。它仅存储唯一的默认值。控制器:“ TestMeController”使用App \ TestMe;使用Illuminate \ Http \ Request; ...

php laravel eloquent laravel-5.6
1个回答
0
投票

只需要在construct上传递$ attributes

© www.soinside.com 2019 - 2024. All rights reserved.