Laravel controller method for router with an optional parameter - laravel

I'd like to have a controller handling url like .../item/list and .../item/list/5.
First one would be to display all items, second one would display items whose author has an ID = 5.
public function getList($userid) {
$items = \DB::table('items')->get();
if ($userid) $items = \DB::table('items')->where('user_id', '=', $userid)->get();
foreach ($items as $item) {
// ...
}
}
URL like .../item has an argument missing.
Is there option to solve this?

I think your route looks like this:
Route::get('item/{userid}', 'MyController#getList');
But what should happen if there is no id?
Let's change your logic like this:
If there is a userId in the url then display the user's items. If there is no id display all.
So change your route:
Route::get('item/{userid?}', 'MyController#getList');
And also your function:
public function getList($userid = null) {
$items = \DB::table('items')->get();
if ($userid) $items = \DB::table('items')->where('user_id', '=', $userid)->get();
foreach ($items as $item) {
// ...
}
}

Create a route with an optional parameter
Route::get('item/list/{userid}', 'MyController#getList');
Then change userid in your function to have a default argument value
public function getList($userid = '') {
$query = \DB::table('items');
if ($userid) {
$query->where('user_id', '=', $userid);
}
$items = $query->get();
foreach ($items as $item) {
// ...
}
}

Related

Laravel filter of multiple variables from multiple models

Goodmorning
I'm trying to make a filter with multiple variables for example I want to filter my products on category (for example 'fruit') and then I want to filter on tag (for example 'sale') so as a result I get all my fruits that are on sale. I managed to write seperate filters in laravel for both category and tag, but if I leave them both active in my productsController they go against eachother. I think I have to write one function with if/else-statement but I don't know where to start. Can somebody help me with this please?
These are my functions in my productsController:
public function productsPerTag($id){
$tags = Tag::all();
$products = Product::with(['category','tag','photo'])->where(['tag_id','category_id'] ,'=', $id)->get();
return view('admin.products.index',compact('products','tags'));
}
public function productsPerCategory($id){
$categories = Category::all(); //om het speciefieke id op te vangen heb ik alle categories nodig
$products = Product::with(['category','tag','photo'])->where('category_id', '=', $id)->get();
return view('admin.products.index',compact('products','categories'));
}
These are my routes in web.php. I guess this will also have to change:
Route::get('admin/products/tag/{id}','AdminProductsController#productsPerTag')->name('admin.productsPerTag');
Route::get('admin/products/category/{id}','AdminProductsController#productsPerCategory')->name('admin.productsPerCategory');
For filter both
change your URL like
Route::get('admin/products/tag/{tag_id?}/{category_id?}','AdminProductsController#productsPerTag')->name('admin.productsPerTag');
Make your function into the controller like
public function productsPerTag($tagId = null, $categoryId = null){
$tags = Tag::all();
$categories = Category::all();
$query = Product::with(['category','tag','photo']);
if ($tagId) {
$query->where(['tag_id'] ,'=', $tagId);
}
if ($tagId) {
$query->where(['category_id'] ,'=', $categoryId);
}
$products = $query->get();
return view('admin.products.index',compact('products','tags', 'categories'));
}
You are trying to filter in your query but you pass only 1 parameter to your controller, which is not working.
1) You need to add your filters as query params in the URL, so your url will look like:
admin/products/tag/1?category_id=2
Query parameters are NOT to be put in the web.php. You use them like above when you use the URL and are optional.
2) Change your controller to accept filters:
public function productsPerTag(Request $request)
{
$categoryId = $request->input('category_id', '');
$tags = Tag::all();
$products = Product::with(['category', 'tag', 'photo'])
->where('tag_id', '=', $request->route()->parameter('id'))
->when((! empty($categoryId)), function (Builder $q) use ($categoryId) {
return $q->where('category_id', '=', $categoryId);
})
->get();
return view('admin.products.index', compact('products', 'tags'));
}
Keep in mind that while {id} is a $request->route()->parameter('id')
the query parameters are handled as $request->input('category_id') to retrieve them in controller.
Hope It will give you all you expected outcome if any modification needed let me know:
public function productList($tag_id = null , $category_id = null){
$tags = Tag::all();
$categories = Category::all();
if($tag_id && $category_id) {
$products = Product::with(['category','tag','photo'])
->where('tag_id' , $tag_id)
->where('category_id' , $category_id)
->get();
} elseif($tag_id && !$category_id) {
$products = Product::with(['category','tag','photo'])
->where('tag_id' , $tag_id)
->get();
} elseif($category_id && !$tag_id) {
$products = Product::with(['category','tag','photo'])
->where('category_id' , $category_id)
->get();
} elseif(!$category_id && !$tag_id) {
$products = Product::with(['category','tag','photo'])
->get();
}
return view('admin.products.index',compact(['products','tags','products']));
}
Route:
Route::get('admin/products/tag/{tag_id?}/{category_id?}','AdminProductsController#productsPerTag')->name('admin.productsPerTag');

