Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在laravel4中,我们需要在哪个文件夹中添加数据库查询?_Php_Laravel_Laravel 4 - Fatal编程技术网

Php 在laravel4中,我们需要在哪个文件夹中添加数据库查询?

Php 在laravel4中,我们需要在哪个文件夹中添加数据库查询?,php,laravel,laravel-4,Php,Laravel,Laravel 4,我是laravel的初学者,已经安装了laravel4,工作起来很酷。甚至我也做过数据库配置。如果我想获取、插入或执行一些需要编写数据库查询的数据库操作?我在router.php中编写了一个简单的代码,如下所示,并从数据库中获取所有值。但我需要知道我们到底需要在哪里编写这个代码片段?我的任务是编写一个RESTAPI。有人能帮我吗 $users = DB::table('user')->get(); return $users; 这取决于你如何设计你的路线。如果你这样走 Route::

我是laravel的初学者,已经安装了laravel4,工作起来很酷。甚至我也做过数据库配置。如果我想获取、插入或执行一些需要编写数据库查询的数据库操作?我在router.php中编写了一个简单的代码,如下所示,并从数据库中获取所有值。但我需要知道我们到底需要在哪里编写这个代码片段?我的任务是编写一个RESTAPI。有人能帮我吗

 $users = DB::table('user')->get();
 return $users;

这取决于你如何设计你的路线。如果你这样走

Route::get('/', array('as' => 'home', function () {

 }));
$users = User::all();
return $users;
然后,您可以在路由页面中执行如下查询

Route::get('/', array('as' => 'home', function () {
   $users = DB::table('user')->get();
    return $users;
 }));
但是如果你在你的路由中调用一个控制器

Route::get('/', array('as' => 'home', 'uses' => 'HomeController@showHome'));
然后您可以在
HomeController
controller中的
showHome
方法中进行查询 像

注意:控制器目录为
app/controllers

更新

如果您想使用
Model
,则需要在
App/models
文件夹中创建模型,如

class User extends Eloquent {
    protected $table = 'user';
    public $timestamps = true; //if true then you need to keep two field in your table named `created_at` and `updated_at`
}
那么查询将是这样的

Route::get('/', array('as' => 'home', function () {

 }));
$users = User::all();
return $users;

不需要使用模型文件夹?我认为我们需要在该文件夹中编写所有数据库查询。不需要使用model文件夹进行流畅的查询。在model文件夹中,您只需为每个表创建模型。您可以使用model,但不能使用mandatory非常感谢。您是否可以告诉我如何向查询发送参数?例如,如果我想在rest API中发送用户名和密码,如何检查该用户名和密码的存在性?您可以使用where条件,如
User::where('username','=','testuser')->where('password','=','testpass')->get()如果使用模型,请阅读文档