2017-10-19 5 views
0

페이지에 채용 정보 목록이 있습니다. 각 게시마다 '친구에게 보내기'버튼이 있습니다. 버튼을 클릭하면 친구의 이메일을 입력하고 양식을 제출할 수있는 양식이있는 모달이 표시됩니다.Laravel and Modals : 고유 한 @foreach 값을 모달 형식으로 전달 하시겠습니까?

관련 작업 게시 이름을 양식으로 가져 오려면 어떻게해야합니까? {{$ posting-> 이름}}

작업 게시물 페이지 :

@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') 

모달 :

<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> 

친구 컨트롤러 :

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

답변

0

: 필드에 숨겨진에 게시 ID를 전달하는 숨겨진 입력

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

사용하여 다음과 같은 JS를 만들려면 모달 폼에서 모달 버튼

<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> 

에 게시 ID를 추가

$('#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); 
}) 

컨트롤러의 ID 가져 오기

관련 문제