how can i notify a specific users

here is my AdsController.php
public function save(Request $request)
{
$this ->validate($request,[
'object'=>'required',
'description'=>'string',
]);
$ads = new Ad;
$current_user=Auth::user();
$ads->object = $request->input('object');
$ads->description = $request->input('description');
$ads->save();
$users = User::where(("id","!=",$current_user->id ) || ("admin","=",1 ))->get();
foreach ($users as $user) {
$user->notify(new NewAd($current_user, $ads));
}
return redirect('listads') ;
}
I want to post an ad to the admin only but i have an error
Please help me
You made a simple mistake of your query builder, the conditions should be chained as follow:
$users = User::where('id', '<>', $current_user->id)->where('admin', 1)->get();
Use where condition like this
$users = User::where("id","!=",$current_user->id)
->orWhere("admin",1 )->get();

Filter model with HasMany relationship

Let's say I have two models: Park and Items. Each Park can have a number of Items, through a HasMany relationship.
In Items table there's a park_id field and a type flag: an Item can be a fountain, or a basketball court, or whatever.
What I need to do is to filter the parks to obtain only those who has ALL of the item types passed in an array. This is what I have:
$parks = Park::whereHas('items', function($q) use ($request) {
$q->where('type', json_decode($request->type_list));
})->get();
But it's not working properly. Any ideas?
Thanks a lot, and happy new year to all!
Edit: I've found a working though really ugly solution:
$types_array = json_decode($request->type_list, true);
$results = [];
// A collection of parks for each item type
foreach($types_array as $type) {
$results[] = Park::whereHas('items', function($q) use ($type) {
$q->where('type', $type);
})->get();
}
// Intersect all collections
$parks = $results[0];
array_shift($results);
foreach ($results as $collection) {
$parks = $collection->intersect($parks);
}
return $parks;
You can use a foreach in whereHas method. It should be something like this:
$array = json_decode($request->type_list, true)
$parks = Park::whereHas('items', function($q) use ($array) {
foreach ($array as $key => $type) {
$q->where('type', $type);
}
})->get();
I think using where clause to match all item's type for the same row will result nothing, it's like doing where id = 1 and id = 2 and this is impossible because id can take only one value, what you should do is to use whereIn to get all items that match at least one type and then use groupBy to group result by park_id, then in the having clause you can check if the group count equal the type_list count:
$types = json_decode($request->type_list, true);
$parks = Park::whereHas('items', function($q) use ($types) {
$q->select('park_id', DB::raw('COUNT(park_id)'))
->whereIn('type', $types)
->groupBy('park_id')
->havingRaw('COUNT(park_id) = ?', [ count($types) ]);
})->get();
You can add multiple whereHas() constraint to one query:
$types_array = json_decode($request->type_list, true);
$query = Park::query();
foreach($types_array as $type) {
$query->whereHas('items', function($q) use($type) {
$q->where('type', $type);
});
}
$parks = $query->get();

Laravel - passing array of columns in 'where' clause

