Laravel. Exporting results from database problems - php

As short as possible. My code runs through multiple databases counts objects and matches name - number of objects
It runs like a script(command in laravel) that exports the results in .csv file.
$formatted_data = array();
$providers = provider::where('del', 'no')->get();
foreach($providers as $provider){
$formatted_data[$provider['id']]['name'] = $provider['name'];
}
$objectMappingsModels = array((new objectMapping1), (new objectMapping2),
(new objectMapping3), (new objectMapping4), (new objectMapping5),
(new objectMapping6), (new objectMapping7), (new objectMapping8));
foreach($objectMappingsModels as $objectMappingsModel){
$totals = $objectMappingsModel::select('providerFK', DB::raw('count(*) as total'),
DB::raw('monthName(ut) as month_name')
)
->where('userFK', '!=', 1)
->where('del', 'no')
->whereMonth('ut', $this->option('month'))
->whereYear('ut', $this->option('year'))
->groupBy('providerFK', 'month_name')
->get()
->toArray();
foreach($totals as $total){
$formatted_data[$total['providerFK']]['count'] = $total['total'];
}
}
$responce = Excel::store(new UsersExport($formatted_data), 'testNameDate.csv',null, \Maatwebsite\Excel\Excel::CSV);
return true;
That's what my code looks like.
class UsersExport implements FromArray
{
protected $invoices;
public function __construct(array $invoices)
{
$this->invoices = $invoices;
}
public function array(): array
{
return $this->invoices;
}
}
And that's what my export class looks like.
Unfortunately, it's not working flawlessly. It gives me wrong results from time to time. Some records a correct and some are not. Also, the last row is always with just a random number(without a name attached to it). Any idea why such problems occur and can you suggest me some code optimizations?
Thanks!

I added this if-else in the last loop
if(isset($formatted_data[$total['providerFK']]['count'])){
$formatted_data[$total['providerFK']]['count'] += $total['total'];
}else{
$formatted_data[$total['providerFK']]['count'] = $total['total'];
}
And it seems to have fixed some issues

Related

Passing a variable to a query creates the error "Trying to get property 'name' of non-object" with Laravel

