user2227682
user2227682

Reputation: 73

Laravel controller with modal bootstrap

I use Laravel Framework 5.4 and i need pass parameters from one button that open one Modal that receiving this parameters in myController and return data to myModal, somebody help me?

page1.blade.php

This button open my searchmodal.blade.php with jquery, it's Right my href to call myController?

<a href="{{url('myController@search')}}" class="btn btn-success btnsearch">Search</a> 

jquery

    $('.btnsearch').click(function(e){
               $("#searchmodal").modal('show');
     })

myController need this parameters and return variables to get in Modal.

myController.php

public function search(parameter1){

  $customers = Customer::find(parameter1);

  //return my modal or page1?
   return view('searchmodal', compact('customers'));
}

mymodal.blade.php, how do i get the customers variable from myController?

<div class="modal fade searchmodal" id="searchmodal"  tabindex="-1" aria-labelledby="searchmodal" role="dialog">
....
</div>

Upvotes: 1

Views: 12379

Answers (3)

Sayantan Das
Sayantan Das

Reputation: 1641

This is what I understood from your question: You have a button that opens up a modal with a search form and you need to pass the parameters to your controller which would return the results.

<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Search</h4>
      </div>
      <div class="modal-body">
          <form method="GET" action={{ route('your.search.route') }}>
              <input type="text" name="search" class="form-control" />
              <input type="submit" class="btn btn-success" value="search" />
          </form>
      </div>
    </div>

  </div>
</div>

Then you need to perform the search in your controller

public function search(Request $request){
  // request object holds all the data you submitted through form
  $keyword = $request->search;

  // you can implement your search logic according to your need
  $customers = Customer::where('field_name', 'LIKE', $keyword . '%')->get();

  return view('searchmodal', ['customers' => $customers]);
}

In your view you can access the customer data

@foreach ($customers as $customer)
    <p>This is customer {{ $customer->id }}</p>
@endforeach

You might have to change it as per your need but you should get the idea. Hope it helps.

Upvotes: 1

Seth Kwakwa
Seth Kwakwa

Reputation: 44

Check the Controller

public function search(parameter1){
  $customers = Customer::find(parameter1);
  //return my modal or page1?
   return view('searchmodal', array('customers'=>$customers));
}

In the views you can access the $customers variable

Upvotes: 0

CarlosCarucce
CarlosCarucce

Reputation: 3569

If you 're using blade templates, you can use @foreach loop

Example:

@foreach ($customers as $customer)
    <p>This is customer {{ $customer->id }}</p>
@endforeach

Upvotes: 1

Related Questions