how to use paginate with raw queries in Laravel 5.3? - laravel-5

How to use paginate with raw queries in Laravel 5.3 ?
I have an error using this:
$sql = "SELECT * FROM table";
$search=DB::select($sql)-paginate(10);
return view('index', compact('search'));

Related

Laravel Eloquent Query Where IN statement

How do I convert this MySQL query to Laravel Eloquent Query:
SELECT * FROM service_package WHERE price IN ('110010', 'Test 02', '11009')
You can use the whereIn() like:
Using DB::table()
$data = \DB::table("service_package")
->whereIn("price", ['110010', 'Test 02', '11009'])
->get();
Using Eloquent
$data = App\ServicePackage::whereIn("price", ['110010', 'Test 02', '11009'])
->get();
Here, make sure you have created model ServicePackage for the table service_package.

How to retrive data from sql table with where condition using get() in laravel 5.8

how to retrieve multiple rows from table with where condition using laravel 5.8
I tried with joins but i am not getting data. below provided code is not working in my instance.
I am able to get first row but i am looking for multiple rows.
public function show($id)
{
$event = Event::find($id);
$speaker = Speaker::where('event_id', '=', $id)->get();
return view('events-show')->with('event', $event)->with('speaker', $speaker);
}
I want to retrieve two tables data using laravel 5.8 and display in view page.
try this
$speaker=Speaker::join('events','events.id','speakers.event_id')->where('events.id', '=', $id)->get();
return view('events-show')->with('speaker', $speaker);

How can i write nested query in laravel

SELECT * FROM posts WHERE user_id IN (SELECT following FROM follows WHERE user_id='1'
This is the query i want to wirte in laravel to fetch the data how can i write this in laravel
This should work:
$result = DB::table('posts')
->whereIn('user_id', function ($query) {
$query->select('following')
->from('follows')
->where('user_id', 1);
})->get();

How to write sub query in Laravel eloquent

Following is my MySQL query
SELECT id
FROM member_measurement_records
WHERE date in (SELECT MAX(date) from member_measurement_records GROUP BY type)
Which I want to write using laravel eloquent.
Can you please help me. As I am new to this.
Well, there are several ways to do it.
Using DB facade
$data = DB::table('member_measurement_records')->where('date', DB::raw("(select max(`date`) from member_measurement_records GROUP BY type)"))->get();
Using eloquent and raw query
$data = MemberMeasurementRecords::whereRaw('date = (select max(`date`) from member_measurement_records GROUP BY type)')->get();
Using Eloquent and DB facade
$data = MemberMeasurementRecords::find(DB::table('member_measurement_records')->max('date')->groupBy('type'));
Haven't tested it, but you can try it:
(It is assumend your corresponding model is called MemberMeasurementRecords)
MemberMeasurementRecords::whereIn('date', function ($query) {
$query->max('date')->orderBy('type');
})->get('id');

Laravel 5.4 whereDate in Eloquent

How to use whereMonth, whereDate in Eloquent ORM? (I know it can in DB query). Im using 5.4
You can use all the methods from DB query in Eloquent models, Check the following samples you can easily capture it. Both the samples, produce the same result
Using query builder
$users = DB::table('users')
->whereDate('created_at', '2016-12-31')
->get();
Using model
$users = Users::whereDate('created_at', '2016-12-31')
->get();

Resources