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.
Related
I have a problem: how can I compare a user's value with a database?
public function login(LoginRequest $request)
{
$data = $request->validated();
$host = Admin::where('email', '=', $data['email'])
->where('password', '=', $data['password'])
->get();
dd($host);
return redirect()->route('login.admin.show');
}
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();
I have the following code which works:
$name = 'jhon';
$users = DB::table('account')->where('login', '=', $name)->get();
How would I specify the AND parameter so that I can query on multiple conditions?
$login = $request->login;
$password = $request->password;
$users = DB::table('account')->where([
['login', '=', $login],
['password', '=', $password]
])->get();
Doesn't work.
$users = DB::table('account')
->where('login', '=', $login and 'password', '=', $password)->get();
To clarify, I'm looking specifically for a query builder solution, not an Eloquent solution.
Use where method twice:
$users = DB::table('account')
->where('login', '=', $login)
->where('password', '=', $password)
->get();
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);
Am using laravel 4.2 and i want to an update.
//-------IN USERCONTROLLER-------///
public function getUpdatepage() {
$user = User::where('id', '=', Auth::user()->id->get());
View::make('users.updatepage')->with('user', $user);
}
//------IN THE BROWSER------//
It says "Trying to get a property of non-object"
You have a typo in your code.
This line
$user = User::where('id', '=', Auth::user()->id->get());
should be this:
$user = User::where('id', '=', Auth::user()->id)->get();
As i assume id is the primary key of the user table.try this
$user = User::find(Auth::user()->id);
if not
$user = User::whereId(Auth::user()->id);