Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

farshadf's avatar

make a search form and show result on the same page

i want to make a search form that searches some dates and show the result to user on the same page that search submits which is this case is the show method of laravel which makes the job double hard i think so considering that i am on show method here is some part of my code

     public function show(Property $property)
    {

        $property = Property::with('propertycalendars')->where('id', $property->id)->first();
        foreach ($property->propertycalendars as $prop) {
            $end_reserve = $prop->reserve_end;
        }
//             HERE NEW RELATION
        $pdate = Property::with('dates')->get();

        return view('users.properties.show', compact('property','pdate','end_reserve'));
    }
//    search dates
     public function search (Request $request,Property $property){
    //Send an empty variable to the view, unless the if logic below changes, then it'll send a proper variable to the view.
    $results = null;

    //Runs only if the search has something in it.
    if (!empty($request->property_id)) {
        $start_date = $request->start_date;
        $search_date = Date::all()->where('date',$start_date);
    }
    $pdate = Property::with('dates')->get();
    return view('users.properties.show',compact('search_date','property','pdate'));
}

and here is my route :


Route::resource('/properties','PropertyController');
Route::post('/properties/search','PropertyController@search');

and finally the form that i use :


 <form action="/properties/search" method="post">
                {{csrf_field()}}

                <div class="row">
                <div class="col-lg-5">
                    <input type="hidden" value="{{$property->id}}" name="property_id">
                    <input name="start_date"  class="form-control m-input start_date" autocomplete="off"></div>

                <div class="col-lg-5">
                    <input name="title"  class="form-control m-input end_date" autocomplete="off"></div>

                <div class="col-lg-2">
                    <input type="submit" value="search" class="btn btn-primary btn-block" autocomplete="off">
                </div>
            </div>
            </form>

so with this code now i get the blank page with nothing inside but i want to show the old property.show page just + to search result . i would be happy to get any kind help if possible ty :)

0 likes
6 replies
burlresearch's avatar

Your PropertyController@search function is called via a POST request - so return JSON from it, not a view().

Then you'll be able to see the response from the form submission will contain the data that was matched. This is all done asynchronously in Javascript, so you'll have to have a JS function to parse the response, and push the results onto your page.

Snapey's avatar
Snapey
Best Answer
Level 122

Change your form to a GET action on the show page.

Not sure what you might search for in the show?

The property ID can be the same as part of the URL and the start and end date will be passed as querystring

Make sure you accept Request into the controller.

     public function show(Property $property, Request, $request)
    {

    if($request->has('start_date') {

        // performing a search against the injected $property

        // return view with results

    } else {

        // regular show

    }

Your $property is already loaded, so you don't need to do it twice

Change this

$property = Property::with('propertycalendars')->where('id', $property->id)->first();

to

$property->load('propertycalendars');

and save a query

What does this do:

        foreach ($property->propertycalendars as $prop) {
            $end_reserve = $prop->reserve_end;
        }

if its just to end up with the last value;

$end_reserve = $property->propertycalendars->last()->reserve_end;

what does this do;

$pdate = Property::with('dates')->get();

gets ALL properties with their dates? Will this be manageable as your application grows?

Might be an idea to install Laravel Debugbar then you can review all queries to see if they make sense.

1 like
farshadf's avatar

@snapey you mentioned good point because that was another question of mine well some parts you mentioned i just placed them to see the workflow and when it works i edit the queries but lets see some parts here now :

 $property->load('propertycalendars');
      foreach ($property->propertycalendars as $prop) {
           $end_reserve = $prop->reserve_end;
       }

it was for a previous method of my work that was my bad not to remove them i am sorry about that :) the important part is here :

$pdate = Property::with('dates')->get();

here is a short story of it i have property and date model and a many to many relation with a pivot table this dates is the relation name of it , so i wana get all the dates that this room(property) has !!! is that code wrong ? or not optimized ??

Snapey's avatar

so i wana get all the dates that this room(property) has !!! is that code wrong

No, you are getting ALL dates for ALL properties

If you want to get dates for this property you must use $property not Property, but I don't know if you have relationships?

1 like
farshadf's avatar

@SNAPEY - yes i have defined that here is the code :


 public function dates(){
        return $this->belongsToMany(Date::class);
    }

so u mean i can achive that with something like this ??

$property->dates ;

and it would give me all pivots related to that property ?? i got a bit confused

farshadf's avatar

@SNAPEY - oh Ok i tried and saw that just now thank you i always had problem with it to retrieve in front now its much easier thanks snapey

Please or to participate in this conversation.