markdown 为Laravel Relations添加更多条件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 为Laravel Relations添加更多条件相关的知识,希望对你有一定的参考价值。

```
public function connections()
{
	$relation = $this
		->belongsToMany(static::class, 'connections', 'requestor_id', 'requested_id')
		->withTimestamps();

	/// delete the already built inner join
	$relation
		->getQuery() // Eloquent\Builder
		->getQuery() // Query\Builder
		->joins = [];

	/// create a new inner join with the needed or condition
	$relation->getQuery()->getQuery()->join('connections', function($join)
	{
		$join->on('users.id','=','connections.requestor_id');
		$join->orOn('users.id','=','connections.requested_id');
	});

	return $relation;
}
```

Here's the full version, rebuilding the where clause and preventing the user to appear in the list of connections:

```
public function allConnections()
{
	$relation = $this
		->belongsToMany(static::class, 'connections', 'requestor_id', 'requested_id')
		->withTimestamps();

	// Delete the already built "inner join".
	$relation
		->getQuery() // Eloquent\Builder
		->getQuery() // Query\Builder
		->joins = [];

	// Delete the already built "where".
	$relation
		->getQuery()
		->getQuery()
		->wheres = [];

	// Delete all bindings.
	$relation
		->getQuery()
		->getQuery()
		->setBindings([]);

	// Create a new inner join with the needed or condition.
	$relation->getQuery()->getQuery()->join('connections', function($join)
	{
		$join->on('users.id','=','connections.requestor_id');
		$join->orOn('users.id','=','connections.requested_id');
	});

	// Create a new where with both conditions
	$relation->where(function($query)
	{
		$query->where('connections.requestor_id', $this->id);
		$query->orWhere('connections.requested_id', $this->id);
	});

	// A user is not connected to itself
	$relation->where('users.id', '!=', $this->id);

	return $relation;
}
```

以上是关于markdown 为Laravel Relations添加更多条件的主要内容,如果未能解决你的问题,请参考以下文章

Laravel文档阅读笔记-Adding a Markdown editor to Laravel

在 Laravel 8 和 VueJS 2 中使用 Marked 解析 Markdown

Laravel文档阅读笔记-Adding a Markdown editor to Laravel

Laravel项目中使用markdown编辑器及图片粘贴上传七牛云

Laravel 可以验证 markdown mime 类型吗?

Laravel 5.4 更改markdown邮件的主题