使用laravel从数据库中选择所有结果,不打印正确的结果

时间:2016-04-06 03:52:51

标签: php laravel

这是我的SearchController代码。如果用户选择所有事件类别,则执行if语句。还有其他声明。

<?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']));

    }
}

这是我的刀片模板(只是表格)。

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

这是我的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是我的数据库的屏幕截图。有关键字&#39;新德里&#39;在eventLocation列中的2个位置,但是当我选择“所有事件”时和新德里的位置&#39;然后显示单个结果(Bash Party)。如果我选择所有活动&#39;并且位置为Noida&#39;,然后没有输出。每次都会重复相似类型的错误,我无法捕获错误。 任何帮助将不胜感激。

编辑我忘记提及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

1 个答案:

答案 0 :(得分:0)

您的选择字段没有任何值属性,如

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

我没有在您的表单中看到任何事件字段并像这样写

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

另外,检查数据库字段名称和