我通过以下迁移创建了两个laravel表:
用户迁移:
public function up()
{
    Schema::create('users', function($table){
        $table->increments('id')->unsigned();
        $table->string('email')->unique();
        $table->string('password', 64);
        $table->string('first_name', 32);
        $table->string('last_name', 32);
        $table->string('remember_token', 100)->nullable();
    });
}
Run Code Online (Sandbox Code Playgroud)
场合迁移:
public function up()
{
    Schema::create('occasions', function($table){
        $table->increments('id')->unsigned();
        $table->integer('created_by_user_id')->unsigned();
        $table->foreign('created_by_user_id')->references('id')->on('users');
        $table->integer('updated_by_user_id')->unsigned();
        $table->foreign('updated_by_user_id')->references('id')->on('users');
        $table->timestamps();
        $table->string('title')->unique();
        $table->string('slug')->unique();
        $table->integer('category')->unsigned();
        $table->foreign('category')->references('id')->on('occasion_categories');
        $table->string('brand', 32);
        $table->string('model', 32)->nullable();
        $table->string('type', 32)->nullable();
        $table->string('body', 32)->nullable();
        $table->string('color', 32);
        $table->integer('fuel')->unsigned()->nullable();
        $table->foreign('fuel')->references('id')->on('occasion_fuels');
        $table->integer('transmission')->unsigned()->nullable();
        $table->foreign('transmission')->references('id')->on('occasion_transmissions');
        $table->decimal('usage', 6, 2)->nullable();
        $table->integer('engine_capacity')->unsigned()->nullable();
        $table->integer('building_year')->unsigned()->nullable();
        $table->string('sign', 8)->nullable();
        $table->date('mot')->nullable();
        $table->integer('kilometers')->nullable();
        $table->decimal('price', 8, 2);
        $table->decimal('action_price', 8, 2)->nullable();
        $table->text('description')->nullable();
    });
}
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试使用以下代码创建一个场合时:
Occasion::create(array(
    'created_by_user_id'=>Auth::id(),
    'updated_by_user_id'=>Auth::id(),
    'title'=>Input::get('title'),
    'slug'=>Str::slug(Input::get('title')),
    'category'=>Input::get('category'),
    'brand'=>Input::get('brand'),
    'model'=>Input::get('model'),
    'type'=>Input::get('type'),
    'body'=>Input::get('body'), …Run Code Online (Sandbox Code Playgroud) 我已经在一个类中创建了这个函数,虽然它有效但我认为它很麻烦并且可以很容易地简化,我可以使用什么技术来简化这个?虽然我可以使用foreach,但每次它都会在Cache文件夹中创建新目录而不是在父目录中创建目录.
示例: $directory = 'page/subpage/homepage'
public static function add_directory( $directory, $cache_path ) {
    $all_directories = explode( '/', $directory );
    $total_directories = count( $all_directories );
    if( $total_directories == 1 ) {
        if( ! File::exists( $cache_path.'/'.$all_directories[0] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0] );
        }
    }
    else if( $total_directories == 2 ) {
        if( ! File::exists( $cache_path.'/'.$all_directories[0] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0] );
        }
        if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] );
        }
    }
    else if( $total_directories == 3 ) …Run Code Online (Sandbox Code Playgroud)