I've been working on a project and this is what i wanna perform in laravel.
<?php
require_once 'db.php';
$query = mysql_query("select distinct p.tag, p.tag2 from packages p order by 1") or die(mysql_error());
while($object = mysql_fetch_object($query)) {
$arr[] = $object;
}
echo $json_response = json_encode($arr);
?>
Since i'm new to Laravel i don't know how to do it. I want my results to be as following json format.
[{"tag":"Dubai","tag2":"U.A.E."},{"tag":"New York","tag2":"U.S.A."}]
I tried using Eloquent but failed. Any kind of help will be appreciated.
I think you are looking for the query builder
Your code in laravel will look similar to this:
$packages = DB::table('packages')->distinct()->select('tag', 'tag2')->get();
foreach ($packages as $package)
{
var_dump($package->tag);
}
return Response::json($packages);
No need to include db file in laravel you just have to set database connections in app/config/database.php
After that if you want to get data/ select data you can use http://laravel.com/docs/quick quick guide here is everything you can learn..
You could also try with Eloquent ORM in Laravel. The Code may be as following;
In Model
class package extends Eloquent {}
In Controller or Route
$packages = Package::distinct()->select('tag', 'tag2')->get()->toJson();
return $packages;