I have made an Instagram like web-application with laravel 7.
The $posts are displayed in DESC order like so: (This is in the PostController)
public function index()
{
// All the users that are followed
$users = auth()->user()->following()->pluck('profiles.user_id');
// All the posts from those users
$posts = Post::whereIn('user_id', $users)->with('user')->latest()->paginate(5);
return view('posts.index', compact('posts'));
}
I want to create a new view named explore.blade.php. The content needs to be the latest post from all the users that are not being followed.
This $posts = Post::whereIn('user_id', $users)->with('user')->latest()->paginate(5); needs to be inversed so that these get left out.
You can use whereNotIn():
$posts = Post::whereNotIn('user_id', $users)->with('user')->latest()->paginate(5);
Related
I have a Laravel 5.8 application where one user can block another user.
User model:
public function blockedUsers()
{
return $this->belongsToMany(User::class, 'blocked_user', 'user_id', 'blocked_id');
}
Now every user can post articles. How to get all articles that are posted by non blocked users in users feed.
If I do:
$posts = Post::all();
Obviously I will get all posts, so how to make a where conditin and say all posts that are created by a user which is not blocked by Auth::user()?
$user = Auth::user();
$posts = Post::where(function($q) use ($user){
// Can't figure out this part
})->get();
You can first get your blocked user ids and use it later in the query to check if the creator of the post is one of the blocked ids using whereNotIn()
$user = Auth::user();
// Get list of blocked user ids
$blockedIds = $user->blockedUsers->pluck('id')->toArray();
// NOTE: I am assuming the field that hold the post creator id is user_id
// Get all post where the creator id is not in the blocked ids array
$posts = Post::whereNotIn('user_id',$blockedIds)->get();
Alternatively you can also write it like this
$user = Auth::user();
$posts = Post::where(function($q) use ($user){
$q->whereNotIn('user_id', $user->blockedUsers->pluck('id'))
})->get();
$user_id = Auth::id();
// get all blocked users by auth user
// note: you need to create BlockedUser model for blocked_user table
$blocked_users=BlockedUser::where('user_id',$user_id)->pluck('blocked_id');
// get all posts without posts that created by blocked users
Post::whereNotIn('user_id',$blocked_users)->get();
I am trying to do something I've never done before in Laravel and cannot figure out how to do it.
I have the following code in my Controller:
public function show($id)
{
//Get application for drug
$application = PharmaApplication::where('ApplNo', $id)->first();
//Get all products for given application (i.e. the different quantities and forms drug comes in)
$product = PharmaProduct::where('ApplNo', $id)->get();
foreach($product as $product){
$product->ProductNo;
}
//Get Marketing Status for drug
$marketingStatus = DB::table('pharma_marketing_statuses')
->where('ApplNo', $id)
->where('ProductNo', $product->ProductNo)
->get();
//Lookup marketing status Description
$marketingStatusDescription = PharmaMarketingSatusLookup::where('MarketingStatusID', $marketingStatus->MarketingStatusID);
return view('profiles.drug', compact('application', 'product', 'marketingStatus', 'marketingStatusDescription'));
}
I am trying to accomplish the following:
Get the application for a drug - this part of my code works
Return an array of objects for the products (i.e. 7 products that belong to one application). I can do this but get stuck going to the next part.
Next, I have to use the array of objects and search a table with the following columns: MarketingStatusID, ApplNo, ProductNo. I know how to query this table and get one row, but the problem is I have an array that I need to search. I imagine I have to use a loop but don't know where.
Finally, I use the MarketingStatusID to retrieve the MarketingStatusDescription which I will know how to do.
I am also getting an error message that says:
Class 'App\Http\Controllers\profiles\PharmaMarketingSatusLookup' not found
In my Controller, I have use App\PharmaMarketingStatusLookup; so I am not sure why it is searching the Controllers folder
You have a typo in your class
From PharmaMarketingSatusLookup change to PharmaMarketingStatusLookup
App\Http\Controllers\profiles\PharmaMarketingStatusLookup
USE whereIn
use App\PharmaApplication;
use App\PharmaProduct;
use App\PharmaMarketingSatusLookup;
public function show($id)
{
$application = PharmaApplication::where('ApplNo', $id)->first();
$products = PharmaProduct::where('ApplNo', $id)->get();
$productid = array();
foreach($products as $product){
$productid[] = $product->ProductNo;
}
$marketingStatus = DB::table('pharma_marketing_statuses')
->where('ApplNo', $id)
->whereIn('ProductNo', $productid)
->get();
$marketingStatusDescription = PharmaMarketingSatusLookup::where('MarketingStatusID', $marketingStatus->MarketingStatusID);
return view('profiles.drug', compact('application', 'product', 'marketingStatus', 'marketingStatusDescription'));
}
I know, we can do this in the controller:
User::with('post')->get();
It will get every user's post from the database, based on users.id.
But the problem is, I want to do this:
User::with(['post' => function($query) {
# Throw users.id here...
}])->get();
How to do that?
You should get the users first, and then load related posts with a separate query and merge them manually.
$users = User::get();
$posts = Post::whereIn('user_id', $users->pluck('id'))->get(); // Get your additional data in this query
$users->each(function ($user) use ($posts)
{
$user->posts = $posts->where('user_id', $user->id);
});
Note: I did not test the code above. It's just an example to show you how to accomplish what you are trying to do.
My method is:
public function show(Tag $tag)
{
$posts = $tag->posts;
return view('posts.index',compact('posts'));
}
It works fine but i want to get the posts where posts->user_id is the authenticated user.
public function show(Tag $tag)
{
$posts = $tag->posts()->where('user_id',Auth::user()->id);
return view('posts.index',compact('posts'));
}
How do i filter on the related posts table?
this is a many to many relationship with a pivot table present
What you have should work, but don't forget to get() the results after adding your where:
$posts = $tag->posts()->where('user_id',Auth::user()->id)->get();
Inside my Laravel project, I'm printing out all the business cards of my contacts.
In the contact model I have:
public function organizations()
{
return $this->belongsToMany('Organization')->withPivot('ContractStatus')->withTimestamps();
}
So in that way, I can show all the companies the contact is still working for.
In the footer of my contacts overview page, I want to print out all the associated companies logo's for that particular search query.
But something like this:
#foreach($contacts as $key => $value)
#foreach($value->organizations as $organization)
#endforeach
#endforeach
Will print duplicated company logo's as soon as the search results contains 2 contacts that both work for the same company.
If you want to list a distinct collection of a collection of contacts, then don't use relation, but whereHas:
$contactsIds = $contacts->modelKeys();
$organizations = Organization::whereHas('contacts', function ($q) use ($contactsIds) {
$q->whereIn('contacts.id', $contactsIds);
})->get();
Another way would be this trick, in case you in fact need to load the relation:
$organizations = null;
$contacts = Contact::with(['organizations' => function ($q) use (&$organizations) {
$organizations = $q->get();
}])->get();
$organizations = $organizations->unique();
This will load the relation as usually and also run additional query and assign its result to $organizations.
In the controller action generate a new Collection with the organizations:
$uniqueOrganizations = new Collection();
foreach($contacts as $contact)
$uniqueOrganizations->merge($contact->organizations);
Then pass it to the view, to show all the different organizations
#foreach($uniqueOrganizations as $org)
//show org logo
#endforeach