Laravel - Invalid parameter number: parameter was not defined

后端 未结 3 1531
轻奢々
轻奢々 2021-01-11 17:38

On Laravel, why do I get an error

Invalid parameter number: parameter was not defined

I have included all parameters.

W

相关标签:
3条回答
  • 2021-01-11 17:54

    You're inserting all of your SQL inside of laravel's query builder select() method. You just need to utilize its other methods:

    $select = [
        'client_id',
        'start_date',
        'end_date',
        'first_name',
        'last_name',
        'phone',
        'postcode'
    ];
    
    $results = \DB::table('hire')
            ->join('client', 'client.id', '=', 'hire.client_id')
            ->select($select)
            ->where('car_id', $car_id)
            ->whereBetween('start_date', [$start_date, $end_date])
            ->orWhereBetween('end_date', [$start_date, $end_date])
            ->get();
    

    These aren't all of your parameters, but it should get you started.

    If you're not looking to use the query builder, try performing a raw($expression):

    $results = \DB::raw('SELECT client_id,
                               date_format(start_date,"%d/%m/%Y") as start_date,
                               date_format(end_date,"%d/%m/%Y") as end_date,
                               first_name, last_name, phone, postcode
                            FROM hire INNER JOIN client ON client.id = hire.client_id
                            where ((:sdate between start_date and end_date OR :edate between start_date and end_date) OR (:sdate <= start_date and end_date <= :edate)) AND car_id = :car_id', [
            'sdate'  => $start_date,
            'edate'  => $end_date,
            'car_id' => $car_id
        ]
    );
    
    0 讨论(0)
  • 2021-01-11 17:56

    Your query is failing because you are reusing parameters in your query. Laravel uses PDO for its SQL queries. According to the docs:

    You cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.

    So even though they have the same value, you will have to rename those parameters.

    $results = \DB::select('SELECT client_id,
                               date_format(start_date,"%d/%m/%Y") as start_date,
                               date_format(end_date,"%d/%m/%Y") as end_date,
                               first_name, last_name, phone, postcode
                            FROM hire INNER JOIN client ON client.id = hire.client_id
                            where ((:sdate between start_date and end_date OR :edate between start_date and end_date) OR (:sdate2 <= start_date and end_date <= :edate2)) AND car_id = :car_id', [
        'sdate'  => $start_date,
        'sdate2'  => $start_date,
        'edate'  => $end_date,
        'edate2'  => $end_date,
        'car_id' => $car_id
    ]
    );
    
    0 讨论(0)
  • 2021-01-11 17:56

    app/config/database.php

    add this in mysql array

    'options'=> extension_loaded('pdo_mysql') ? array_filter([
                    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                    PDO::ATTR_EMULATE_PREPARES => true,
                ]) : [],
    
    0 讨论(0)
提交回复
热议问题