I have a search query like this:
$data = User::where('first_name', 'like', '%'.$query.'%')
->orWhere('last_name', 'like', '%'.$query.'%')
->get();
Now, I have many models, each with different column names. Instead of defining a search() function into every controller, I want to do this:
// User
public static function searchableFields()
{
return ['first_name', 'last_name'];
}
// Some other model
public static function searchableFields()
{
return ['name', 'description'];
}
And put the search logic in a shared controller, something like this:
$data = $classname::
where($classname::searchableFields(), 'like', '%'.$query.'%')
->get();
How can I achieve this?
Thanks a lot.
You can loop over the fields and add them to your Eloquent query one by one.
$data = $classname::where(function ($query) use ($classname) {
foreach ($classname::searchableFields() as $field)
$query->orWhere($field, 'like', '%' . $query . '%');
})->get();
I would use scope for that.
You can create base model that all the models should extend (and this model should extend Eloquent model) and in this model you should add such method:
public function scopeMatchingSearch($query, $string)
{
$query->where(function($q) use ($string) {
foreach (static::searchableFields() as $field) {
$q->orWhere($field, 'LIKE', '%'.$string.'%');
}
});
}
Now you can make a search like this:
$data = User::matchingSearch($query)->get();
Just to avoid confusion - $query parameter passed to matchingSearch becomes $string parameter in this method.
You can try something like this.
// Controller
function getIndex(Request $request)
{
$this->data['users'] = User::orderBy('first_name','asc')->get();
if ($request->has('keyword')) {
$results = User::search($request->keyword);
$this->data['users'] = collect([$results])->collapse()->sortBy('first_name');
}
}
// Model
function search($keyword)
{
$results = [];
$search_list = ['first_name','last_name'];
foreach ($search_list as $value)
{
$search_data = User::where($value,'LIKE','%'.$keyword.'%')->get();
foreach ($search_data as $search_value) {
$exist = 0;
if (count($results)) {
foreach ($results as $v) {
if ($search_value->id == $v->id) {
$exist++;
}
}
if ($exist == 0) {
$results[] = $search_value;
}
} else{
$results[] = $search_value;
}
}
}
return $results;
}

Scan pivot table for two identical values

I have this pivot table:
+----+---------+-----------------+
| id | user_id | conversation_id |
+----+---------+-----------------+
| 1 | 2 | 48 |
+----+---------+-----------------+
| 2 | 1 | 48 |
+----+---------+-----------------+
I am trying to find a way to detect if two users are in the same conversation, and if they are, return the conversation. Someting like:
Conversation::with([1, 2]); // should return conversation 48
My Conversation class looks like this:
class Conversation extend Eloquent {
public function users() {
return $this->belongsToMany('User');
}
public function messages(){
return $this->hasMany('Message');
}
}
So far I have below code, but it is clumsy and very ineffective, and thought that there must exists some cleaner Laravel way:
class Conversation {
public static function user_filter(Array $users) {
$ids = array();
foreach ($users as $user)
$ids[$user->id] = $user->id;
sort($ids);
foreach (self::all() as $conversation){ // loop through ALL conversations (!!!)
$u_ids = $conversation->users->lists('id');
sort ( $u_ids );
if ($u_ids === $ids)
return $conversation;
}
return null;
}
}
Usage:
Conversation::user_filter([User::find(1), User::find(2)]); // returns conversation 48
Any ideas?
Let's first look at how you can actually retrieve the result without having too loop trough all kind of arrays...
You can use the whereHas function to filter a relationship. We can apply conditions and if they are met for the related records of a model, it will be included in the result.
$conversations = Conversation::whereHas('users', function($q){
$q->where('user_id', 1);
})->get();
So that's for one user. For two or more users it's basically the same. You just need to chain whereHass
$conversations = Conversation::whereHas('users', function($q){
$q->where('user_id', 1);
})
->whereHas('users', function($q){
$q->where('user_id', 2);
})->get();
Now this isn't very elegant. You can create a query scope to do all the work for you and keep the controller code clean. In Conversation
public function scopeWithUsers($query, array $userIds){
foreach($userIds as $id){
$query->whereHas('users', function($q) use ($id){
$q->where('user_id', $id);
});
}
return $query;
}
Usage:
$conversations = Conversation::withUsers([1,2])->get();
$conversations = Conversation::withUsers([1,2,3,4])->get();
$conversations = Conversation::withUsers([1])->get();
If it should only be one anyways use first() instead of get()
Something like this should work.
class Conversation {
public static function user_filter(Array $users) {
$ids = array();
foreach ($users as $user) {
$ids[] = $user->id;
}
return self::select("conversation_id")->whereIn("user_id", $ids)->groupBy("conversation_id")->havingRaw("count(*) = " . count($ids))->get();
}
}
Get all conversations with given users.
Conversation::whereHas('users', function($query) use ($userids) {
$query->whereIn('user_id', $userids);
})->get();
If you need get special one, you can:
Conversation::whereHas('users', function($query) use ($userids) {
$query->whereIn('user_id', $userids);
})->whereId($id)->first();

Resources