2017-10-19 7 views
0

J'ai une liste d'offres d'emploi sur une page. Pour chaque publication, j'ai un bouton appelé "envoyer à un ami". En cliquant sur le bouton, vous obtenez un modal avec un formulaire dans lequel vous pouvez entrer le courriel de votre ami et soumettre le formulaire.Laravel et Modals: Transmettre la valeur @foreach unique en forme modale?

Comment puis-je extraire le nom de l'offre d'emploi correspondante dans mon formulaire? {{Posting- $> name}}

Offres d'emploi page:

@foreach ($postings as $posting) 

        <a href="postings/{{ $posting->id }}"> <p> {{ $posting-> short_desc }} </p> </a> 
        <p> {{ $posting->location }} | {{ $posting->duration }} </p> 

        <button type="button" class="btn btn-outline" data-toggle="modal" href="postings/sendfriend" value="{{ $posting->short_desc }}" data-target="#sendfriend" style="background-color: #004AAE; color: #FFF; font-family: Lato; border-radius: 10px;">Send to a friend</button> 

@endforeach 

@include('postings.sendfriend') 

Modal:

<div id="sendfriend" class="modal fade" role="dialog"> 
<div class="modal-dialog"> 


<div class="modal-content"> 
    <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal">&times;</button> 
     <h4 class="modal-title">Invite a friend to apply for this rotation</h4> 
    </div> 
     <div class="modal-body"> 
     <form class="form-horizontal" method="post" action="{{ action('[email protected]') }}"> 
    <div class="form-group"> 
    <label class="control-label col-sm-2" for="email">Email:</label> 
    <div class="col-sm-10"> 
     <input type="email" class="form-control" id="email" placeholder="Enter friend's email"> 
    </div> 
    </div> 

    <button type="submit" class="btn btn-outline"> Share the love </button> 

</div></div> 
</form></div></div> 

contrôleur ami:

public function store() { 
    $friend = new Friend; 
    $friend->email = request('email'); 
    $friend->company = $posting->name; //Something like this 
} 

Répondre

0

Ajoutez l'ID d'affichage à votre bouton modal

<button type="button" data-id="{{ $posting->id }}" class="btn btn-outline" data-toggle="modal" href="postings/sendfriend" value="{{ $posting->short_desc }}" data-target="#sendfriend" style="background-color: #004AAE; color: #FFF; font-family: Lato; border-radius: 10px;">Send to a friend</button> 

dans votre formulaire modal créer une entrée cachée

<input type="hidden" name="posting"> 

Utilisez les js suivants pour passer l'ID d'affichage à CACHÉ déposée

$('#sendfriend').on('show.bs.modal', function (event) { 
    var button = $(event.relatedTarget) // Button that triggered the modal 
    var id = button.data('id') // Extract info from data-* attributes 
    var modal = $(this) 
    modal.find('.modal-body input[name="posting"]').val(id); 
}) 

Récupère l'ID dans le contrôleur

$friend->company = request('posting');