BadMethodCallException Call to undefined method App\User::map() - laravel

I want to add additional data to $user collection so can get the profile fields in the view
I have tried using $user['profileFields'] = $this->getProfileFields() and pass $user to the view using compatct or with and this is working fine.
DD
However, I have found some reference over the net saying I can extend using map but when I tried it is giving the following error
BadMethodCallException
Call to undefined method App\User::map()
So here is what I am trying to understand
Question:
Is the below code is wrong and won't work for what I am looking for and the first approach is the solution? Is there any
recommended method to add additional data to the $user collection?
public function show(User $user)
{
$user->map(function ($user){
$user['profileFields'] = $this->getProfileFields();
return $user;
});
return view('admin.user.show', compact('user'));
}

collection methods are works on the collection, here you're getting the object of user.
$user->profileFields = $user->getProfileFields();
return view('admin.user.show', compact('user'));

If the profileFields is in another table and having a foreign key with model ProfileField, then try to add a one to one relation to the User model.
Inside the User model, add a function
public function profileFields()
{
return $this->belongsTo('App\ProfileField', 'foreign_key','other_key');
}
This will give you the profileFields in every user when calling using eloquent.

Related

How to add scope query in Laravel scout to retrieve record in model

I have a model Post has method scope:
public function scopeABC($query) {
return $query->where('status', 'publish') //
}
And I want to after search by Laravel Scout, the results of Post will apply this scope
$posts = Post::searchable('zzzzz')->ABC()->paginate();
But Laravel fire exception Laravel\Scout\Builder has not method ABC. Hope you help me. Thanks!
It's not documented feature but you can pass a callback to query method
https://github.com/matchish/laravel-scout-elasticsearch/issues/18#issuecomment-505977823
$posts = Post::searchable('zzzzz')->query(function($query) {
return $query->ABC();
})->paginate();
I cannot find solution for you but i think your searchable(() cannot working with paginate(), your problem is not from scope function

Laravel Eloquent: get model id from inside getAttribute function?

I have a model like the following:
User extends Authenticatable
{
protected $appends = array('role');
public function getRoleAttribute()
{
$role = DB::table('acl_user_has_roles')->where('user_id', $this->id)
->value('role');
return $role;
}
}
When I try to reference this foo attribute like the following:
$user = User::find(1);
unset($user->id); // This line causes the problem.
echo $user->role;
I always get "null" instead of expected "Owner".
What did I miss here?
I am running this in laravel 5.5.43.
In the following post, getKey() function was mentioned, that didn't work.
The problem is actually caused by something else, as described in the following medium post.
Just in case anyone else runs into the same issue. The problem is caused by another line in the code. I unset $user->id before echo.
The issue is described in detail in the following medium post:
Be Careful to Use Laravel Eloquent’s Accessors, Mutators And Events

Sending multiple variables from controller to view with Laravel

Can someone please help me fixing this? I am getting a 'Undefined variable: roles' error on the view with this in the controller:
public function create()
{
$offices = Office::pluck('name', 'id');
$roles = Role::pluck('name', 'id');
return view('gebruikers.create')->with('offices', $offices, 'roles', $roles);
}
According to the docs, "As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view:"
In the code you posted your trying to post multiple pieces of data. This can be done by calling the with method multiple times.
return view('gebruikers.create')->with('offices', $offices)->with( 'roles', $roles);

Database relation with Laravel Eloquent ORM

I'm new to Laravel and I'm stuck. This is what I am struggling with:
$questions = Question::find($id)->quiz(); // this code retrieves data from
// the table using the primary key
// in the table. The is a parameter
// that is passed via get.
This is what I have right now:
$questions = Question::where('quiz_id', '=', $id)->quiz();
This is the error I get:
Call to undefined method Illuminate\Database\Query\Builder::quiz()
What I want to do:
I want to run a query to get data from my database table using the foreign key in the table not the primary key, I also want to be able to use relations with this as seen from what I tried to do above.
Edit: Added the Question Model
<?php
class Question extends Eloquent{
protected $table = 'quiz_questions';
public function quiz()
{
return $this->belongsTo('Quiz');
}
}
Calling the quiz() function from Question::find($id)->quiz() will return a Query Builder instance allowing you to query the parent of the Question, its not going to return any data at that point until you call ->get() or another method that actually executes the query.
If you're wanting to return all the questions belonging to a certain quiz then you can do it like this.
$questions = Question::where('quiz_id', $id)->get();
This will return an Eloquent\Collection of the results for all questions with a quiz_id that is equal to $id.
If you've setup the relations between the Quiz and Questions then you can also do this using the Laravel relations.
$quiz = Quiz::findOrFail($id);
foreach($quiz->questions as $question)
{
// Do stuff with $question
}
Laravel will automagically pull Questions from the database that belongTo the Quiz you've already got from the database, this is known as eager loading http://laravel.com/docs/4.2/eloquent#eager-loading
Wader is correct, just calling where() will not execute your query. You either call get() and get an iterable result or use first() if you only want one result.
$quiz = Question::where('quiz_id', '=', $id)->first()->quiz();

Laravel Eloquent - update() function

Laravel's Eloquent update() function returns a boolean, and not the updated entry. I'm using:
return User::find($id)->update( Input::all() )
And this returns only a boolean. Is there a way I can get the actual row, without running another query?
I think that's the behaviour that you want:
$user = User::find($id)->fill(Input::all());
return ($user->update())?$user:false;
I hope it works fine for you.
Another approach can is to use the laravel tap helper function.
Here is an example to use it for updating and getting the updated model:
$user = User::first();
$updated = tap($user)->update([
"username" => "newUserName123"
]);
tap($user) allows us to chain any method from that model. While at the end, it will return the $user model instead of what update method returned.

Resources