Answer the question
In order to leave comments, you need to log in
How to create a query with a subquery in Laravel Eloquent?
There are tables
log
id->index
dec_key
status
keys
id->index
dec_key->index relation with log->dec_key
user
comment
users
id->index relation with keys->user
login
I can't create a query with a subquery using the Eloquent model to display columns:
log->dec_key | users->login | keys->comment | log->status
The current query outputs log->dec_key | keys->user | keys->comment | log->status
controller:
namespace App\Http\Controllers;
use App\Log;
use App\User;
class LogsController extends Controller
{
public function index (Request $request) {
$req = Log::with(['comment:dec_key,comment,user']);
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
class Log extends Model
{
protected $table = 'log';
public function key () {
return $this->hasOne('App\Key', 'key', 'key');
}
public function comment () {
return $this->hasOne('App\Key1', 'dec_key', 'dec_key');
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
class Key1 extends Model
{
protected $table = 'keys';
}
namespace App;
use Illuminate\Database\Eloquent\Model;
class Key extends Model
{
public function user()
{
return $this->hasOne('App\User', 'id', 'user');
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function keys () {
return $this->hasMany('App\Key', 'user', 'id');
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question