Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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 在Laravel中连接数据库的最佳方法_Php_Mysql_Laravel - Fatal编程技术网

Php 在Laravel中连接数据库的最佳方法

Php 在Laravel中连接数据库的最佳方法,php,mysql,laravel,Php,Mysql,Laravel,据我所知,在Laravel中有两种连接数据库的方法 方法1: // the name of table is defined in the model file Model_Name::WHERE(1)->get(); DB::table('table_name')->WHERE(1)->get(); 方法2: // the name of table is defined in the model file Model_Name::WHERE(1)->get();

据我所知,在Laravel中有两种连接数据库的方法

方法1:

// the name of table is defined in the model file
Model_Name::WHERE(1)->get();
DB::table('table_name')->WHERE(1)->get();
方法2:

// the name of table is defined in the model file
Model_Name::WHERE(1)->get();
DB::table('table_name')->WHERE(1)->get();
好的,两种方法都是一样的,结果也一样。那有什么不同呢?在哪种情况下哪个更好


无论如何,我什么时候应该使用哪一个呢?

Laravel提供了一个名为Elounce的内置ORM,它使我们与数据库的交互变得简单。雄辩的ORM提供了活动记录实现,这意味着在MVC结构中创建的每个模型都对应于数据库中的一个表。这里,“Post”模型将对应于“posts”表。因此,您可以访问posts表中的数据,如下所示

Post::all()          // Get all the posts 
Post::find($id)      // Find a post
Post::delete($id)    // Delete a post
Fluent查询生成器

顾名思义,它提供了创建和运行数据库查询的流畅接口。Laravel查询生成器始终使用PDO参数绑定来保护应用程序免受SQL注入攻击。不需要清除作为绑定传递的字符串。其中一些例子是

DB::table('posts')->get();                       // Get all the posts
DB::table('posts')->where('id',$id)->first();    // Find a post
DB::table('posts')->where('id',$id)->delete();   // Delete a post
能言善辩的优点

You can use all Fluent functions in eloquent. But, You can’t use eloquent functions in the query builder
Eloquent model relationship
Easy to use
Code Readability
Execution time may increase little bit
In some places, Eloquent fail to compete against SQL queries for complex queries
能言善辩的缺点

You can use all Fluent functions in eloquent. But, You can’t use eloquent functions in the query builder
Eloquent model relationship
Easy to use
Code Readability
Execution time may increase little bit
In some places, Eloquent fail to compete against SQL queries for complex queries
结论

因为,有口才和流利的查询生成器都有各自的优缺点。您应该根据需求使用这两种方法。正如许多事情一样,80:20规则似乎适用于这里。使用eloquent为您完成80%的工作,并准备使用fluent为其他20%的工作编写SQL和一些持久性代码。不要对eloquent期望过高,否则最终会出现一些奇怪的bug和性能问题


Laravel提供了一个名为Eloquent的内置ORM,使我们与数据库的交互变得简单。雄辩的ORM提供了活动记录实现,这意味着在MVC结构中创建的每个模型都对应于数据库中的一个表。这里,“Post”模型将对应于“posts”表。因此,您可以访问posts表中的数据,如下所示

Post::all()          // Get all the posts 
Post::find($id)      // Find a post
Post::delete($id)    // Delete a post
Fluent查询生成器

顾名思义,它提供了创建和运行数据库查询的流畅接口。Laravel查询生成器始终使用PDO参数绑定来保护应用程序免受SQL注入攻击。不需要清除作为绑定传递的字符串。其中一些例子是

DB::table('posts')->get();                       // Get all the posts
DB::table('posts')->where('id',$id)->first();    // Find a post
DB::table('posts')->where('id',$id)->delete();   // Delete a post
能言善辩的优点

You can use all Fluent functions in eloquent. But, You can’t use eloquent functions in the query builder
Eloquent model relationship
Easy to use
Code Readability
Execution time may increase little bit
In some places, Eloquent fail to compete against SQL queries for complex queries
能言善辩的缺点

You can use all Fluent functions in eloquent. But, You can’t use eloquent functions in the query builder
Eloquent model relationship
Easy to use
Code Readability
Execution time may increase little bit
In some places, Eloquent fail to compete against SQL queries for complex queries
结论

因为,有口才和流利的查询生成器都有各自的优缺点。您应该根据需求使用这两种方法。正如许多事情一样,80:20规则似乎适用于这里。使用eloquent为您完成80%的工作,并准备使用fluent为其他20%的工作编写SQL和一些持久性代码。不要对eloquent期望过高,否则最终会出现一些奇怪的bug和性能问题


如果性能不是问题,我更喜欢第一种方法,因为:

// the name of table is defined in the model file
Model_Name::WHERE(1)->get();
DB::table('table_name')->WHERE(1)->get();
假设您在代码的100个位置使用了第二种方法。现在,由于某些特定原因,您需要更改表名。你会怎么做?您需要在使用特定
表名的100个位置进行更改

DB::table('table_name')->where(1)->get(); //change table_name in 100 palces
如果使用第一种方法,您可能已经编写了

Model_Name::where(1)->get(); //no change here
现在,您需要更改表名,只需在模型中更改/添加表属性。大概是这样的:

class Model_Name{
    protected $table = 'new_table_name'; //change table_name in one place
}
别无他事可做。您的表名非常灵活


注意:第一种方法是雄辩的ORM方法,它具有许多
ORM
应该具备的功能。另一方面,fluent查询的性能更好。你想用什么就用什么。但是我认为避免在单个项目中混淆这两种方法是一种很好的做法。如果性能不是问题,我更喜欢第一种方法,因为:

// the name of table is defined in the model file
Model_Name::WHERE(1)->get();
DB::table('table_name')->WHERE(1)->get();
假设您在代码的100个位置使用了第二种方法。现在,由于某些特定原因,您需要更改表名。你会怎么做?您需要在使用特定
表名的100个位置进行更改

DB::table('table_name')->where(1)->get(); //change table_name in 100 palces
如果使用第一种方法,您可能已经编写了

Model_Name::where(1)->get(); //no change here
现在,您需要更改表名,只需在模型中更改/添加表属性。大概是这样的:

class Model_Name{
    protected $table = 'new_table_name'; //change table_name in one place
}
别无他事可做。您的表名非常灵活


注意:第一种方法是雄辩的ORM方法,它具有许多
ORM
应该具备的功能。另一方面,fluent查询的性能更好。你想用什么就用什么。但是,我认为,避免在单个项目中混淆这两种方法是一种很好的做法。

这两种方法都可以获得相同的结果,但在使用时,可以很好地处理代码/工作关系,这样可以保持代码的可读性,同时有助于运行复杂的SQL查询

Laravel提供了一个名为的内置功能。ORM Eloquent使每个模型的代码都能轻松地与数据库交互。只需在MVC结构中创建一个对应于DB表的表。使用也非常简单,如:

Model_name::DB_interaction_functions
query builder提供了预构建的fluent界面,可通过以下简单方式创建和运行数据库查询:

DB::table('table_name')->DB_interaction_functions
这是一件简单的事情,但若我们谈论雄辩和流利的表现,流利是最好的,然后雄辩。我不能测试它,但你可以应用它

夏季
在代码可读性方面,Elount是一种简单的使用方法,但执行查询只需要很少的时间。而Fluent在执行复杂的SQL查询时表现良好


Ref:

这两种方法的工作结果相同,但在您使用时,将允许代码/工作关系非常好,因此您可以保持代码可读性,同时有助于运行复杂的SQL查询

Laravel提供了一个名为的内置功能。奥默尔