I am trying to get distinct city names from a MySQL table called "city".
Here is how the controller code looks like:
public function getCityNames()
{
if(Auth::check())
{
$cities = DB::table('city')->distinct()->get();
var_dump($cities);
}
else
{
return Redirect::route('account-signin');
}
}
Here is the code for the model City:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class City extends Eloquent
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'city';
/**
* Using a different primary key name
*
*/
protected $primaryKey = 'geoname_id';
public $timestamps = false;
}
The Problem
I can output distinct values from other models using the exact code as in this controller above but when I run it for the city, I get a BLANK page.
UPDATE: When I write the following, I get data:
public function Test()
{
return City::where('country_name', '=', 'Canada')->get();
}
But when I write the following, I get the black page? Something with the data size?
public function Test()
{
return City::all()->get();
}
Not sure if this is problem, but are you trying to view the output of var_dump($cities)
?
If so, shouldn't return var_dump($cities);
give you the output, rather than the empty page?
Code looks like this:
public function getCityNames()
{
if(Auth::check()) // Tip: You can apply auth filters to controllers if you want.
{
$cities = DB::table('city')->distinct()->get();
return var_dump($cities); // Add return here.
}
else
{
return Redirect::route('account-signin');
}
// If the code doesn't go anywhere, it goes here.
return "TEST";
}
I am able to access the city table fine when I ran small queries (see my update in the question). Only explanation could be that I was exceeding the browser buffer size when I was trying to get all . This table has more than 70,000 rows.
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.