Update index algolia - laravel

I'm using Laravel 5.5 with Scout. I have an index in algolia using Documents and the users associated with theses Documents
class Documents extends Model
{
use Searchable;
public function toSearchableArray()
{
$data = $this->toArray();
// formatting relationship for algolia
$data['users'] = $this->types->toArray();
$data['document_type'] = $this->typeDocuments->name;
return $data;
}
protected $fillable = array('name', 'description', 'document_type');
public function types() {
return $this->belongsToMany('App\Users', 'document_rights', 'user_id', 'id');
}
public function typeDocuments() {
return $this->belongsTo('App\Document_type', 'document_type');
}
}
In my case, I will update one day the name of the Users, like this:
public function update(Request $request)
{
$user = Users::find(5);
$user->name = 'jean floriot';
$user->update();
}
But it never changes the User in the index of Algolia. Any ideas how to proceed ?

I believe you can use the searchable() method for that. After $this->update() you will get something like:
App\Documents::where('user_id', '=', $this->id)->searchable();
Please let me know if that worked or if I missed something.

Related

Laravel - How to use eloquent ORM to populate a foreign key column when getting all results from a table?

I have setup my model as following:
class Items extends Model {
use HasFactory;
protected $table = 'item';
protected $primaryKey = 'id';
protected $connection = 'mysql';
public $timestamps = false;
protected $fillable = ['user_id', 'title', 'desc', 'start_datetime', 'due_datetime', 'priority', 'status'];
public function getManager() {
return $this->belongsTo(User::class, 'user_id');
}
public function getAssignees() {
return $this->belongsToMany(User::class);
}
}
I am getting all items using the controller method below, what I want to do is to populate the user_id field in each of the items using getManager() method I declared in my Item model. I know how to do this when getting only one item, but how to populate every record when getting all of them?
public function getall() {
try {
$items = Item::get();
return response()->json(['items' => $items], 200);
} catch (Throwable $err) {
return response()->json($err, 400);
}
}
I have tried this but no luck:
public function getall() {
try {
$items = Item::get();
$items = array_map(function ($el) {
return $el->manager = $el->getManager()->get();
}, $items);
return response()->json(['items' => $items], 200);
} catch (Throwable $err) {
return response()->json($err, 400);
}
}
There are a few things here that I have some concerns about. Your code may work, but you are also doing more than you need to and not using Laravel how it was meant to be used.
Model Name
Your model name is Items, but it should be singular, Item. This helps Laravel automate things so you have less work to do.
https://laravel.com/docs/8.x/eloquent#eloquent-model-conventions
class Item extends Model {
Database Settings
You've set the $table, $primaryKey, and $connection attributes, but these should be automatic. You can probably remove them.
protected $table = 'items'; // assuming your model name is Item, this would automatically be 'items'
protected $primaryKey = 'id'; // default is already 'id'
protected $connection = 'mysql'; // default is your main db, probably already 'mysql', unless if you have multiple db connections
Timestamps
I'm not sure why you'd want to turn timestamps off. You definitely can but I always find it helpful to know when something was created or last updated. Since Laravel handles the timestamps for you, I'd suggest leaving it on, but it's up to you.
https://laravel.com/docs/8.x/eloquent#timestamps
public $timestamps = false;
Manager Relationship
Your manager relationship is getManager but should just be manager. It will still work, but isn't how Laravel was meant to work. I would suggest changing it to manager(), and not specifying the column name. This would make the column name automatically manager_id, so you'd have to update that. Or you can keep the column name 'user_id'.
https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-inverse
public function manager() {
return $this->belongsTo(User::class);
}
Assignees Relationship
Same as with the Manager relationship, you should change getAssignees() to assignees(). I'm assuming you already have a database migration set up for your 'item_user' table that Laravel will look for. If not, check the Laravel docs on how to set it up.
https://laravel.com/docs/8.x/eloquent-relationships#many-to-many
public function assignees() {
return $this->belongsToMany(User::class);
}
Retrieving Items
Finally, with the above changes, getting all Items should be easy. To load the relationships, use the $with method. This is called Eager Loading. Check the docs for more info.
https://laravel.com/docs/8.x/eloquent-relationships#eager-loading
$items = Item::with('manager','assignees')->get();
Returning Response Codes
You were returning your responses incorrectly. You do not need to set the response code 200, as this is the default. If you are going to set it to something else, put the code in the response() method, instead of the json() method.
https://laravel.com/docs/8.x/responses
return response()->json(['items' => $items]);
return response($err,400);
Now putting it all together, your Item model should look something like this:
class Item extends Model {
use HasFactory;
protected $fillable = ['manager_id', 'title', 'desc', 'start_datetime', 'due_datetime', 'priority', 'status'];
public function manager() {
return $this->belongsTo(User::class);
}
public function assignees() {
return $this->belongsToMany(User::class);
}
}
public function getall() {
try {
$items = Item::get()
->transform(function($el){
$el->manager = $el->getManager()->get();
);
return response()->json(['items' => $items], 200);
} catch (Throwable $err) {
return response()->json($err, 400);
}
}
Try the transform method on your results and it would work.
https://laravel.com/docs/8.x/collections#method-transform
the transform function would basically just iterate over the results and do whatever it is told to like a for loop but for collections.
Also, to make your query efficient avoid the use of loading the relation in the transform function and and use with function of laravel to make it efficient

Getting specific value when using eloquent from Laravel

I am using Laravel 5.2 and I need to get specific values from the database with a leftjoin. The code I am using is as follow:
public function commentList(Request $request)
{
$inputs = $request->all();
$commentList = Comment::select(
'projects_comments.id as comment_id',
'u.name as user_name',
'projects_comments.comment as comment',
'projects_comments.created_at as created_at'
);
$commentList->leftjoin('users AS u', 'projects_comments.user_id', '=', 'u.id');
if (!empty($inputs['project_ids'])) {
$commentList->where(function ($query) use ($inputs) {
foreach ($inputs['project_ids'] as $i) {
$query->orWhere('projects_comments.project_id', $i);
}
});
};
$data = $commentList->get();
return $data;
}
It works fine but I would like to know if there is a better way to do this using eloquent but I can't really understand how to write this for eloquent to work. I need to get all the comments from an array of project ids.
I have the following model for Comment:
class Comment extends Model
{
protected $table = 'projects_comments';
public $timestamps = true;
protected $guarded = ['id'];
public function project()
{
return $this->belongsTo('App\Project', 'project_id');
}
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
}
I assume what you want is to get Comments (with their users) that belongs to specific Projects provided by the user as an array of IDS
Comment::whereIn('project_id', $inputs['project_ids'])->with('user')->get();
And if you only want the id and name of the user associated with the comment, pass the fields to the with function like so
Comment::whereIn('project_id', $inputs['project_ids'])
->with('user:id,name')->get();

Create new Post with default Category belongsToMany

I have a Post/Category manyToMany relations and would like to be able to attach a default category named "Uncategorised" to each new post that is created. How can I do that? A BelongsToMany method only works on the Details page, not on Create page.
BelongsToMany::make(__('Categories'), 'categories', Category::class),
You can also set default value to your database field so that you can omit passing category and will be taken default to Uncategorised like if you are using MySQL you can do it this way by creating migration
$table->text('category')->default(0);
Because the BelongsToMany not show on mode create in Post Nova model. So we have to make our custom Select, by add this code to your fields:
public function fields(Request $request)
{
if($request->editMode=="create"){
$categories = \App\Category::get(['id','name']);
$options = [];
foreach($categories as $value){
$options[$value->id] = $value->name;
}
return [
ID::make()->sortable(),
Text::make('Title'),
Text::make('Summary'),
Textarea::make('Content'),
Select::make('Categories', 'category_id')
->options($options)
->displayUsingLabels()
->withMeta(['value' => 1]) // 1 = id of Uncategorised in categories table
];
}
return [
ID::make()->sortable(),
Text::make('Title'),
Text::make('Summary'),
Textarea::make('Content'),
BelongsToMany::make('Categories','categories')->display('name'),
];
}
Don’t forget relationship function in both, Post and Category model:
class Post extends Model
{
public function categories(){
return $this->belongsToMany(Category::class, 'category_post', 'post_id', 'category_id');
}
}
And:
class Category extends Model
{
public function posts(){
return $this->belongsToMany(Post::class,'category_post', 'category_id', 'post_id');
}
}
Then, custom the function process the data on mode Create of Post resource page, it’s at nova\src\Http\Controllers\ResourceStoreController.php, change function handle to this:
public function handle(CreateResourceRequest $request)
{
$resource = $request->resource();
$resource::authorizeToCreate($request);
$resource::validateForCreation($request);
$model = DB::transaction(function () use ($request, $resource) {
[$model, $callbacks] = $resource::fill(
$request, $resource::newModel()
);
if ($request->viaRelationship()) {
$request->findParentModelOrFail()
->{$request->viaRelationship}()
->save($model);
} else {
$model->save();
// your code to save to pivot category_post here
if(isset($request->category_id)&&($resource=='App\Nova\Post')){
$category_id = $request->category_id;
$post_id = $model->id;
\App\Post::find($post_id)->categories()->attach($category_id);
}
}
ActionEvent::forResourceCreate($request->user(), $model)->save();
collect($callbacks)->each->__invoke();
return $model;
});
return response()->json([
'id' => $model->getKey(),
'resource' => $model->attributesToArray(),
'redirect' => $resource::redirectAfterCreate($request, $request->newResourceWith($model)),
], 201);
}
}
All runs well on my computer. A fun question with me! Hope best to you, and ask me if you need!
What I ended up doing was saving the data on Post Model in boot().
public static function boot()
{
parent::boot();
static::created(function (Post $post) {
$post->categories()->attach([1]);
});
}

Method addEagerConstraints does not exist in Laravel

I'm building a small application on Laravel 5.6 where I'm having a Company model where I am having a hasMany relation to model FinancialAndRisk something like this:
class Company extends Model {
use SoftDeletes;
protected $fillable = [
'name', 'slug', 'establishment', 'parent_id', 'website', 'updates'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'created_at','updated_at','deleted_at'
];
public function financial()
{
return $this->hasMany('Noetic\Plugins\Conxn\Models\Company\FinancialAndRisk', 'company_id');
}
public function latestFinancial()
{
return $this->hasMany('Noetic\Plugins\Conxn\Models\Company\FinancialAndRisk', 'company_id')->latest()->first();
}
}
Now at some places I want the latest financial report so I made a function latestFinancial
But when in my controller I do something like this:
public function index()
{
$companies = Company::with('latestFinancial')->get();
return response()->json(['companies' => $companies], 200);
}
I get an error:
{
"message": "Method Illuminate\\Database\\Query\\Builder::addEagerConstraints does not exist.",
"exception": "BadMethodCallException",
"file": "D:\\xampp\\htdocs\\conxn\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Query\\Builder.php",
"line": 2671,
How can I resolve this.
On your model you should define that you want an eager load with only One result, so instead of saying hasMany you should do hasOne: Also just use ->latest(); first() isn't necessary here
public function latestFinancial()
{
return $this->hasOne('Noetic\Plugins\Conxn\Models\Company\FinancialAndRisk', 'company_id')->latest();
}
Then it will only give you the latest record associated
The problem is ->first() because it executes the query. Remove it and use HasOne:
public function latestFinancial()
{
return $this->hasOne('Noetic\Plugins\Conxn\Models\Company\FinancialAndRisk', 'company_id')->latest();
}
The problem for me was that i was using ->get() at the end of the query.
I removed it and my query was fine i.e
formally i did
public function priceCategoryEntities()
{
return $this->hasOne((new AppUsersVipPriceCategoriesEntity), 'price_category_id', 'id')->get();
}
But Now am using
public function priceCategoryEntities()
{
return $this->hasOne((new AppUsersVipPriceCategoriesEntity), 'price_category_id', 'id');
}
and the error is gone.

How would I paginate these results in laravel?

I was reading this article to work out how to sort records in my database based on how many likes they have:
Laravel OrderBy relationship count
I came up with this which works:
$Book = Book::with('likes')->get()->sortByDesc(function($book_sort)
{
return $book_sort->likes->count();
});
Which is based upon this Book model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
public $timestamps = true;
protected $fillable = [
'title', 'author', 'category', 'featured', 'rating', 'description'
];
public function category()
{
return $this->hasOne('App\Cat', 'id', 'category_id');
}
public function likes()
{
return $this->belongsToMany('App\User', 'favourite_books')->withTimestamps();
}
public function total_likes()
{
return $this->likes()->count();
}
}
However now I am stuck on how I would paginate these results. Does anyone know?
Create manual pagination, try this:
$Book = Book::with('likes')->get()->sortByDesc(function($book_sort)
{
return $book_sort->likes->count();
});
$paginator = new Illuminate\Pagination\Paginator($Book, 10);
return view('pages.homepage', compact('paginator'))
sortBy() and sortByDesc() are working with collections only. orderBy() is working only with column name. Also, getting results, sorting and paginating them is a perfomance hit. It will also can eat all the memory.
So, the only solution I can see here is to use orderByRaw() or even raw query.

Resources