Hello!
Laravel version 9.x
PHP version 8.1
Current code:
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Site $site
*
* @return \Illuminate\Http\Response
*/
public function edit(Request $request, Site $site)
{
$result = Server::select('id', 'name')
->orderBy('name', 'asc')
->simplePaginate(config('app.limit'));
if ($request->ajax()) {
return response()->view('site.ajax.edit', compact('site', 'result'));
}
return response()->view('site.edit', compact('site', 'result'));
}
\ app/models/Server.php
public function sites()
{
return $this->belongsToMany(Site::class, 'server_site', 'server_id', 'site_id')->withTimestamps();
}
\ app/models/Site.php
public function servers()
{
return $this->belongsToMany(Server::class, 'server_site', 'site_id', 'server_id')->withTimestamps();
}
New business rule: List all servers, paginated, showing first all servers related to the informed site, followed by the other servers that have no relationship with the informed site or any other site. In all cases (abstinence and existence), the result must be ordered by server name.
This the part of the code that I need to be updated.
$result = Server::select('id', 'name')
->orderBy('name', 'asc')
->simplePaginate(config('app.limit'));
Note: Bringing in all the results to do the sort in PHP would not be an option. The idea is to bring the result ready with one query only. I tried using join() and with() but I didn't get it. I think because of my own limitation. If anyone has a suggestion on how to do this, I would appreciate the help.
Please, I'm not asking anyone here to make the query for me, but I need some guide coz I'm stucked :(
Thx for you help :)