2017-10-11 79 views
0

有一個表單,它將詳細介紹並將其保存到db中的4個不同表中。項目,活動,捐贈和機會。項目有許多活動,許多捐贈和許多機會。我想在其他表中使用項目ID。但是當我保存表格時,詳細信息存儲在每個表格中,但該項目未被用於其他3個表格事件,捐贈和機會。它的值是0.如何在所有其他三個表中採用該特定的項目ID(自動遞增)。將值插入到laravel中的關係表(引用投影)

我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'); 
} 
} 

回答

0

最好試試下面的代碼

$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'])); 
+0

感謝。但是當我嘗試它時遇到錯誤。 「BadMethodCallException 調用未定義的方法Illuminate \ Database \ Query \ Builder :: event()」出現此錯誤。對不起,我新來laravel。 –

+0

請參閱更新代碼@DebiJeyachandran –

+0

感謝表引用項目ID。但是現在出現了一個新問題。只有捐贈表正在接受價值。事件表插入2行,事件ID&pro_id但其他值爲空。機會表爲空。未插入行。請幫助我 –