Update Error in Laravel 4.2 - laravel-4

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);

Related

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();

Specifying where-and in Laravel's query builder

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();

How to call name user has login

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.

Laravel 5 eloquent conditional chaining query

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);

LIKE doesn't return results

I am trying to find "sam" inside FullName in the table users with this eloquent operation:
$user = User::where('id', '=', $input)->orWhere('fullName', 'LIKE', "%$input%")->find(10);
finding through id works as expected but the Where with LIKE isn't returning any results.
(if $input is 1 the first where returns a result but if $input is sam the second where doesn't return anything)
In the database fullName has the value "Sam Pettersson".
Anything I am doing wrong?
For some reason laravel doesn't want find() and like queries in the same query so using this instead worked:
$user = User::where('id', '=', $input)->orWhere('fullName', 'LIKE', "%$input%")->take(10)->get();
$user = User::where(function($query) use($input)
{
$query->where('id', '=', $input)
->orWhere('fullName','LIKE', '%'.$input.'%');
})->get();
This worked for me. Hope this work for you.

Resources