Laravel,多个属于自动插入ID?

时间:2016-06-16 09:07:00

标签: php mysql laravel eloquent relationship

您好我想将一个Subboard插入到主板上,但是这个子板属于用户和主板,我试图自动插入主板ID和用户ID auto但是我正在使用这个代码:

Subboard.php

<?php

namespace App;

use App\Thread;
use App\User;
use App\Board;
use Illuminate\Database\Eloquent\Model;

class Subboard extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['subboaName'];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'useId' => 'int',
        'boaId' => 'int',
    ];

    /**
     * Obtenemos los threads del board.
     *
     * @return los threads dentro de ese board.
     */
    public function thread()
    {
        return $this->hasMany(Thread::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'useId');
    }

    public function board()
    {
        return $this->belongsTo(Board::class, 'boaId');
    }
}

SubboardController.php(创建方法)

public function subseccion(Request $request)
    {
        $this->validate($request, [
            'subboaName' => 'required|max:25',
            'boaId' => 'required',
        ]);

        $request->user()->subboards()->create([
            'subboaName' => $request->subboaName,
            'boaId' => $request->boaId,
        ]);

        return redirect('/administrar/subsecciones');
    }

Subboards.blade.php(我们插入的表格)

                                        <form class="form col-md-12 center-block" role="form" method="POST" action="{{ url('/administrar/subsecciones') }}">
                                            {!! csrf_field() !!}

                                            <div class="form-group{{ $errors->has('selectboa') ? ' has-error' : '' }}">
                                                <select name="boaId" class="form-control input-lg selectpicker" data-live-search="true" title="Selecciona sección" data-style="input-lg btn-default">
                                                    @foreach ($boards as $board)
                                                        <option value="{{$board->id}}">{{$board->boaName}}</option>
                                                    @endforeach
                                                </select>
                                                @if ($errors->has('boaId'))
                                                    <p class="help-block">
                                                        <div class="alert alert-danger" role="alert">
                                                          <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                                                          <span class="sr-only">Error:</span>
                                                          {{ $errors->first('boaId') }}
                                                        </div>
                                                    </p>
                                                @endif
                                            </div>

                                            <div class="form-group{{ $errors->has('subboaName') ? ' has-error' : '' }}">
                                                <input type="text" class="form-control input-lg" placeholder="Nombre de la subsección" name="subboaName" value="{{ old('subboaName') }}">
                                                @if ($errors->has('subboaName'))
                                                    <p class="help-block">
                                                        <div class="alert alert-danger" role="alert">
                                                          <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                                                          <span class="sr-only">Error:</span>
                                                          {{ $errors->first('subboaName') }}
                                                        </div>
                                                    </p>
                                                @endif
                                            </div>
                                            <div class="form-group">
                                                <button type="submit" class="btn btn-default btn-lg btn-block">
                                                    Añadir
                                                </button>
                                            </div>
                                        </form>

1 个答案:

答案 0 :(得分:1)

将字段名称添加到 Calendar calendar = Calendar.getInstance(); mYear = calendar.get(Calendar.YEAR); mMonth = calendar.get(Calendar.MONTH); mDay = calendar.get(Calendar.DAY_OF_MONTH); //OnDateSetListener private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) { calendar.set(year, monthOfYear, dayOfMonth); String currentDate = date_format.format(calendar.getTime()); ... } }; //Create DatePickerDialog here protected Dialog onCreateDialog(int id) { if (id == 999) { DatePickerDialog dpDialog = new DatePickerDialog(getContext(), myDateListener, mYear, mMonth, mDay); return dpDialog; } return null; } 属性

$fillable