Laravel. How to use request to perform APP search

Asked

Viewed 33 times

1

Good morning gentlemen I am starting my studies in Latin working with version 6 of the framework, and I came across problem that is probably a detail that I missed in my code.

When trying to perform a select in the database from a requeste I have a syntax error of mariadb.

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.`8 Accept-Encoding: gzip, deflate Accept-Language: pt-BR,' at line 1 (SQL: select * from `cities` where `GET /cities?id=1 HTTP/1`.`1 Accept: text/html

My method of controller:

public function cities(Request $request)
{
  $cities = \App\Cities::where($request)->get(); 
  return $cities;     
}

But if I let the I change to this result I get the result I need.

$cities = \App\Cities::where('id', '=', '1')->get(); 

I am sending following url to the Standard http://127.0.0.1:8000/Cities? id=1 and when I return $request I get json id "1" what I need to do with this request before sending the query?

I need Where because in most cases I will have more than a result in the search. Thanks in advance.

1 answer

1


Good morning Andrew, you are passing the entire Request object, you are also missing the first parameter of the Where clause that would be the comparison column in the database, in your 'ID' statement do it this way and it will work.

public function cities(Request $request)
{
  $cities = \App\Cities::where('id',$request->id)->get(); 
  return $cities;     
}

There are several ways to get data from Request follows two.

$request->get('chave');
$request->input('chave');
$request->chave;
  • Excuse my ignorance but did not fully understand, 'paramtro' is the column of the bank? and that complement after the request would be what? '->yourVariavel_get_ou_post' thanks in advance.

  • @Andrewpalmeira gave an edited answer, see if improved

  • Understood! thank you very much for the explanation, gave straight!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.