ThinkPhp6框架三元运算符使用方法

三元运算符有两种判断方式:

                                一种是一个值的判断

                                一种的两个值的进行判断

{$res?'成功':'失败'}
{$res==$state?'成功':'失败'}

这两种的判断方式都是一样的,判断成功就执行左边(成功),判断失败就执行右边(失败)

                        记得一定要加上大括号  {  }    如果不写大括号没办法取到值

ThinkPHP6 中使用 ElasticSearch 需要先安装 ElasticSearch 和 Elasticsearch for PHP,具体安装步骤请参考前面的回答。 安装完成后,我们可以在代码中使用 Elasticsearch for PHP 提供的 API 进行数据的增删改查操作。下面是一个简单的示例: ```php <?php namespace app\controller; use Elasticsearch\ClientBuilder; use think\facade\Db; class Index { public function search() { $client = ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'body' => [ 'query' => [ 'match' => [ 'title' => 'ElasticSearch' ] ] ] ]; $response = $client->search($params); return json($response); } public function add() { $data = Db::table('my_table')->find(); $client = ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => $data['id'], 'body' => [ 'title' => $data['title'], 'content' => $data['content'], ] ]; $response = $client->index($params); return json($response); } public function update() { $client = ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => [ 'doc' => [ 'title' => 'new title', ] ] ]; $response = $client->update($params); return json($response); } public function delete() { $client = ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id' ]; $response = $client->delete($params); return json($response); } } ``` 这里我们定义了四个方法,分别是 `search`、`add`、`update` 和 `delete`。 `search` 方法用于查询数据,我们在查询中使用了 `match` 查询,查询了 `title` 字段中包含 `ElasticSearch` 关键字的文档。 `add` 方法用于添加数据,我们使用了 `index` 方法,将 `my_table` 表中的数据添加到了名为 `my_index`,类型为 `my_type` 的文档中。 `update` 方法用于更新数据,我们使用了 `update` 方法,将 ID 为 `my_id` 的文档中的 `title` 字段更新为了 `new title`。 `delete` 方法用于删除数据,我们使用了 `delete` 方法,删除了 ID 为 `my_id` 的文档。 这只是一个简单的示例,实际使用中需要根据业务需求进行更详细的配置和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值