How to use Laravel with 3 where clause in function? - laravel

i have a function to show data with 3 where clause condition . but this output is invalid . i have 4 data and just show 1 . this function like this :
public function alkes_user()
{
$user_id = Auth::user()->id;
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$alat = Alat::with('users')
->where('jenis' ,'Alkes')
->where('user_id', $user_id)
->where('is_active', 'true')
->orderBy('created_at', 'desc')->paginate(10);
return view('users.alkes_user',['alat' => $alat , 'unit' => $unit ,'count' => $count ]);
}
this data only showing 1 , but in database i have so many data . what wrong and how to solve this ? thanks you

Rewrite your query like this if you want multiple where condition:
public function alkes_user(){
$user_id = Auth::user()->id;
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$alat = Alat::with('users')
->where([['jenis' ,'Alkes'],['user_id', $user_id],['is_active', 'true']])
->orderBy('created_at', 'desc')->paginate(10);
return view('users.alkes_user',['alat' => $alat , 'unit' => $unit ,'count' => $count ]);
}

Simple Solution
$alat= Alat::with(array('users' => function($query) {
$query->where([['jenis' ,'Alkes'],['user_id', $user_id],[is_active', 'true']]);
}))->orderBy('created_at', 'desc')->paginate(10);

$user_id = Auth::user()->id;
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$alat = Alat::with('users')->where([
['jenis' ,'=','Alkes'],
['user_id','=',$user_id],
['is_active','=','true']
])->orderBy('created_at', 'desc')->paginate(10);
return view('users.alkes_user',['alat' => $alat , 'unit' => $unit ,'count' => $count ]);

Related

Laravel - How to set all is status to 0 when the current one is 1

I have this code in my Laravel 5.8 controller:
public function store(StoreIdentityRequest $request)
{
$userCompany = Auth::user()->company_id;
$identity = Identity::create([
'name' => $request->name,
'is_status' => $request->has('is_status'),
]);
$id = $identity->id;
Identity::where('id', '!=', $id)
->where('company_id', $userCompany)
->update(['is_current' => 0]);
}
is_status can either be 0 or 1.
When user submits, if the current (or selected) is_status is 1, I expect the application to toggle all other is_status values to 0 except the current one.
I applied this code as shown in the controller:
Identity::where('id', '!=', $id)
->where('company_id', $userCompany)
->update(['is_current' => 0]);
but it turns everything to 0 even when the current is_status is 0
How do I resolve this?
you are doing all good just do is_current = 0 first, then make your new entry with is_current = 1 : something like this
public function store(StoreIdentityRequest $request)
{
$userCompany = Auth::user()->company_id;
if($request->is_status == 0){
Identity::where('company_id', $userCompany)->update(['is_status'=>0]);
}
$identity = Identity::create([
'name' => $request->name,
'is_status' => $request->is_status,
]);
}

I am getting undefined variable on second page of user search function please solve this issue

On first page showing data but when i click on next page then i am
getting
" Undefined variable: users (View: C:\wamps\www\service-
portal\resources\views\users\index.blade.php)"
public function usersearch(Request $request)
{
$first_name=$request->first_name;
$email=$request->email;
$status=$request->status;
$mobile_number=$request->mobile_number;
if($request->has('first_name') && $request->has('email') && $request-
>has('mobile_number') && $request->has('status') )
{
$users=User::where([
['first_name', 'like', '%'.$first_name.'%'],
['email', 'like', '%'.$email.'%'],
['mobile_number', 'like', '%'.$mobile_number.'%'],
['status', 'like', '%'.$status.'%'],
])->paginate(5);
return view('users.index',compact('users'));
}
Web.php
Route::Resource('user','UsersController');
Route::get('/usersearch', 'UsersController#usersearch');
In your view you need to pass your variable on every pages :
public function usersearch(Request $request)
{
$first_name = $request->first_name;
$email = $request->email;
$status = $request->status;
$mobile_number = $request->mobile_number;
if($request->has('first_name') && $request->has('email') && $request-
>has('mobile_number') && $request->has('status') )
{
$users=User::where([
['first_name', 'like', '%'.$first_name.'%'],
['email', 'like', '%'.$email.'%'],
['mobile_number', 'like', '%'.$mobile_number.'%'],
['status', 'like', '%'.$status.'%'],
])->paginate(5);
return view('users.index',compact('users','first_name', 'email', 'status', 'mobile_number'));
}
In your blade call your pagination like this :
{{ $users->appends(['first_name' => $first_name, 'email' => $email, 'status' => $status, 'mobile_number' => $mobile_number])->links() }}

display the users order history (laravel)

I want to create a page that shows users what they bought but I really have no idea how can I do this!
and this is cart view and I don't have cart model:
public function addtocart(Request $request){
$data = $request->all();
if (empty($data['user_email'])){
$data['user_email'] = ' ';
}
$user_id = Auth::id();
$session_id = Session::get('session_id');
if (empty($session_id)){
$session_id = Str::random(40);
Session::put('session_id' , $session_id);
}
DB::table('cart')->insert(['product_id' => $data['product_id'] , 'product_name' => $data['product_name'], 'user_id'=>$user_id,
'product_price' => $data['product_price'], 'qty' => $data['qty'], 'user_email' => $data['user_email'] , 'session_id' => $session_id ]);
return redirect('cart');
}
public function cart(){
$user_id = Auth::user()->id;
$session_id = Session::get('session_id');
$userCart = DB::table('cart')->where(['session_id'=>$session_id])->get();
foreach ( $userCart as $key=>$product){
$productDetail = Singleproduct::where('id' , $product->product_id)->first();
$userCart[$key]->image = $productDetail->image;
}
return view('UI.store.cart' , compact('userCart'));
}
can anyone help me in it plz?
To get a list of orders for a user you can do:
$history = DB::table('cart')
->where('user_id', auth()->id())
->join('products', 'products.id', '=', 'cart.product_id')
->select('products.name', 'cart.quantity', 'cart.product_price')
->get();
Then it's up to you to output it how you want

Column not found:

public function AddWishlist($id)
{
$userid = Auth::id();
$check = DB::table('whishlists')
->where('user_id', $userid)
->where('whishlists', $id)
->first();
$data = ['user_id' => $userid, 'product_id' => $id];
if (Auth::check())
{
if ($check)
{
$notification = ['messege' => 'already added whisahlist', 'alert-type' => 'error'];
return redirect()->back()->with($notification);
}
else
{
DB::table('whishlists')->insert($data);
$notification = [
'messege' => 'Suucessafully added whisahlist',
'alert-type' => 'success'
];
return redirect()->back()->with($notification);
}
}
else
{
$notification = array(
'messege' => 'At first login your account',
'alert-type' => 'error'
);
return redirect()->back()->with($notification);
}
}
Problem
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'whishlists' in 'where clause' (SQL: select * from `whishlists` where `user_id` = 1 and `whishlists` = 37 limit 1)
the logic will be to change
->where('whishlists', $id)
with
->where('whishlist_id', $id)
if its not working, Please show us the table whishlists
Note:
if $id is the id of the wishlist, i think this line should be reviewed :
$data = ['user_id' => $userid, 'product_id' => $id];
because it set 'product_id' with the whishlist id

How to set the logged in user ID on a session (CodeIgniter)?

I am having trouble, getting the logged in user ID from the database...
This is my controller code :
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if ($query){
$this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
This is my model function where I return the user id from the database:
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users');
foreach ($query->result() as $row)
{
$user_id = $row->id;
}
return $user_id;
}
Everything works perfect, except that $user_id returns empty... What am I doing wrong?
Try:
$user_id= $this->users_model->get_userID($this->input->post('email'));
try modifying the model function to this -
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users')->row_array(); // retrieves only first-row.
return $query['id'];
}
In controller the function needs just a bit of modification as follows -
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if($query){
//$user_id was not assigned before :)
$user_id = $this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
hope it helps,
faisal ahmed
blog / site

Resources