I am using Laravel 6. I am trying to create a validation system with a form to create a meeting.
When a user creates a meeting with participants that are already occupied in another meeting, a message should appear in the view with the name of the participants already occupied.
For some reason the function that should find the name of the participants doesn't work. I pass an id during a foreach loop but when I run the form appears the following message: "Trying to get property 'name' of non-object".
The strange thing is that the id passed to the function are OK, but if I write a number (for example "8") in place of $id in the query appears correctly the name "Chris" in the view.
The format of the column "id_participants" in the table meetings is the following "23;7;6".
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use DB;
use App\User;
class CheckParticipant implements Rule
{
protected $participants_occupied = array();
/**
* Create a new rule instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
$participants = request('participants');
foreach($participants as $participant) {
$meetings = DB::table('meetings')
->where('is_active', '1')
->where('date', request('date_meeting'))
->where(function ($query) {
$query->where(function($sub_q) {
$sub_q->where('start_hour', '>=', request('start'))
->where('start_hour', '<', request('end'));
})
->orWhere(function($sub_q) {
$sub_q->where('start_hour', '<', request('start'))
->where('end_hour', '>=', request('end'));
})
->orWhere(function($sub_q) {
$sub_q->where('end_hour', '>', request('start'))
->where('end_hour', '<=', request('end'));
});
})
->where(function ($query) use($participant) {
$query->where('id_participants', $participant)
->orWhere('id_participants', 'like', '%;'.$participant)
->orWhere('id_participants', 'like', $participant.';%')
->orWhere('id_participants', 'like', '%;'.$participant.';%');
})
->get();
if(count($meetings) > 0) {
array_push($this->participants_occupied, $participant);
}
}
if(count($this->participants_occupied) > 0) {
return false;
} else {
return true;
}
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
for($i = 0; $i < count($this->participants_occupied); $i++) {
$this->participants_occupied[$i] = $this->getNameSurnameById($this->participants_occupied[$i]);
}
return 'The participants are already occupied at that time: ' . implode(',', $this->participants_occupied);
}
public function getNameSurnameById($id)
{
$users = User::all()->where('id', 18)->first(); //if I write a number in place of $id everything works
return $users->name;
}
}
I would like that this program works dynamically. I suppose there is something wrong in the query with the variable $id. Is someone able to help me?
UPDATE:
I solved the problem modifying the message function as follows:
public function message()
{
$arr_names = array(); //I created this array
for($i = 0; $i < count($this->participants_occupied); $i++) {
array_push($arr_names, $this->getNameSurnameById($this->participants_occupied[$i]));
}
return 'The following participants are already occupied at that time: ' . implode(', ', $arr_names);
}
I suppose that the problem consisted that I gave a string value (the name of the participant) to an array that had integers values (The id of the participant). I solved creating a new empty array and I pushed the names to the new array.
You may find it much easier to grab your ids based on some type of Laravel object, rather than an array. I suspect that the array has an incorrect value (not an id) at the index of $i during the loop at some point. And, as pointed out in the comments by #Cristóbal Ramos Merino, you are setting the variable to a potential string (the user name) at the same time as you are trying to pass the possible id through to the getNameSurnameById() method.
I would grab all of the ids passed from the form, do a DB query on User to see who is already occupied, and then just pull the name from the resulting collection.
Something like:
$allFormUsers = User::whereIn('id', $formIds)->get();
Then loop on this to get the names of those occupied:
$occupiedNames = [];
foreach($AllFormUsers->where('occupied', 1) as $u){
$occupiedNames[] = $u->name;
}
I have no idea how you are tracking the occupied - and so the above code is little more than pseudo code, but hopefully will give you an idea of how to do this without the array / concurrency. This also is a little less work on the Database, since you have one query, instead of looping on individual queries each time. You can even pull all users first so you have them stored, and then do a where('occupied', 1) against the collection if you like, as in the above loop. (Assuming that's how you track occupied)

laravel chunk (help)

when i using postman get the result from chunk,but the result will return empty,how can i solve this?
enter image description here
here's my code
public function downloadMemberInfo()
{
error_log('download');
set_time_limit(240); //testing
$memberListsArray = array();
Member::select('vipcode')->where('vipcode','!=','')
->chunk(3000,function($members) use($memberListsArray){
foreach($members as $member){
$memberListsArray[] = $member;
}
});
return response()->json($memberListsArray);
}
You need to call get before use chunk; because chunk works with collections. Try with the next code.
public function downloadMemberInfo()
{
error_log('download');
set_time_limit(240);
$members = Member::select('vipcode')
->where('vipcode', '!=', '')
->get()
->chunk(3000)
->toArray();
return response()->json($members);
}
By the way, I recommend you to use paginate or some query limit to avoid performance issues

How to test Laravel Scout (with Algolia)?

I have a piece of code like this:
public function index(Request $request, Runner $runnerParam)
{
$name = $request->input('name');
$fromDate = $request->input('from_date');
$toDate = $request->input('to_date');
$runners = Runner::query();
if ($name) {
$runners = $runnerParam::search($name);
}
if ($fromDate && $toDate) {
$runners->where('created_at', '<=',$toDate )
->where('created_at', '>=', $fromDate);
}
switch ($type) {
case 1:
$runners->where('role', '=', runner::PRO);
break;
case 2:
$runners->where('role', '=', runner::AMATEUR);
break;
}
$runners = $runners->get();
foreach($runners as $runner){
$runner->distance = $runner->stats->sum('distance');
}
return $runners;
}
The question is, how do I write test for this? If I just try to provide 'name' in test, it will return nothing like search() function isn't working at all while testing. Tried really hard to find anything on this, but the info is scarce and I only ended up with something like 'set Algolia driver to null', which I managed to do, but to no effect since I don't know what's the point of doing so and how do you apply it in tests. There are absolutely no examples of successful tests out there, just a few questions with short answer that didn't quite help.
A piece of test:
public function testNameFilter()
{
$this->logIn();
$runners = factory(runner::class, 30)->create();
$name = $runners[0]->name;
$response = $this->json('get', route('api::runners.get'), ['name' => $name]);
$responseContent = $response->getContent();
...
}
So, what I get in the end is empty responseContent, which means this is not the right way to test this. Any thoughts?
Why not just test that you've properly configured your class to use Laravel Scout, vs. testing that Laravel Scout works as expected?
public function class_uses_scout()
{
$this->assertTrue(in_array('Laravel\Scout\Searchable', class_uses('App\FooModel')));
}
public function class_has_searchable_array()
{
// compare the searchable array with a hardcoded array here
}
Be sure to set your disable Laravel Scout in your test environment.

Laravel detect if there is a new item in an array

I want to implement a system in my project that "alerts" users when there is a new comment on one of their posts.
I currently query all comments on the posts from the logged in user and put everything in an array and send it to my view.
Now my goal is to make an alert icon or something when there is a new item in this array. It doesn't have to be live with ajax just on page load is already good :)
So I've made a function in my UsersController where I get the comments here's my code
public function getProfileNotifications()
{
$uid = Auth::user()->id;
$projects = User::find($uid)->projects;
//comments
if (!empty($projects)) {
foreach ($projects as $project) {
$comments_collection[] = $project->comments;
}
}
if (!empty($comments_collection)) {
$comments = array_collapse($comments_collection);
foreach($comments as $com)
{
if ($com->from_user != Auth::user()->id) {
$ofdate = $com->created_at;
$commentdate = date("d M", strtotime($ofdate));
$comarr[] = array(
'date' => $ofdate,
$commentdate,User::find($com->from_user)->name,
User::find($com->from_user)->email,
Project::find($com->on_projects)->title,
$com->on_projects,
$com->body,
Project::find($com->on_projects)->file_name,
User::find($com->from_user)->file_name
);
}
}
} else {
$comarr = "";
}
}
Is there a way I can check on page load if there are new items in the array? Like keep a count and then do a new count and subtract the previous count from the new one?
Is this even a good way to apprach this?
Many thanks in advance! Any help is appreciated.
EDIT
so I added a field unread to my table and I try to count the number of unreads in my comments array like this:
$uid = Auth::user()->id;
$projects = User::find($uid)->projects;
//comments
if (!empty($projects)) {
foreach ($projects as $project) {
$comments_collection[] = $project->comments;
}
}
$unreads = $comments_collection->where('unread', 1);
dd($unreads->count());
But i get this error:
Call to a member function where() on array
Anyone any idea how I can fix this?
The "standard" way of doing this is to track whether the comment owner has "read" the comment. You can do that fairly easily by adding a "unread" (or something equivalent) flag.
When you build your models, you should define all their relationships so that stuff like this becomes relatively easy.
If you do not have relationships, you need to define something like the following:
In User
public function projects()
{
return $this->hasMany('App\Models\Project');
}
In Project
public function comments()
{
return $this->hasMany('App\Models\Comment');
}
Once you hav ethose relationshipt, you can do the following. Add filtering as you see fit.
$count = $user->projects()
->comments()
->where('unread', true)
->count();
This is then the number you display to the user. When they perform an action you think means they've acknowledged the comment, you dispatch an asynchronous request to mark the comment as read. A REST-ish way to do this might look something like the following:
Javascript, using JQuery:
jQuery.ajax( '/users/{userId}/projects/{projectId}/comments/{commentId}', {
method: 'patch'
dataType: 'json',
data: {
'unread': false
}
})
PHP, in patch method:
$comment = Comment::find($commentId);
$comment->update($patchData);
Keep in mind you can use Laravel's RESTful Resource Controllers to provide this behavior.
try this
$unreads = $project->comments()->where('unread', 1);
dd($unreads->count());
EDIT
My be Has Many Through relation will fit your needs
User.php
public function comments()
{
return $this->hasManyTrough('App\Project', 'App\Comment');
}
Project.php
public function comments()
{
return $this->hasMany('App\Comment');
}
then you can access comments from user directly
$user->comments()->where('unread', 1)->count();
or I recommend you define hasUnreadComments method in User
public function hasUnreadComments()
{
$return (bool) $this->comments()->where('unread', 1)->count();
}
P.S.
$uid = Auth::user()->id;
$projects = User::find($uid)->projects;
this code is horrible, this way much better
$projects = Auth::user()->projects;

Create like/unlike functionality with Laravel

I have a list of properties for a real estate application and im trying to implement a like/unlike functionality based on each property detail. The idea is to add a like or remove it matching the current property and user. This is my code so far, but it only remove likes so it doesnt work as expected. If anyone can suggest for a better approach ill be appreciated.
//Controller
public function storeLike($id)
{
$like = Like::firstOrNew(array('property_id' => $id));
$user = Auth::id();
try{
$liked = Like::get_like_user($id);
}catch(Exception $ex){
$liked = null;
}
if($liked){
$liked->total_likes -= 1;
$liked->status = false;
$liked->save();
}else{
$like->user_id = $user;
$like->total_likes += 1;
$like->status = true;
$like->save();
}
return Redirect::to('/detalle/propiedad/' . $id);
}
// Model
public static function get_like_user($id)
{
return static::with('property', 'user')->where('property_id', $id)
->where('user_id', Auth::id())->first();
}
// Route
Route::get('store/like/{id}', array('as' => 'store.like', 'uses' => 'LikeController#storeLike'));
#Andrés Da Viá Looks like you are returning object from model. In case there is no data in database, it will still return an object - so far my guessing. Can you do something like below in the if($liked){ code?
Try this instead:
if(isset($liked -> user_id)){
Also try to print $liked variable after try and catch blocks. Use var_dump.
If this still does not work for you then let me know. I will try to create code based on your question.
Fix it by adding a where clause in my model to make the status equal to True ->where('status', 1)->first();

Categories