Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Anything?

Last updated 2 years ago.
0

You need to use this for setting sql variable:

DB::statement(DB::raw('set @sum=0'));

Then goes your query (more or less raw).

Example usage:

public function scopeWithRowNumber($query, $column = 'created_at', $order = 'asc')
	{
		DB::statement(DB::raw('set @row=0'));

		$sub = static::selectRaw('*, @row:=@row+1 as row')
			->orderBy($column, $order)->toSql();
		$query->from(DB::raw("({$sub}) as sub"));
	}
Last updated 2 years ago.
0

thanks jarektkaczyk,

separating it to a standalone statement is what I needed

DB::statement(DB::raw('set @row=0'));
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.