Reputation: 15
How do I implement this select query in laravel?
SELECT `kilometer` FROM `tyres` WHERE `usage`=1 ORDER BY id DESC LIMIT 1
Upvotes: 1
Views: 4070
Reputation: 1
$result = Tyre::select('kilometer')->where('usage', 1)->orderBy('id', 'DESC')->limit(1)->get();
Upvotes: 0
Reputation: 16
If you are using Tyres as model then Eloquent is the best bet.
Try to use App/Tyres
Tyres::select('kilometer')->where('usage', 1)->order_by('id', 'DESC')->limit('1)->get()
Upvotes: 0
Reputation: 3342
You are better off with Eloquent
To get kilometers from tyres, you need a tyre model if you dont have one already
use Illuminate\Database\Eloquent\Model;
class Tyre extends Model {
protected $table = 'tyres';
}
Tyre::select('kilometers')->where('usage', 1)->orderByDesc('id')->limit(1)->get();
Upvotes: 1
Reputation: 652
Your query will be
Tyres::select('kilometer')->where('usage',1)->orderBy('id', 'DESC')->take(1)->get();
Upvotes: 4
Reputation: 240
you can write like this:
$data = DB::select("SELECT kilometer FROM tyres WHERE usage=1 ORDER by id DESC LIMIT 1");
Upvotes: 1
Reputation: 1156
$data = DB::table('tyres')->select('kilometer')
->where('usage',1)->orderBy('id','desc')->first();
dd($data->kilometer);
Upvotes: 2