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();
Related
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
I am using laravel 5.6 and i want to get user posts with comments (only id field)
User Model
public function posts()
{
return $this->hasMany('App\Post');
}
Post Model
public function user()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
Comment Model
public function post()
{
return $this->belongsTo('App\Post');
}
In my controller i am using this code to get user posts with their comments
$posts = $request->user()->posts()->with(['comments' => function($query) {
$query->select(['id']);
}]);
But its not working...
When i comment $query->select(['id']); it works fine but returns Comment model all fields. I want to only select id field.
What i am missing here?
You also have to select the foreign key column (required for matching the results):
$posts = $request->user()->posts()->with('comments:id,post_id');
If you want to only one column, you can use ->pluck('id')
https://laravel.com/docs/5.6/collections#method-pluck
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 this query:
I'm building a forum and I would like to show the latest message that has being posted. I'm doing that like this:
return Forum::with(['users' => function($query){
$query->select('user.id', 'user.name', 'user.last_name')
}]);
forum model:
/**
* #return mixed
*/
public function users()
{
return $this->belongsToMany(User::class, 'message');
}
How do I only receive the latest user. Right now I receive them all.
Thanks a lot!
public function users()
{
return $this->belongsToMany(User::class, 'message')->orderBy('id', 'desc');
}
If you want to limit the number of users returned, append ->take(10); to take only last 10 users
Hey how can I make relationships between two table.
Users: id, email
Notification: id, user_id(id of the logged user), client_id(id of sender)
I would like make relationship between users and notifications by user_id and client_id.
Then I will can get all notifications assigned to logged user, and get email of sender users.
I made that:
public function notifications_with_client() {
return $this->hasManyThrough('App\Models\User', 'App\Models\Notification', 'user_id', 'id', 'client_id');
}
But when I using query i got good notifications but with wrong email.
I got email from ralationship id(from users table) == id(from notifications table)
My query
$column = 'notifications_with_client';
$value[1] = ['email', 'notifications.id', 'client_id'];
$query->with([$column => function($query) use ($value) {
$query->select($value[1]);
}]);
Someone know what I do wrong?
You can try it by defining the following relations:
User Model
public function notifications()
{
return $this->hasMany('App\Models\Notification');
}
Notification Model
public function to()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
public function from()
{
return $this->belongsTo('App\Models\User', 'client_id');
}
And then you can query it as:
$notifications = auth()->user()->notifications()->with('from')->get();
Or if you just want email then query it as:
$notifications = auth()->user()
->notifications()
->with(['from' => function($q) {
$q->select('email');
}])
->get();
public function user()
{
return $this->belongsTo(Users::class, 'user_id');
}
public function client()
{
return $this->belongsTo(Users::class, 'client_id');
}
With this code in your Notification Model you can get the logged user with
$this->user(); // $notification->user();
and the sender with
$this->client(); //$notification->client();
You cannot use $this->hasManyThrough(). It use for a different reason
You can use $this->belongsTo() like this.
class User extends BaseModel
{
public function user()
{
return $this->belongsTo(Notification::class, 'user_id');
}
public function client()
{
return $this->belongsTo(Notification::class, 'client_id');
}
}
Then you can query like.
User::with(['user']);
or
User::with(['client']);