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();
Related
How do you paginate the following line
$posts = Category::where('name', request('category'))->firstOrFail()->posts;
in the function below
public function index()
{
$categories = category::all();
if (request('category')) {
$posts = Category::where('name', request('category'))->firstOrFail()->posts;
} else {
$posts = post::where('status', 1)->orderBy('created_at', 'DESC')->paginate(5);
}
return view('user.blog', compact('posts', 'categories'));
}
You have to use the actual relationship method to get access to the query builder:
$posts = Category::where('name', request('category'))
->firstOrFail()
->posts()
->paginate(5);
See the Eloquent docs on Relationship Methods Vs. Dynamic Properties for reference.
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');
hi how can we implement a condition checks in laravel query builder
$flag_email =true;
$query = DB::table('customer');
if($flag_email) {
$query->where('email','=',$email);
}
if(!$flag_email) {
$query->where('mobile','=',$email);
}
$query->get();
use when method here to check condition see
$query = DB::table('customer')
->when($flag_email, function ($query,$email) {
return $query->where('email', $email);
})
->when(!$flag_email, function ($query,$email) {
return $query->where('mobile', $email);
})->get();
you can use ->when to do conditional check
$query = DB::table('customer')
->when($flag_email, function ($query, $email) {
return $query->where('email', $email);
})
->when(!$flag_email, function ($query, $email) {
return $query->where('mobile', $email);
})->get();
Ternary operator to the rescue:
$query = DB::table('customer')->where($flag_email?'email':'mobile',$email);
You can try by this way. This way will outputs your desire one. This just a way, I am not sure, this one is the proper way.
$flag_email =true;
$query = DB::table('customer');
if($flag_email)
$query = $query->where('email','=',$email);
if(!$flag_email)
$query = $query->where('mobile','=',$email);
$result= $query->get();
public function getDeletee($id)
{
$data['data'] = DB::table('lembur_karyawan')
->where('id', $id)
->delete();
$users = DB::table('users')
<your code??>
->first();
return redirect('user/detail/'.$users->name);
}
(ASK) how to call user who has logged to $users?
Use:
Auth::user();
Instead of:
$users = DB::table('users')
<your code??>
->first();
To get the name, use Auth::user()->name;
More on this here.
Need help on this simple code, what code should I replace at
$users = User::all(); so I can conditional chaining the scope method and paginate it at the end?
I tried initiate the User object with $users = new User(); and there is error, Trying to get property of non-object error when using at VIEW.
public function index()
{
// user search
$name = $this->request->name;
$email = $this->request->email;
$users = User::all();
if (!empty($name)) {
$users->name($name);
}
if (!empty($email)) {
$users->email($email);
}
$users->paginate(5);
return view('admin.users.index',compact('users'));
}
Thanks in advance
$users = DB::table('users');
$users = empty($email) ? $users->paginate(5) : $users->whereEmail($email)->paginate(5);
You do not need to check for a name if all users have an email, because email is always unique. That way to do the task is faster and more convinient.
In case, if you need to check name or anything else, you can use this:
$users = DB::table('users');
$users = empty($email) ? $users : $users->whereEmail($email);
$users = empty($name) ? $users : $users->whereName($name);
$users = $users->paginate(5);
Try this:
$users = new User;
if( !empty($name) )
$users->whereName($name);
if( !empty($email) )
$users->whereEmail($email);
$users =$users->paginate(5);
I found the answer from the following link
https://stackoverflow.com/a/21739314/417899
Eg code:
$users = new User;
if (!empty($name)) {
$users = $users->name($name);
}
$users = $users ->paginate(5);