在本文中,我们将为您详细介绍如何使用LaravelEloquent创建多个Where子句查询?的相关知识,此外,我们还会提供一些关于Laravel5框架学习之Eloquent关系,laravelelo
在本文中,我们将为您详细介绍如何使用 Laravel Eloquent 创建多个 Where 子句查询?的相关知识,此外,我们还会提供一些关于Laravel 5框架学习之Eloquent 关系,laraveleloquent_PHP教程、Laravel 5框架学习之Eloquent (laravel 的ORM),laraveleloquent、Laravel 5框架学习之Eloquent (laravel 的ORM),laraveleloquent_PHP教程、Laravel Eloquent Query 使用 WHERE 和 OR AND OR?的有用信息。
本文目录一览:- 如何使用 Laravel Eloquent 创建多个 Where 子句查询?
- Laravel 5框架学习之Eloquent 关系,laraveleloquent_PHP教程
- Laravel 5框架学习之Eloquent (laravel 的ORM),laraveleloquent
- Laravel 5框架学习之Eloquent (laravel 的ORM),laraveleloquent_PHP教程
- Laravel Eloquent Query 使用 WHERE 和 OR AND OR?
如何使用 Laravel Eloquent 创建多个 Where 子句查询?
我正在使用 Laravel Eloquent 查询构建器,并且我有一个查询,我想要一个WHERE
关于多个条件的子句。它有效,但并不优雅。
例子:
$results = User::where(''this'', ''='', 1) ->where(''that'', ''='', 1) ->where(''this_too'', ''='', 1) ->where(''that_too'', ''='', 1) ->where(''this_as_well'', ''='', 1) ->where(''that_as_well'', ''='', 1) ->where(''this_one_too'', ''='', 1) ->where(''that_one_too'', ''='', 1) ->where(''this_one_as_well'', ''='', 1) ->where(''that_one_as_well'', ''='', 1) ->get();
有没有更好的方法来做到这一点,还是我应该坚持这种方法?
答案1
小编典典在 Laravel 5.3中(在7.x中仍然如此),您可以使用更细粒度的 wheres 作为数组传递:
$query->where([ [''column_1'', ''='', ''value_1''], [''column_2'', ''<>'', ''value_2''], [COLUMN, OPERATOR, VALUE], ...])
就我个人而言,我还没有通过多次调用找到这个用例where
,但事实是你可以使用它。
自 2014 年 6 月起,您可以将数组传递给where
只要你想要所有的wheres
useand
运算符,你可以这样分组:
$matchThese = [''field'' => ''value'', ''another_field'' => ''another_value'', ...];// if you need another group of wheres as an alternative:$orThose = [''yet_another_field'' => ''yet_another_value'', ...];
然后:
$results = User::where($matchThese)->get();// with another group$results = User::where($matchThese) ->orWhere($orThose) ->get();
以上将导致这样的查询:
SELECT * FROM users WHERE (field = value AND another_field = another_value AND ...) OR (yet_another_field = yet_another_value AND ...)
Laravel 5框架学习之Eloquent 关系,laraveleloquent_PHP教程
laravel 5框架学习之eloquent 关系,laraveleloquent
一个用户可能有多个文章,一个文章是某个用户书写的,这就是关系。同样文章中可能包含多个 tag,而一个 tag 可能关联多个文章。
在项目中,我们已经有了 User.php,也就是用户模型,查看一下,相当简单。我们希望直接使用 $user->articles() 的形式获取全部文章,让我们修改 user 模型:
public function articles() { return $this->hasMany(''App\Article''); }
但是我们仅仅完成了关系中的一端,让我们来处理另一端。我们希望的形式是 $article->user() ,让我们修改 article 模型。
public function user() { return $this->belongsTo(''App\User''); }
在我们的数据库中,文章模型没有用户的外键,我们需要设置,修改 create_article_table.php
Schema::create(''articles'', function(Blueprint $table) { $table->increments(''id''); //指定外键列 $table->integer(''user_id'')->unsigned(); $table->string(''title''); $table->text(''body''); $table->timestamp(''published_at''); $table->timestamps(); //生成外键 $table->foreign(''user_id'') ->references(''id'') ->on(''users'') ->onDelete(''cascade''); });
因为我们只是在开发阶段,还没有上线运行,我们可以直接修改数据库迁移文件,回滚然后迁移即可,但是如果上线运行的话,应该新建一个迁移。
立即学习“PHP免费学习笔记(深入)”;
php artisan migrate:refresh #输出信息 Rolled back: 2015_03_28_050138_create_article_table Rolled back: 2014_10_12_100000_create_password_resets_table Rolled back: 2014_10_12_000000_create_users_table Nothing to rollback. Migrated: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_100000_create_password_resets_table Migrated: 2015_03_28_050138_create_article_table Migrated: 2015_03_28_051200_add_excerpt_to_articels_table
现在让我们使用 tinker 来创建一个用户。
php artisan tinker Psy Shell v0.4.1 (PHP 5.4.16 — cli) by Justin Hileman #下面是执行过程 >>> $user = new App\User; => <App\User #000000007f1ad61a000000006497cc4f> {} >>> $user->name = ''zhang jinglin''; => "zhang jinglin" >>> $user->email = ''zjl@example.com''; => "zjl@example.com" >>> $user->password = bcrypt(''pass''); => "$2y$10$Nbl2b9wqd.rXqKEsd3pRSOoIyFAFIhbqf71BufwDfS3Guv21SlEx2" >>> $user->save(); => true >>> App\User::first()->toArray(); => [ "id" => "1", "name" => "zhang jinglin", "email" => "zjl@example.com", "created_at" => "2015-03-31 03:24:55", "updated_at" => "2015-03-31 03:24:55" ] >>>
现在我们需要新发布的文章和用户关联,我们暂时先修改 form_partial.blade.php 来隐藏一个用户id,只是暂时:
复制代码 代码如下:
{{--临时处理--}}
{!! Form::hidden(''user_id'', 1) !!}
同时要修改模型的 $fillabel 属性,以便我们的 Mass Assisment。
protected $fillable = [ ''title'', ''body'', ''published_at'', ''user_id'' //临时设置 ];
OK,添加一个文章。我们使用 tinker 来查看一下。
php artisan tinker Psy Shell v0.4.1 (PHP 5.4.16 — cli) by Justin Hileman >>> App\Article::first()->toArray(); => [ "id" => "1", "user_id" => "1", "title" => "User 1 Article", "body" => "User 1 Body", "published_at" => "2015-03-31 08:00:00", "created_at" => "2015-03-31 04:17:58", "updated_at" => "2015-03-31 04:17:58", "excerpt" => null ] #获取用户 >>> $user = App\User::first(); => <App\User #0000000051cbb9d70000000073e11a3e> { id: "1", name: "zhang jinglin", email: "zjl@example.com", created_at: "2015-03-31 03:24:55", updated_at: "2015-03-31 03:24:55" } #获取用户文章 >>> $user->articles()->toArray(); BadMethodCallException with message ''Call to undefined method Illuminate\Database\Query\Builder::toArray()'' >>> $user->articles->toArray(); => [ [ "id" => "1", "user_id" => "1", "title" => "User 1 Article", "body" => "User 1 Body", "published_at" => "2015-03-31 08:00:00", "created_at" => "2015-03-31 04:17:58", "updated_at" => "2015-03-31 04:17:58", "excerpt" => null ] ] #为什么使用 $user->articles 而不是 #user->articles()? #事实上,$user->articles()返回的是关系,如果你想用 articles() 你需要这样用 >>> $user->articles()->get()->toArray(); => [ [ "id" => "1", "user_id" => "1", "title" => "User 1 Article", "body" => "User 1 Body", "published_at" => "2015-03-31 08:00:00", "created_at" => "2015-03-31 04:17:58", "updated_at" => "2015-03-31 04:17:58", "excerpt" => null ] ] #你只能使用 articles() 来进行下一步的工作,比如下面的查询 $user->articles()->where(''title'', ''User 1 Article'')->get(); #我们也可以通过 article 获取 user >>> $article = App\Article::first(); => <App\Article #0000000051cbb9d60000000073e11a3e> { id: "1", user_id: "1", title: "User 1 Article", body: "User 1 Body", published_at: "2015-03-31 08:00:00", created_at: "2015-03-31 04:17:58", updated_at: "2015-03-31 04:17:58", excerpt: null } >>> $article->user; => <App\User #0000000051cbb92d0000000073e11a3e> { id: "1", name: "zhang jinglin", email: "zjl@example.com", created_at: "2015-03-31 03:24:55", updated_at: "2015-03-31 03:24:55" } >>>
以上所述就是本文的全部内容了,希望大家能够喜欢。
Laravel 5框架学习之Eloquent (laravel 的ORM),laraveleloquent
laravel 5框架学习之eloquent (laravel 的orm),laraveleloquent
我们来生成第一个模型
复制代码 代码如下:
php artisan make:model Article
#输出
Model created successfully.
Created Migration: 2015_03_28_062517_create_articles_table
查看一下生成的文件 app/Article.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Article extends Model { // }
没什么特别的,除了继承自 Model 以外,但是具有强大的功能,这些都封装在laravel的Model中。模型自动具有了 save() update() findXXX() 等强大的功能。
tinker 是 laravel提供的命令行工具,可以和项目进行交互。
php artisan tinker #以下是在tinker中的交互输入 Psy Shell v0.4.1 (PHP 5.4.16 — cli) by Justin Hileman >>> $name = ''zhang jinglin''; => "zhang jinglin" >>> $name => "zhang jinglin" >>> $article = new App\Article; => <App\Article #000000005c4b7ee400000000ab91a676> {} >>> $article->title = ''My First Article''; => "My First Article" >>> $article->body = ''Some content...''; => "Some content..." >>> $article->published_at = Carbon\Carbon::now(); => <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> { date: "2015-03-28 06:37:22", timezone_type: 3, timezone: "UTC" } >>> $article; => <App\Article #000000005c4b7ee400000000ab91a676> { title: "My First Article", body: "Some content...", published_at: <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> { date: "2015-03-28 06:37:22", timezone_type: 3, timezone: "UTC" } } >>> $article->toArray(); => [ "title" => "My First Article", "body" => "Some content...", "published_at" => <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> { date: "2015-03-28 06:37:22", timezone_type: 3, timezone: "UTC" } ] >>> $article->save(); => true #查看数据结果,添加了一条记录 >>> App\Article::all()->toArray(); => [ [ "id" => "1", "title" => "My First Article", "body" => "Some content...", "published_at" => "2015-03-28 06:37:22", "created_at" => "2015-03-28 06:38:53", "updated_at" => "2015-03-28 06:38:53" ] ] >>> $article->title = ''My First Update Title''; => "My First Update Title" >>> $article->save(); => true >>> App\Article::all()->toArray(); => [ [ "id" => "1", "title" => "My First Update Title", "body" => "Some content...", "published_at" => "2015-03-28 06:37:22", "created_at" => "2015-03-28 06:38:53", "updated_at" => "2015-03-28 06:42:03" ] ] >>> $article = App\Article::find(1); => <App\Article #000000005c4b7e1600000000ab91a676> { id: "1", title: "My First Update Title", body: "Some content...", published_at: "2015-03-28 06:37:22", created_at: "2015-03-28 06:38:53", updated_at: "2015-03-28 06:42:03" } >>> $article = App\Article::where(''body'', ''Some content...'')->get(); => <Illuminate\Database\Eloquent\Collection #000000005c4b7e1800000000ab91a676> [ <App\Article #000000005c4b7e1b00000000ab91a676> { id: "1", title: "My First Update Title", body: "Some content...", published_at: "2015-03-28 06:37:22", created_at: "2015-03-28 06:38:53", updated_at: "2015-03-28 06:42:03" } ] >>> $article = App\Article::where(''body'', ''Some content...'')->first(); => <App\Article #000000005c4b7e1900000000ab91a676> { id: "1", title: "My First Update Title", body: "Some content...", published_at: "2015-03-28 06:37:22", created_at: "2015-03-28 06:38:53", updated_at: "2015-03-28 06:42:03" } >>> >>> $article = App\Article::create([''title'' => ''New Article'', ''body'' => ''New body'', ''published_at'' => Carbon\Carbon::now()]); Illuminate\Database\Eloquent\MassAssignmentException with message ''title''
MassAssignmentException,laravel保护我们不能直接插入记录。比如,在一些特殊情况下我们需要直接利用表单的信息填充数据库记录,但是如果我们并没有在表单中添加密码字段,而黑客产生了密码字段连同我们的其他字段一起送回服务器,这将产生修改密码的危险,所以我们必须明确的告诉laravel我们的模型那些字段是可以直接填充的。
修改我们的模型文件 Article.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Article extends Model { protected $fillable = [ ''title'', ''body'', ''published_at'' ]; }
表示,title, body, published_at 是可以直接填充的。
退出 tinker,重新进入
>>> $article = App\Article::create([''title'' => ''New Article'', ''body'' => ''New body'', ''published_at'' => Carbon\Carbon::now()]); => <App\Article #000000005051b2c7000000007ec432dd> { title: "New Article", body: "New body", published_at: <Carbon\Carbon #000000005051b2c6000000007ec4081d> { date: "2015-03-28 06:55:19", timezone_type: 3, timezone: "UTC" }, updated_at: "2015-03-28 06:55:19", created_at: "2015-03-28 06:55:19", id: 2 } # It''s ok >>> App\Article::all()->toArray(); => [ [ "id" => "1", "title" => "My First Update Title", "body" => "Some content...", "published_at" => "2015-03-28 06:37:22", "created_at" => "2015-03-28 06:38:53", "updated_at" => "2015-03-28 06:42:03" ], [ "id" => "2", "title" => "New Article", "body" => "New body", "published_at" => "2015-03-28 06:55:19", "created_at" => "2015-03-28 06:55:19", "updated_at" => "2015-03-28 06:55:19" ] ] >>> $article = App\Article::find(2); => <App\Article #000000005051b22b000000007ec432dd> { id: "2", title: "New Article", body: "New body", published_at: "2015-03-28 06:55:19", created_at: "2015-03-28 06:55:19", updated_at: "2015-03-28 06:55:19" } >>> $article->update([''body'' => ''New Updaet Body'']); => true #update自动调用save()
以上所述就是本文的全部内容了,希望能够对大家学习Laravel5框架有所帮助。
Laravel 5框架学习之Eloquent (laravel 的ORM),laraveleloquent_PHP教程
laravel 5框架学习之eloquent (laravel 的orm),laraveleloquent
我们来生成第一个模型
复制代码 代码如下:
php artisan make:model Article
#输出
Model created successfully.
Created Migration: 2015_03_28_062517_create_articles_table
查看一下生成的文件 app/Article.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Article extends Model { // }
没什么特别的,除了继承自 Model 以外,但是具有强大的功能,这些都封装在laravel的Model中。模型自动具有了 save() update() findXXX() 等强大的功能。
tinker 是 laravel提供的命令行工具,可以和项目进行交互。
php artisan tinker #以下是在tinker中的交互输入 Psy Shell v0.4.1 (PHP 5.4.16 — cli) by Justin Hileman >>> $name = ''zhang jinglin''; => "zhang jinglin" >>> $name => "zhang jinglin" >>> $article = new App\Article; => <App\Article #000000005c4b7ee400000000ab91a676> {} >>> $article->title = ''My First Article''; => "My First Article" >>> $article->body = ''Some content...''; => "Some content..." >>> $article->published_at = Carbon\Carbon::now(); => <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> { date: "2015-03-28 06:37:22", timezone_type: 3, timezone: "UTC" } >>> $article; => <App\Article #000000005c4b7ee400000000ab91a676> { title: "My First Article", body: "Some content...", published_at: <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> { date: "2015-03-28 06:37:22", timezone_type: 3, timezone: "UTC" } } >>> $article->toArray(); => [ "title" => "My First Article", "body" => "Some content...", "published_at" => <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> { date: "2015-03-28 06:37:22", timezone_type: 3, timezone: "UTC" } ] >>> $article->save(); => true #查看数据结果,添加了一条记录 >>> App\Article::all()->toArray(); => [ [ "id" => "1", "title" => "My First Article", "body" => "Some content...", "published_at" => "2015-03-28 06:37:22", "created_at" => "2015-03-28 06:38:53", "updated_at" => "2015-03-28 06:38:53" ] ] >>> $article->title = ''My First Update Title''; => "My First Update Title" >>> $article->save(); => true >>> App\Article::all()->toArray(); => [ [ "id" => "1", "title" => "My First Update Title", "body" => "Some content...", "published_at" => "2015-03-28 06:37:22", "created_at" => "2015-03-28 06:38:53", "updated_at" => "2015-03-28 06:42:03" ] ] >>> $article = App\Article::find(1); => <App\Article #000000005c4b7e1600000000ab91a676> { id: "1", title: "My First Update Title", body: "Some content...", published_at: "2015-03-28 06:37:22", created_at: "2015-03-28 06:38:53", updated_at: "2015-03-28 06:42:03" } >>> $article = App\Article::where(''body'', ''Some content...'')->get(); => <Illuminate\Database\Eloquent\Collection #000000005c4b7e1800000000ab91a676> [ <App\Article #000000005c4b7e1b00000000ab91a676> { id: "1", title: "My First Update Title", body: "Some content...", published_at: "2015-03-28 06:37:22", created_at: "2015-03-28 06:38:53", updated_at: "2015-03-28 06:42:03" } ] >>> $article = App\Article::where(''body'', ''Some content...'')->first(); => <App\Article #000000005c4b7e1900000000ab91a676> { id: "1", title: "My First Update Title", body: "Some content...", published_at: "2015-03-28 06:37:22", created_at: "2015-03-28 06:38:53", updated_at: "2015-03-28 06:42:03" } >>> >>> $article = App\Article::create([''title'' => ''New Article'', ''body'' => ''New body'', ''published_at'' => Carbon\Carbon::now()]); Illuminate\Database\Eloquent\MassAssignmentException with message ''title''
MassAssignmentException,laravel保护我们不能直接插入记录。比如,在一些特殊情况下我们需要直接利用表单的信息填充数据库记录,但是如果我们并没有在表单中添加密码字段,而黑客产生了密码字段连同我们的其他字段一起送回服务器,这将产生修改密码的危险,所以我们必须明确的告诉laravel我们的模型那些字段是可以直接填充的。
修改我们的模型文件 Article.php
立即学习“PHP免费学习笔记(深入)”;
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Article extends Model { protected $fillable = [ ''title'', ''body'', ''published_at'' ]; }
表示,title, body, published_at 是可以直接填充的。
退出 tinker,重新进入
>>> $article = App\Article::create([''title'' => ''New Article'', ''body'' => ''New body'', ''published_at'' => Carbon\Carbon::now()]); => <App\Article #000000005051b2c7000000007ec432dd> { title: "New Article", body: "New body", published_at: <Carbon\Carbon #000000005051b2c6000000007ec4081d> { date: "2015-03-28 06:55:19", timezone_type: 3, timezone: "UTC" }, updated_at: "2015-03-28 06:55:19", created_at: "2015-03-28 06:55:19", id: 2 } # It''s ok >>> App\Article::all()->toArray(); => [ [ "id" => "1", "title" => "My First Update Title", "body" => "Some content...", "published_at" => "2015-03-28 06:37:22", "created_at" => "2015-03-28 06:38:53", "updated_at" => "2015-03-28 06:42:03" ], [ "id" => "2", "title" => "New Article", "body" => "New body", "published_at" => "2015-03-28 06:55:19", "created_at" => "2015-03-28 06:55:19", "updated_at" => "2015-03-28 06:55:19" ] ] >>> $article = App\Article::find(2); => <App\Article #000000005051b22b000000007ec432dd> { id: "2", title: "New Article", body: "New body", published_at: "2015-03-28 06:55:19", created_at: "2015-03-28 06:55:19", updated_at: "2015-03-28 06:55:19" } >>> $article->update([''body'' => ''New Updaet Body'']); => true #update自动调用save()
以上所述就是本文的全部内容了,希望能够对大家学习Laravel5框架有所帮助。
Laravel Eloquent Query 使用 WHERE 和 OR AND OR?
我怎么说WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1)
对于更复杂的查询,我应该使用原始 SQL 吗?
答案1
小编典典利用逻辑分组(Laravel
7.x /
4.2)。对于您的示例,它将是这样的:
Model::where(function ($query) { $query->where(''a'', ''='', 1) ->orWhere(''b'', ''='', 1);})->where(function ($query) { $query->where(''c'', ''='', 1) ->orWhere(''d'', ''='', 1);});
今天的关于如何使用 Laravel Eloquent 创建多个 Where 子句查询?的分享已经结束,谢谢您的关注,如果想了解更多关于Laravel 5框架学习之Eloquent 关系,laraveleloquent_PHP教程、Laravel 5框架学习之Eloquent (laravel 的ORM),laraveleloquent、Laravel 5框架学习之Eloquent (laravel 的ORM),laraveleloquent_PHP教程、Laravel Eloquent Query 使用 WHERE 和 OR AND OR?的相关知识,请在本站进行查询。
本文标签: