In my laravel-application I want to display a list of all users/candidates. So far I have this, which basically works fine:
public function candidates()
{
$users = User::whereHas(
'roles',
function ($q) {
$q->where('slug', 'candidates');
}
)->get();
return response(['success' => true, "users" => $users], 200);
}
Now, I actually want to display users/candidates who have taken an education. I have a Model called UserEducation but I don't really know to include it to get the data out I want.
can someone help me out?
EDIT
In my User model, I have this relation:
public function educations()
{
return $this->hasMany('App\Models\UserEducation');
}
you can use has:
$users = User::has('UserEducation')->whereHas(
'roles',
function ($q) {
$q->where('slug', 'candidates');
}
)->get();
You can try create relationship with User and UserEducation models and then get data.
https://laravel.com/docs/7.x/eloquent-relationships
Related
I need to query and check if the relationship column is the same in my main table column.
example codes:
not working
User::with('product', => function($q) {
$q->whereRaw('users.company_id',' product.company_id');
})->get();
not working
User::with('product', => function($q) {
$q->whereRaw('users.company_id = product.company_id');
})->get();
not working
User::with('product')->whereColumn('users.company_id', 'product.company_id')->get();
but it's not working..
any idea how to do it?
My Models
User Model
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
Product Model
public function users()
{
return $this->hasMany(User::class, 'product_id');
}
Please try again:
User->with(['product' => function ($query) {
$query->join('users', 'users.company_id', '=', 'product.company_id');
}])->get();
Try to use DB:raw() then show the table info? like this
I have three tables that are the following users, mapas, marcadores
A user has several mapas
a mapa has several marcadores
What I'm trying to do is show the marcadores that belong to the mapas of the user who has logged in.
this are the tables and relationship
This is the function in the controller that I am working on:
public function index()
{
$mapas = Mapa::orderBy('id', 'DESC')->where('user_id', auth()->user()->id);
$marcadores = Marcador::orderBy('id', 'DESC')->where('mapa_id');
return view('user.marcadores.index', compact('marcadores'));
}
thanks for your help
You are trying to get all the mapas id first and then filter marcadores according to those id's. Try using the code below for that:
public function index()
{
$mapas = Mapa::orderBy('id', 'DESC')->where('user_id', auth()->user()->id)->pluck('id')->toArray();
$marcadores = Marcador::orderBy('id', 'DESC')->whereIn('mapa_id', $mapas)->get();
return view('user.marcadores.index', compact('marcadores'));
}
You can use JOINS for this. Try the next code (maybe the syntax is not the correct one, but take the idea):
public function index()
{
$marks = DB::table('mapas')
->join('marcadores', 'marcadores.mapa_id', '=', 'mapas.id')
->where('mapas.user_id', auth()->user()->id)
->select('marcadores.*')
->orderBy('marcadores.id', 'DESC')
->get();
return view('user.marcadores.index', compact('marks'));
}
The easiest and Laravel standard way to do this is create a hasManyThrough relationship from User to Marcadores
UserModel
class User extends Model
{
public function marcadores()
{
return $this->hasManyThrough(Marcadores::class, Mapas::class);
}
}
Controller
public function index()
{
$marcadores = auth()->user()->marcadores;
return view('user.marcadores.index', ['marcadores' => $marcadores]);
}
I'm still learning Laravel and I can't find the solution for this problem.
I need to get invoices(with expenses) that are related to specific Partner Type.
I tried this:
$p = Project::with(['invoices.partner.partnerType' => function($query){
$query->where('partnerTypeName', 'Lieferant');
}, 'expenses'
])->where('id', $id)
->first();
I want to select invoices for Lieferant, but I get all invoices for one project.
Project Model:
public function invoices()
{
return $this->hasMany('App\Invoice');
}
Invoice Model
public function expenses()
{
return $this->hasMany('App\Expense');
}
public function partner()
{
return $this->belongsTo('App\Partner');
}
Partner Model
public function partnerType()
{
return $this->belongsTo('App\PartnerType');
}
Edit: PartnerType Model
public function partners()
{
return $this->hasMany('App\Partner');
}
Edit 2: Database
Partner(partnerID, name, partnerTypeId)
PartnerType(partnerTypeId, partnerTypeName)
Project(projectID, name)
Invoice(invoiceID, name, projectID, partnerID)
Expenses(expenseID, invoiceID)
If your models look like that.
Should be like :
$p = Project::with(['invoices' => function($query){
$query->where('partnerTypeName', 'Lieferant')
->with(['expenses','partner' => function($q){
$q->with('partnerType');
}]);
}])->where('id', $id)
->first();
return dd($p);
The solution to your problem is to update your query like this:
$p = Project::with(['invoices' => function($query){
$query->with('expenses')->whereHas('partner.partnerType', function($q){
$q->where('partnerTypeName', 'Lieferant');
});
}])
->where('id', $id)
->first();
But a cleaner solution would be using a scope for your problem.
In your Invoice model.
// Invoice.php
public function scopeByPartnerType($query, $partnerType)
{
$query->whereHas('partner.partnerType', function($q) use ($partnerType) {
$q->where('partnerTypeName', $partnerType);
});
}
And then in your Project model, add another relation that will just get Invoices with a particular partner type.
// Project.php
public function lieferantInvoices()
{
return $this->hasMany('App\Invoices')->byPartnerType('Lieferant');
}
Now you can do just this:
$project->find($id)->load('lieferantInvoices');
I want to fetch all orders and connect them with their statuses, but I want only the last status for each. I was trying to do it like this, but I only get the status for the first order the rest doesn't have any. How can I fix it?
static function actualKioskOrders()
{
$orders = Order::query();
return $orders->with([
'statuses' => function ($query) {
$query->orderBy('created_at', 'desc')->first();
}
]);
}
In order model add this method, I suggest doing query on model class.
public function status(){
return $this-> belongsToMany(Status::class);
}
public function latestStatus()
return $this->status()->latest()->first();
}
.
I believe the easier way to do it would be
$orders = Order::with('status')->orderBy('created_at', 'desc');
Then if you dd($orders); you will have all the orders with their corresponded status.
I've got this query in Laravel:
return Forum::with(['users' => function($query){
$query->select('user.id', 'user.name', 'user.last_name');
}])->latest()->withCount('messages')->paginate(10);
This returns all users that have posted a message in a forum.
On my forum model I've these relations:
public function users()
{
return $this->belongsToMany(User::class, 'message');
}
public function messages()
{
return $this->hasMany(Message::class);
}
I would like to receive only the latest user that has posted a message. How could I do this?
You can retrieve the last user by messages relationship, try this:
return Forum::with(['messages' => function($query){
$q->orderBy('created_at', 'desc');
},'messages.users'])->first();
Use has() method and order by created_at in descending order.
User::has('forums.messages')->orderBy('created_at','desc')->first();