0

Has MySQL view 'histstat'.

In MySQL select * from histstat works fine.

Laravel model is very simple:

class HistStat extends Model
{
    use HasFactory;

    protected $table = 'histstat';

    protected $fillable = ['day', 'total', 'paid'];
}

Then I want to get first 14 records of hisstat:

$dynamic = HistStat::all()->slice(14);

... and execution ends with error SQLSTATE[42000]: Syntax error or access violation: 1055 (SQL: select * from 'histstat')

When I try to use table-based model ($dynamic = History::all()->slice(14);) - everything works fine.

So, the problem in MySQL view + Laravel.

How to use view-based model in Laravel ?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Valery Bulash
  • 445
  • 4
  • 22

2 Answers2

0

For the SQL error,

In config\database.php in "mysql" array

Set 'strict' => false

I think this one will help Laravel: Syntax error or access violation: 1055 Error.

For the first 14 records, you can do it

HistStat::oldest()->take(14)->get();

better than all() because I do not recommend using it if you have a huge table that will be load on the server because all() get all records select * from "users"

Zoe
  • 27,060
  • 21
  • 118
  • 148
Mohamed Gamal Eldin
  • 680
  • 1
  • 6
  • 18
0

In config/datatable.php (mysql section):

  1. Change strict to false
  2. Add / change in options - PDO::ATTR_EMULATE_PREPARES => true
Valery Bulash
  • 445
  • 4
  • 22