gpt4 book ai didi

php - Laravel,多个 belongsto 和自动插入 ID?

转载 作者:行者123 更新时间:2023-11-28 23:29:24 24 4
gpt4 key购买 nike

你好,我想将一个子板插入板,但这个子板属于用户和板,我正在尝试自动插入板 ID 和用户 ID,但板 ID 无法正常工作这是我的代码:

子板.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>

最佳答案

将字段名称添加到$fillable 属性

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'subboaName',
'boaId',
'useId',
];

关于php - Laravel,多个 belongsto 和自动插入 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37854658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com