skip to Main Content

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?

3

Answers


  1. Chosen as BEST ANSWER
    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!');
    }
    

  2. 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');
        }
    }
    
    Login or Signup to reply.
  3. The model looks like this:

    <?php
    
    namespace AppModels;
    
    use IlluminateDatabaseEloquentFactoriesHasFactory;
    use IlluminateDatabaseEloquentModel;
    
    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');
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search