0

I have 3 tables :legalpursuit , guarantors, guarantors_legalpursuit. and I have Many To Many Relationships in my project. I save legalpursuit_id in legalpursuit / guarantors_id in guarantors / and both of them in guarantors_legalpursuit.

I am using the laravel 8. how can I show every post's tags ? how can I select data from tag_post table? I use it as follows. but it does not get the data.

it's my Legalpursuit model:

public function guarantorses()
{
    //return $this->belongsToMany(RelatedModel, pivot_table_name, foreign_key_of_current_model_in_pivot_table, foreign_key_of_other_model_in_pivot_table);
    return $this->belongsToMany(Guarantors::class, 'guarantors_legalpursuit', 'legalpursuit_id', 'guarantors_id');
}

it's my Guarantor model:

    public function legalpursuits()
{
    //return $this->belongsToMany(RelatedModel, pivot_table_name, foreign_key_of_current_model_in_pivot_table, foreign_key_of_other_model_in_pivot_table);
    return $this->belongsToMany(LegalPursuit::class,'guarantors_legalpursuit','guarantors_id', 'legalpursuit_id');
}

and it's my controller :

    public function edit(LegalPursuit $legalPursuit, $id)
{
    $user           = Auth::user();
    $data           = LegalPursuit::find($id);

    $guarantors     = $data->guarantorses()->orderBy('name')->get();
    $lawyers        = $data->lawyers()->orderBy('name')->get();

    dd($guarantors);

    $filesPursuit   = LegalPursuit::find($id)->files;
    $getLawyersLeft = DB::table('users')->where('user_type', '2')
                                        ->where('active', '1')
                                        ->where('deleted', '0')
                                        ->take('5')
                                        ->get();

    return view('pages.legalpursuit.edit', compact('data', 'getLawyersLeft', 'filesPursuit', 'user'));
}
  • You use Spanish plural names for some of the tables. There is nothing wrong with that. But you must tell your Model what table it is: `protected $table = 'guarantorses';`. You may have done that. If not, Laravel will follow its own protocol, ergo `'guarantors_id'` won't translate to the table name `'guarantorses'`. At first sight, there is nothing wrong with your models and relationships. – Dimitri Mostrey Mar 29 '21 at 12:00
  • protected $table = 'guarantors'; – KemalToygun Mar 29 '21 at 18:06

1 Answers1

0

no table name is different and it actually follows exactly;

protected $table    = 'guarantors';

The fields of the table are as follows;

table name; guarantors
field: ID
field: name

pivot tablo name and details;

 table name; guarantors_legalpursuit
id;
guarantors_id
legalpursuit_id



I can also do the adding process successfully as follows.

        $guarantorArray = $request->input('GUARANTORS');
        //$GuarantorsID = Guarantors::find($guarantorArray);
        $data->guarantorses()->attach($guarantorArray);

but I can't pull relational data. it does not give an error. but although there is data in the table, the array feels empty.

  • Thanks. In this case, the pivot table name should be `guarantor_legalpursuit`, both in singular. – Dimitri Mostrey Mar 30 '21 at 03:05
  • II changed the table name exactly as you said, but the problem was not solved. – KemalToygun Mar 30 '21 at 05:32
  • Bro, your problem is a mystery. Try do `dd()` everything you can and find out what is going on. Nothing looks wrong in your code. But somehow it doesn't work. Please let us know if you have found the reason. – Dimitri Mostrey Mar 30 '21 at 12:33