Php Laravel、多个belongsto和自动插入ID?
你好,我想在线路板中插入一个子板,但此子板属于用户和线路板,我正在尝试自动插入线路板id和用户id,但线路板id无效这是我的代码: Subboard.phpPhp Laravel、多个belongsto和自动插入ID?,php,mysql,laravel,eloquent,relationship,Php,Mysql,Laravel,Eloquent,Relationship,你好,我想在线路板中插入一个子板,但此子板属于用户和线路板,我正在尝试自动插入线路板id和用户id,但线路板id无效这是我的代码: Subboard.php <?php namespace App; use App\Thread; use App\User; use App\Board; use Illuminate\Database\Eloquent\Model; class Subboard extends Model { /** * The attribute
<?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');
}
}
Subboards.blade.php(我们插入的表单)
{!!csrf_field()!!}
@foreach($board作为$board)
{{$board->boaName}
@endforeach
@如果($errors->has('boaId'))
错误:
{{$errors->first('boaId')}
@恩迪夫
@如果($errors->has('subboaName'))
错误:
{{$errors->first('subboaName')}
@恩迪夫
阿尼亚迪尔
将文件名添加到$filleble
属性中
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'subboaName',
'boaId',
'useId',
];
<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>
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'subboaName',
'boaId',
'useId',
];