简体   繁体   中英

Select all result from database using laravel ,is not printing correct result

This is my SearchController code. If User selects All Events category then if statement is executed. Else its else statement.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Event;
use Symfony\Component\HttpFoundation\Response;

class SearchController extends Controller
{
    public function index(Request $request){
        $location = $request->location;
        $category = $request->category;
        //matching in the database
        if($category == "All Events")
        {
            //dd('its here');
            $events= Event::where('eventLocation', 'like', '%'.$location.'%')
                ->get(['eventLocation', 'eventName','eventCategory']);
        }
        else{
            $events = Event::where('eventCategory', 'like', $category)
                ->where('eventLocation', 'like', '%'.$location.'%')
                ->Where('eventName','like', '%'.$event.'%')
                ->get(['eventLocation', 'eventName','eventCategory']); 
              //its getting the 'eventLocation', 'eventName','eventCategory'
            //from the requeted event
        }



        return view('Events.SearchResult',compact(['events','event','location']));

    }
}

This is my blade template(Just a form).

<form action="{{url('search')}}" style="margin: 18% 19%;">
            <h2 style="text-align: center; color: #569;">Search For your Favourite Events</h2>

            <select name="category">
                <option>All Events </option>
                <option>Party </option>
                <option>Business </option>
                <option>Sports </option>
                <option>Food and Drinks </option>
                <option>Technology </option>
            </select>

         <!--    <input type="text" placeholder="Search for Events or Categories" name="event"> -->
            <input type="text" placeholder="Enter Desigred Location"  id="pac-input"  name="location">
            <input type="button" value="My Location" class="searchbtn">
            <input type="submit" value="SEARCH" class="searchbtn">
            <h2 style="text-align: center; margin-top: 100px; color: #fff;">OR Choose From Categories Below</h2>


        </form>

And this is my routes.php

<?php


Route::get('/u', 'EventController@upcomingEvent');
Route::get('search', 'SearchController@index');
Route::group(['middleware' => ['web']], function () {
    Route::get('register', [
        'as' => 'quantity',
        'uses'=> 'BookingController@getQuantity'
    ]);
    Route::post('checkout', 'BookingController@checkout');

    //Routes for the user controller
    Route::group(['prefix' => 'user/{id}/'], function () {
        Route::get('myorder', 'UserController@getOrders');
        Route::get('myevents', 'UserController@getEvents');
    });

      //Route for event registrations

    //Route::get('register/checkout','BookingController@getCheckout');
    //routes for the event
    Route::post('eventup','EventController@newEvent');
    Route::get('event/category/{category}','EventController@getEventsCategory');
    Route::resource('event', 'EventController');


    });

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/home', 'HomeController@index');
    Route::get('register/confirm/{token}', 'Auth\AuthController@confirmEmail');
    Route::get('/', function () {
        return view('Events.home');
    });
    Route::group(['prefix' => 'admin'], function () {
        Route::get('/', 'AdminController@index');
        Route::get('events', 'AdminController@myEvents');
        });


});


Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/home', 'HomeController@index');
    Route::get('eventup', function(){
        return view('eventup');
    });

    Route::get('afterlogin', function(){
        if(Auth::user()->hasRole('admin')){
            return redirect('admin/events');
        }
        else{
            return redirect('event');
        }
    });
});

This is screenshot of my database.There is keyword 'New Delhi' at 2 places in eventLocation Column ,But when I select 'All Events' and location as 'New Delhi' then single result(Bash Party) is shown. And if i select 'All Events' and location as 'Noida', then No output.Similar type of errors are repeating every time and I am unable to catch the errors. Any help would be appreciated.

Edit I forget to mention code for SearchResult.blade.php

 @if(count($events) != 0)
            @foreach($events as $event)
                <div class="col-md-3 col-sm-6 bottommargin">
                <div class="team">
                    <div class="team-image">
                        <img src="images/events/thumbs/1.jpg" alt="Event pic" style="height: 167px;">
                    </div>
                    <div class="team-desc team-desc-bg">
                        <div class="team-title"><h4><a href="{{url('event/'.$event->id)}}">{{$event->eventName}}</a></h4><p>{{$event->eventDescription}}</p><span>{{$event->eventLocation}}</span></div>
                        <a href="#" class="social-icon inline-block si-small si-light si-rounded si-facebook">
                            <i class="icon-facebook"></i>
                            <i class="icon-facebook"></i>
                        </a>
                        <a href="#" class="social-icon inline-block si-small si-light si-rounded si-twitter">
                            <i class="icon-twitter"></i>
                            <i class="icon-twitter"></i>
                        </a>
                        <a href="#" class="social-icon inline-block si-small si-light si-rounded si-gplus">
                            <i class="icon-gplus"></i>
                            <i class="icon-gplus"></i>
                        </a>
                    </div>
                </div>
            </div>
            @endforeach
        @else
                    No Result Found
        @endif

Your select field does not have any value attribute like

<select name="category">
      <option value="All Events">All Events </option>
      <option value="Party">Party </option>
      <option value="Business">Business </option>
      <option value="Sports">Sports </option>
      <option value="Food and Drinks">Food and Drinks </option>
      <option value="Technology">Technology </option>
 </select>

I don't see any event field in your form and write query like this

$events= Event::where('eventLocation', 'like', '%'.$location.'%')
         ->Where('eventName','like', '%'.$event.'%')
         ->select('eventLocation', 'eventName','eventCategory')
         ->get();

Also, check database field name and

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM