I am trying to update a record in the database and am getting "Method save does not exist" ---- code looks like:
$listingsDB = User::where('id', '=', auth()->id())->get();
foreach($listingsDB as $numberOfListing){
$listings = $numberOfListing->numberOfListings;
}
$listings = ++$listings;
$listingsDB->numberOfListings = $listings;
$listingsDB->save();
Any help appreciated. I can't see what I'm doing wrongs - Thanx
Actually, code should look like this and should work - -did not know about the increments function - so here it is:
$user = auth()->user(); //no need to query to get the current user, we
already have it!
$user->increment('numberOfListings');
$user->save();
Related
Trying to do assignRole() to many users, but it show me Call to undefined method Illuminate\Database\Query\Builder::assignRole(). Is this spatie laravel bug?
$get_username = \App\Applicant::select('username')->whereIn('id', $ids);
$updateUser = \App\User::whereIn('username', $get_username);
$updateUser->assignRole('Applicant');
$updateUser->save();
Any solution for bulk assignRole() in this case?
$get_username = \App\Applicant::select('username')->whereIn('id', $ids);
This is an incomplete query via the QueryBuilder. As well as the second line.
When utilizing the QueryBuilder you need to finish of your queries with the get() method to get a collection to iterate over.
The correct execution would like like this:
$get_username = \App\Applicant::select('username')->whereIn('id', $ids)->get();
Without knowing the exact result of this, it looks like you'd like to retrieve a collection of usernames, that you'd like to get the User model from.
You could do something like the following (untested) for bulk updating.:
$users = User::whereIn('username', function($query) use ($ids) {
$query->select('username')
->from('applicants')->whereIn('id', $ids);
})->get();
foreach($users as $user) {
$user->assignRole('Applicant');
$user->save();
}
I'm very confused by this!
In a laravel controller:
$user = Auth::user();
return $user;
returns the entire user object
$user = Auth::user()->id;
return $user;
returns the id of $user as expected
however if I put that exact thing into a query such as:
$user = Auth::user();
$query = Model::where('user_id', $user->id)->get();
return $query;
I get an error that I'm trying to get an 'id' property of a non-object!!!
How can this be possible?
I've also checked 100 times that I'm logged in when testing this.
Editing to add that I've also tried:
$query = Model::where('user_id', 1)->get();
and that works fine
edit to show the function:
$user = Auth::id();
$result = Lesson::where('user_id', $user)
->whereNotNull('notes')
->get();
return response()->json($result, 200);
Expected results: A list of objects with a filled in "notes" column.
Actual results: an empty [] and an error saying can't get id of a non-object
Don't use Auth in Models.
You can set a relation between User and your Model and do query like:
Auth::user()->relation()->get();
This error occurs if the Model doesn't contain any raw with that user id. Make sure you have the raw in the Model where you are querying to get data by user id.
You should try this
$user = Auth::user();
$result = Model::where('user_id', $user['id'])->get();
The error occurs because of you passed property of non-object.
Even after error not solve then restart your xampp or wamp.
If possible, upload the screenshot of error.
Your code should work if you are authed, it seems your are not based on the error message. Since you return json, i assume this is an api request. Api-routes uses another auth driver as default in Laravel (token vs. session). Either change the driver/guard or move your route(s) from routes/api.php to routes/web.php.
See docs for auth
You can change the driver in config/auth.php.
try this
$user_id = Auth::user()->id;
$query = Model::where('user_id', $user_id)->get();
return $query;
I want to order by created_at posts in descending order
I also would like to paginate posts.
My issue is, I am not getting data directly through Post Model, I don't know how to do this.
I am using Laravel 5.7. This is what I have written inside a function in HomeController.
$id = auth()->user()->id;
$user = User::find($id);
return view('home')->with('posts',$user->posts);
Thank You.
Try this:
$posts = auth()->user()->posts()->latest()->paginate();
return view('home', compact('posts'));
i want to display data from my database
controller
public function generatePDF(Request $request)
{
$id = Auth::user()->id;
$name = User::select("NAME")->where("id", $id)->get();
$pdf = PDF::loadView('generatePDF', ['name'=>$name]);
return $pdf->stream('generatePDF.pdf');
}
blade
<h2>{{name}}</h2>
result
[{"NAME":"name_from_database"}]
can i display without [{"name":}], so just the value of data (name_from_database) ?
Simple use find like this
$name = User::find($id)->name;
Or direct from auth
$name = \Auth::user()->name;
To get logged in user id you can use \Auth::id()
you can use:
$user_id = User::findOrFail($id)->first();
$name=$user_id->name;
test the laravel log file:
\Log::info($name);
Use first() instead of get(), because get() will return an array and first() will return the object.
DRY Code line no.1 and no.2. simple use:
$name = auth()->user()->name;
to get the name of the currently authenticated user.
Send $name to you view is fine
$pdf = PDF::loadView('generatePDF', ['name' => $name]);
Correct your code in blade file
{{ name }} -> {{ $name }}
I hope this might help you. :)
Hello Aldi Rostiawan,
$nameGet = User::select("NAME")->where("id", $id)->get();
$nameFirst = User::select("NAME")->where("id", $id)->first();
Here in both line of code the difference is only Get and First.
Get method returns an Array of objects. So for example if the query apply on many records then the code will give you many objects in an array. Or if query will be true for only one record then also it will return an array with one object.
If you know that id is primary key of he table then only one record will be fetched then you can use First function instead if Get method.
First function always return an Object. In case if query apply on many records then also first method will return you only first record as an Object.
And it seems that you have required an Object only.
You should try this:
Controller
public function generatePDF(Request $request)
{
$id = Auth::user()->id;
$rsltUser = User::where("id", $id)->first();
$pdf = PDF::loadView('generatePDF', ['rsltUser'=>$rsltUser]);
return $pdf->stream('generatePDF.pdf');
}
blade
<h2>{{$rsltUser->name}}</h2>
use VALUE() to display name only.
in your case:
<h2>{{$rsltUser->value('name')}}</h2>
your name variable is an array containing a single object with a NAME attribute .. so to diplay that value, you should change your script to
<h2>{{name[0]['NAME']}}</h2>
I'm using Laravel 5.3.
If I do something like this:
$posts = Post::where('id', 1)
->with('stats')
->first();
Is it possible to save the stats relationship, like:
$posts->stats->num_users = 5;
$posts->stats->save();
Is this the correct way to accomplish this?
You can do something like that
$posts = Post::where('id', 1)
->with('stats')
->first();
$stats = $post->stats;
if you want to update the value then
$stats->update(['num_users'=> 5]);
that should do.
Yes, that is possible.
However, it could just be the 'stats' relation is not yet created on your post object.
If that's the case, you could do something like this:
//retrieve the post
$post = Post::with('stats')->find(1);
//use existing stats object or new up one
$stats = $post->stats ?: new PostStats(['post_id' => $post->id]);
$stats->num_users = 5;
//persist
$stats->save();