1

I am having a problem in a laravel project, i have this entity where it is self referncing. I am creating a PPA (program,project,activity) Entity . So this belongs in an Rc_Indicator Entity. The problem is i do not know to create the self referencing .

So there it how it goes. An Indicator >Program > Project > Activity .

However, an indicator , may ONLY have ACTIVITIES , no program, or project, or the indicator may have a program only have either Project or Activity. Ex.) Rc_Indicator 1 -Program 1.1 -Project 1.1.1 -Activity 1.1.1 -Activity 1.1.12 Ex.)

Rc_Indicator 2 -Program 2.1

-Program 2.2 -Project 2.2.1

Ex.) Rc_Indicator 3

-Activity 1

Erd

Can u suggest a controller or model?

  • 1
    You can easily create self referencing relationships but we need some code or database structure to be able to create an answer that makes sense. – mrhn Mar 10 '23 at 09:18
  • Does this answer your question? [How to create self referential relationship in laravel?](https://stackoverflow.com/questions/20923773/how-to-create-self-referential-relationship-in-laravel) – Alijvhr Mar 10 '23 at 09:37
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 10 '23 at 19:12

3 Answers3

0

Try defining parent and children field:

class PPA extends Eloquent {

    public function parent()
    {
        return $this->belongsTo(PPA:class , 'ppa_parent');
    }

    public function children()
    {
        return $this->hasMany(PPA:class, 'ppa_parent');
    }
}
Alijvhr
  • 1,695
  • 1
  • 3
  • 22
0
public function savePPA(Request $request) {
    $request->validate([
        'indctr_rcs_id'=>'required',
        'ppa_name'=>'required',
        'ppa_parent'=>'required'
        
    ]);

    $indctr_rcs_id = $request->input('indctr_rcs_id');
    $ppa_name = $request->input('ppa_name');
    $ppa_parent = $request->input('ppa_parent');
    $parent_id = $request->input('parent_id');

    if ($ppa_parent == 1) {
        $ppa_name = 'Program';
        $parent_id = null;
    } elseif ($ppa_parent == 2) {
        $ppa_name = 'Project';
        $parent_id = $request->input('program_id');
    } elseif ($ppa_parent == 3) {
        $ppa_name = 'Activity';
        $parent_id = $request->input('project_id');
    }

    DB::table('ppas')->insert([
        'indctr_rcs_id' => $indctr_rcs_id,
        'ppa_name' => $ppa_name,
        'ppa_parent' => $ppa_parent,
        'parent_id' => $parent_id
    ]);

    return redirect('rc/wfp/index')->with('success', 'Data saved successfully!');
}
francisco
  • 1,387
  • 2
  • 12
  • 23
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 20 '23 at 10:24
0

The model looks like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Ppa extends Model
{
    use HasFactory;

    public function indctrRc()
    {
        return $this->belongsTo(IndctrRc::class);
    }
    public function parentPpa()
    {
        return $this->belongsTo(Ppa::class, 'ppa_parent');
    }

    public function childPpas()
    {
        return $this->hasMany(Ppa::class, 'ppa_parent');
    }
}
francisco
  • 1,387
  • 2
  • 12
  • 23