How to create a form for hasManyThrough relationships, with a pivot point, in a Laravel5 view

I come from the Rails background, where we can easily create has_many_through with the right links.

My models in this project: Deal <=> DealsFaqs <=> Faqs

In the Transaction model, I:

public function faqs(){ return $this->hasManyThrough('App\Faq', 'App\DealsFaqs'); } 

Faqs transactions have:

  public function deals() { return $this->belongsTo('App\Deal'); } public function faqs() { return $this->belongsTo('App\Faq'); } 

and my Faq model has:

  public function deals(){ return $this->hasManyThrough('App\Deal', 'App\DealsFaqs'); } 

The deal must be related to N faqs.

So, I created a table for DealsFaqs that has deal_id and faq_id fields.

I can easily list the FAQ on the transaction form page, but this is not related to offers.

How to display faqs, but send them through the form so that I can check the connection in the controller when sending?

--- EDIT ON REQUEST FOR MORE CODE AND DETAILS:

In the end, it will be about 20-30%. And an increasing number of transactions. When the administrator creates each transaction, they need to choose which questions relate to the transaction. Some will be relevant, while others will not. Then these FAQs will be displayed on the page.

I suggested that I need hasManyThrough, because I can’t just put one faq_id field in Deal, since the FAQ often requires a lot of the same thing. Frequently asked questions apply to many transactions.

My DealCreate method in the controller:

  /////////////////// // ASSOCIATE FAQ TO DEALS /////////////////// if($request->faqs){ foreach($request->faqs as $key => $faq){ // Create new DealsFaqs $entry = new \App\DealsFaqs; // Populate it $entry->faq_id = $faq['id']; $entry->deal_id = $deal->id; $entry->save(); // Save the DealsFaqs } } 

Then my current section of the form will link the FAQ to the suggestions:

  <?php $a=0; foreach($faqs as $faq): ?> <div class="col-sm-12"> <label> <input type="radio" name="faqs[{{ $a }}][id]" value="{{ $faq->id }}"> {{ $faq->name }} </label> </div> <?php $a++; endforeach; ?> 

When I select 1 or all of them, a new relationship is created in the deal_faqs table we created. But then for obvious reasons (I don’t know how to solve them) But the radios don’t know how to stay checked, and I don’t know how to access / update the controller. ALSO is this the right way to do this, or am I mistaken in a relationship?

+7
laravel has-many-through
source share
1 answer

You said that dealing with an attitude has a lot through , which, as I understand it, requires the work of 3 separate models. I feel that you are working with a ratio from many to many , since only you are working with two models.

As you said, you come from the Rails background, I feel that I am either making a huge mistake, or I don’t understand your relationship with db, or you have a lot, and many of them are confused.

FYI: Enough, from laravel docs:

The has-many-through relationship provides a convenient short distance to access distance relationships through an intermediate relationship. For example, a country model can have many Post models through an intermediate user model.

More details here: https://laravel.com/docs/5.2/eloquent-relationships#has-many-through

and from many to many: https://laravel.com/docs/5.2/eloquent-relationships#many-to-many

If you work with many for many, I believe that your database schema should consist of three tables:

deals , faqs and the faqs staging table, the third table name follows the laravel conventions for the staging table, where the singular names of the linked tables are used in alphabetical order with underscores in between. For the deal_faq table, deal_id and faq_id .

Then you will need two models (only two, not three)

Deal and Faq with the following methods:

Deal Model:

 public function faqs(){ return $this->belongsToMany('App\Deal'); } 

Faq Model:

 public function deals(){ return $this->belongsToMany('App\Faq'); } 

Now, to use them, you can do the following, for example:

 // get a deal $deal = App\Deal::first(); // grab the first one in the db // get faqs linked to deal $deal->faqs; // connect an faq and deal together $faq = App\Faq::first(); $deal->attach($faq->id); // where id is the primary key attribute 

Note that this answer assumes that you perform laravel settings when defining relationships.

And I am very sorry that I do not completely understand this part of your question: "How do I display the faqs, but submit them through the form so that I can check the relationship in the controller by presentation?"

I assume that you are asking if I can represent Faq in a form, how can I capture a Faq relationship. In this case, you can do something similar if you use the Faq id in your form request.

 $faqId = $request->faq_id; $faq = App\Faq::find($faqId); $deals = $faq->deals; 

As you have not shown a lot of code, it's hard to say, and I made a lot of assumptions. In any case, I hope this helps, and if you need any further clarification, please ask and provide more detailed information about your code, if you can.

+1
source share

All Articles