gpt4 book ai didi

php - 在laravel中将值插入到具有关系的表单中(引用projectid)

转载 作者:行者123 更新时间:2023-11-29 18:19:52 25 4
gpt4 key购买 nike

有一个单一的表单,它接收详细信息并将其保存到数据库中的 4 个不同的表中。项目、事件、捐赠和机会。项目有很多事件、很多捐赠和很多机会。我也想在其他表中使用项目 ID。但是当我保存表格时,详细信息存储在每 4 个表中,但其他 3 个表“事件”、“捐赠”和“机会”并未采用该项目。它的值为 0。如何在所有其他三个表中获取该特定项目 id(自动增量)。

我的ProjectController是这样的:

class ProjectController extends Controller
{
public function getProject()
{
return view ('other.project');
}

public function postProject(Request $request)
{
$this->validate($request, [
'ptitle' => 'required|max:200',
'pdescription' => 'required',
'etitle' => 'required|max:200',
'edetails' => 'required',
'dtotal' => 'required',
'oposition' => 'required|max:100',
'odescription' => 'required',
]);

Project::create([
'ptitle' => $request->input('ptitle'),
'pdescription' => $request->input('pdescription'),
'pduration' => $request->input('pduration'),
'psdate' => $request->input('psdate'),
'pedate' => $request->input('pedate'),
'pcategory' => $request->input('pcategory'),
'pimage' => $request->input('pimage'),
]);

Event::create([
'pro_id' => $request->input('pid'),
'etitle' => $request->input('etitle'),
'pdetails' => $request->input('pdetails'),
'edate' => $request->input('edate'),
'etime' => $request->input('etime'),
'elocation' => $request->input('elocation'),
'eimage' => $request->input('eimage'),
]);

Donation::create([
'pro_id' => $request->input('pid'),
'dtotal' => $request->input('dtotal'),
'dinhand' => $request->input('dinhand'),
'dbankaccount' => $request->input('dbankaccount'),
]);

Opportunity::create([
'pro_id' => $request->input('pid'),
'oposition' => $request->input('oposition'),
'odescription' => $request->input('odescription'),
'olocation' => $request->input('olocation'),
'odeadline' => $request->input('odeadline'),
]);

return redirect()
->route('home')
->with('info', 'Your project has been created.');
}

我的项目模型:

class Project extends Model
{
use Notifiable;

protected $fillable = [
'ptitle',
'pdescription',
'pduration',
'psdate',
'pedate',
'pcategory',
'pimage',
];

public function events()
{
return $this->hasMany('Ngovol\Models\Event', 'pro_id');
}

public function donations()
{
return $this->hasMany('Ngovol\Models\Donation', 'pro_id');
}

public function opportunities()
{
return $this->hasMany('Ngovol\Models\Event', 'pro_id');
}

protected $hidden = [

];
}

事件模型:

class Event extends Model
{
use Notifiable;

protected $table = 'events';

protected $fillable = [
'pro_id',
'etitle',
'edetails',
'edate',
'etime',
'elocation',
'eimage',
];

public function projects()
{
return $this->belongsTo('Ngovol\Models\Project', 'pro_id');
}
}

捐赠模式:

class Donation extends Model
{
use Notifiable;

protected $fillable = [
'pro_id',
'dtotal',
'dinhand',
'drequired',
'dbankaccount',
];

protected $hidden = [

];

public function projects()
{
return $this->belongsTo('Ngovol\Models\Project', 'pro_id');
}
}

机会模型:

class Opportunity extends Model
{
use Notifiable;

protected $fillable = [
'pro_id',
'oposition',
'odescription',
'olocation',
'odeadline',

];

protected $hidden = [

];

public function projects()
{
return $this->belongsTo('Ngovol\Models\Project', 'pro_id');
}
}

最佳答案

最好尝试以下代码

$project = Project::create( $request->only(['ptitle', 'pdescription', 'pduration', 'psdate', 'pedate', 'pcategory', 'pimage']));

$project->events()->create( $request->only(['etitle', 'pdetails', 'edate', 'etime', 'elocation', 'eimage']));

$project->donations()->create($request->only(['dtotal', 'dinhand', 'dbankaccount']));

$project->opportunities()->create($request->only(['oposition','odescription','olocation','odeadline']));

关于php - 在laravel中将值插入到具有关系的表单中(引用projectid),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46680885/

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