1

for some reason Laravel gives me an SQL error when I try to run the following code. The error i get goes as follows:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from `homes` where `Company` = ?' at line 1 (SQL: select from `homes` where `Company` = Abbeyfield)

Here's the code:

<?php

class AnalysisController extends BaseController {

public function getSelect(){

    //current USER
    $current_user = Auth::user();

    echo $relevant_homes = Home::where('Company', '=', $current_user->company_name)->get(array());

    /*
    return View::make('analysis.select')->with('current_user', $current_user);
    */
}
}
?>

Any ideas?

5
  • select from homes where Company` = Abbeyfield` where is your select column names in your query ?
    – Krish R
    Commented Apr 15, 2014 at 16:20
  • @KrishR Company is the column name in this query. It is Eloquent ORM Commented Apr 15, 2014 at 16:22
  • @decker, I didnt find any column name or * infront of FROM in select Query~
    – Krish R
    Commented Apr 15, 2014 at 16:24
  • @decker Yep that's correct. I'm looking at the 'company_name' of the user currently logged-in, and comparing it to a table containing a set of homes, where I want to retrieve those homes owned by that company (by looking at the 'Company column' )
    – Max F
    Commented Apr 15, 2014 at 16:27
  • @KrishR Actually, I think you are correct. He is passing to get the array of column names he wants to get. It is an empty array. Commented Apr 15, 2014 at 16:31

3 Answers 3

1

Remove the blank array from get(). Your code has generated following sql:

select from `homes` where `Company` = Abbeyfield

Mising * or any fields because you have passed a blank array. It should be:

$relevant_homes = Home::where('Company', '=', $current_user->company_name)->get();

Or pass some field names in the array to select, for example:

// Assumed that, id and Company as available fields in the table
$relevant_homes = Home::where('Company', '=', $current_user->company_name)->get(array('id', 'Company'));
4
  • Ok this worked, however When I uncomment the view::make(analyis.select) and redirect to it, the $relevant_home->Home_name variable is "not defined"
    – Max F
    Commented Apr 15, 2014 at 16:31
  • If you redirect then you have to flash the data to session to use it from next request, you may do it using with('data', $data). That's another issue and I think you should post another question with relevant (non-working) code (if you still need more information).
    – The Alpha
    Commented Apr 15, 2014 at 16:33
  • Yes sorry just realized that, had withdrawn that bit previously to isolate problem! Thanks so much!
    – Max F
    Commented Apr 15, 2014 at 16:34
  • 1
    +1, @MaxF, If you need more information about the redirect problem, you may post another question with code example.
    – The Alpha
    Commented Apr 15, 2014 at 16:36
0

Your query should be something like this:

$relevant_homes = Home::where('Company', '=', $current_user->company_name)->get()->toArray();
0
'from `homes` where `Company` = ?'

There are no columns being selected. get takes an array of column names you want to get.

You are passing an empty array, so are not getting anything.

I think you should have this:

$relevant_homes = Home::where('Company', '=', $current_user->company_name)->get();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.