I want to check if user login only if email exist and activated==1
set in database but activated always show null value.
Image 1:
Image 2:
Try this one
$user = DB::table('users')->where([
['email', '=', $email],
['activated', '=', '1'],
])->get();
or
make your where clause with array to check multiple conditions.
You can use Model class to get data using where clause.
See Queries: Laravel
Another Example (eloquent):
$matchThese = ['email' => $email, 'activated' => '1'];
//........
//........
$results = User::where($matchThese)->get();
Related
I want to store the user_id in the profile table into a variable. In my index function I have:
$userid = Profile::where('user_id', auth()->user()->id)
->first();
return view ('profile.index',[
'user' => $user,
'userid' => $userid,
'about' => $about,
and in my index view:
#if(Auth::user()->id == $userid)
<h3>hello</h3>
#endif
Yet, I get this error:
Object of class App\Models\Profile could not be converted to int (View: C:\xampp\laravelprojects\testfrihand\resources\views\profile\index.blade.php)
Change it to get user_id from the model instance
$userid = Profile::where('user_id', auth()->id)
->first()->user_id;
Notice1: When you call first() method, you'll get an instance of eloquent model. You should tell it what attribute do you want. Here, you want user_id. Either ->first()->user_id and ->first()->get('user_id') will give your desired answer.
Notice2: You can get id of current authenticated user by calling auth()->id
Not actually understand what do you need exactly.
But anywhere
$userid = Profile::where('user_id', auth()->user()->id)
->first();
Here you get a Profile Object, not an id.
Please specify your question: do you want to store user_id in this code, or to get user_id and use it in condition?
$user = auth()->user();
$whatEverYouWant= Profile::where('user_id', $user->id)->first();
return view ('profile.index',[
'user' => $user,
'userid' => $user->id,
'about' => $about
]
$user= Select from user where 'email' = $email AND 'specialty' =null OR 'address' = null OR 'country' =null OR 'state' = null;
This is the code i have but its not working properly. What i want is to return the row if any of the stated columns has its value as null.
$doctor= Doctor::where('email',$email)->where(function ($query) {
$query->orWhere('state','=','')
->orWhere('country','=','')
->orWhere('address','=','')
->orWhere('specialty','=','');
})->get();
First things first - in SQL to filter by NULL one should be using where x IS NOT NULL.
For the laravel part, Eloquent has a whereNull method, see more: https://laravel.com/docs/5.6/queries
So your eloquent code should look somehing like:
$doctor= Doctor::where('email',$email)->where(function ($query) {
$query->orWhereNull('state')
->orWhereNull('country')
->orWhereNull('address')
->orWhereNull('specialty');
})->get();
I am trying to return a (JSON) string of a specific user with his/her posts. However the Post model contains several columns that aren't of interest for API implementations and I want to exclude these columns from the result.
Why does the following still return no columns at all in the Posts relation
I've tried multiple ways of retrieving specific columns on the Post model.
$result = User::with([
'posts' => function($q) {
$q->addSelect('title', 'tag');
}])
->where(['api' => 1, 'id' => $id])
->first(['id', 'username', 'role']);
return $result;
dumping
$q->get()
shows exactly what I want, however the returned $result includes none of the columns in the Post model.
My Laravel version is 5.2
try changing addSelect to just select
$result = User::with([
'posts' => function($q) {
$q->select(['title', 'tag', 'user_id']); // You might have to also include `user_id` here
}])
// ->where(['api' => 1, 'id' => $id]) change this
->where('api', 1)
->where('id', $id)
->first();
return $result;
Because addSelect is adding items to an existing select, rather than specifying what you actually want in the select.
I have the following function to create a new related model;
//Create the results entry
$result = new Result([
'result' => $total,
'user_id' => $user->id,
]);
//attach it to the fixture - parse through looking for the user_id or opponent_id
//to match the current user in the loop.
$fixture = LeagueFixture::where('league_id', $league_id)
->where('gameweek', $gameweek)
->where(function($q) use ($user){
$q->where('user_id', $user->id)
->orWhere('opponent_id', $user->id);
})
->first();
$fixture->results()->save($result);
The ->save() at the end does most of the magic, attaching the correct fixture_id to the result table. The problem is that if the function is run again, it creates new entries for the same results.
There is a firstOrCreate() method, but i don't know how to use this when saving a related model.
Thanks
It's exactly like this: http://laravel.com/docs/5.0/eloquent#insert-update-delete.
//Create or find a existing one...
$result = Result::firstOrCreate([
'result' => $total,
'user_id' => $user->id,
]);
//grab fixture...
$fixture = LeagueFixture::where('league_id', $league_id)
->where('gameweek', $gameweek)
->where(function($q) use ($user){
$q->where('user_id', $user->id)
->orWhere('opponent_id', $user->id);
})
->first();
//associate (set fixture_id in $result equals to $fixture's key)
//any previous association will disappear.
$fixture->results()->associate($result);
//save to write changes in the database.
$result->save()
you can check here (https://github.com/laravel/framework/blob/5.0/src/Illuminate/Database/Eloquent/Model.php#L559). Laravel will search in your database and return if it found it or create a new.
I am gettin error 'No query results for model [App\User].', when i am trying to find an user with this query.
if(User::where('email', '=', Request::only('email'))->firstOrFail()){
$validator->errors()->add(...);
}
Request::only() will actually return an array. In your case: ['email' => 'the-value-of-email']. Either do this:
User::where(Request::only('email'))->firstOrFail()
Or just use Request::input() to retrieve only the value:
User::where('email', '=', Request::input('email'))->firstOrFail()