How do I fetch comments with Laravel Pagination that are not redundant?

My main problem:

If user writes a comment and then decides to load more comments, he will get to see at least one redundant comment.

Causation:

Via Laravel Pagination the script loads via Axios the last 10 items. After adding one comment and fetching again the next 10 items, the “order” is shifted of one element. Thus, getting one repeated element.


Tries to fix it:

First Try:
I tried with Array.prototype.includes() or Lodash Includes:

 data(){
    return { items: [] };
 }
 ...
 addWithoutRedudancy(newItems){
    for(let item of newItems){
        if( ! _.includes( this.items, item ) ) this.items.push(item);
    }
 }

Occurring problems:

  1. Even though I seemingly have two of the same objects, I get in my console false if I check:

    $vm0.items[10] == $vm0.items[11] //False even though objects look the same, and actually are (looking to all properties)

    That is a huge problem, because Includes can’t find the redundant Object! I’ve no clue why these objects are saved differently. Perhaps because I fetch them over axios on two different timings?

  2. But even if it would work, after the user wrote for example 8 new comments, he would get only 2 new comments from the pagination (controller) back, but should get of course the next 10 comments.

Second Try

/**
 * Fetch all relevant comments.
 *
 * @param string $username
 * @param Selllink $selllink
 */
public function index($username, $selllink)
{
    //Route::prefix('/{username}/{selllink}
    //Sellink gets the model of sell and loads all comments to that sell.
    //get me the latest first (we want to see the current comments)
    //take only 10 coments with paginatation

    return $selllink->sell->comments()
                ->latest()
                ->offset($addedCommentsNumber??) //that can't work...
                ->paginate(10);
}

Occurring problems:
I cannot really offset the whole table. I need to offset the paginated ones and add the missing ones again.


Conclusion

There must be a much better way to do this. But I just don’t know how.


More details explanation (if needed):

I have a Vue Comment Component that fetches the data from my CommentController with Pagination:

methods: {
        fetch(){
            axios.get(this.url()).then(this.refresh);
        },
        url() {
            this.page++;
            return `${location.pathname}/comments?page=${this.page}`;
        },
        refresh({data}){
            this.dataSet = data;
            this.addWithoutRedudancy(data.data);
        },
}

If the users want to get more Comments, they just click on the “load more” button and I’m getting the comments loaded

model_function on backpack CRUD

Since I want my data to be encrypted in my database, how can I get this to be done using a model_function? I’ve manually encrypted prior to trying to do this and attempting to show the decrypted result on my edit page but to no avail?

I’ve currently got the following code for my controller:

$this->crud->addField([
    'name' => 'password',
    'label' => 'Password',
    'model_function' => 'decryptPassword'
]);

And this for my model:

public function decryptPassword() {
    return decrypt($this->password);
}

How to validate this context? In the validate method()?

Context: When a user creates a new conference he needs to select a start and an end date for the conference and the user needs to enter an end date after the start date, that is, the end date needs to be after the start date.

To deal with this logic properly, do you know if we can use the laravel validate method in the store method or is not a good approach or not possible do with this method? If it is not adequate to do this in the validate method do you know how to properly do that?

ConferenceController store method:

public function store(Request $request)
 {
        $this->validate($request, [
            'conference_name' => 'required|max:255|string',
            'conference_startDate' => 'required|date_format:d/m/Y',
            'conference_endDate' => 'required|date_format:d/m/Y',
        ]);
        $conference = Conference::create([
            'nome' => $request->conference_name,
            'startDate' => Carbon::parse($request->conference_startDate),
            'endDate' => Carbon::parse($request->conference_endDate),
        ]);
    }

Html fields:

<div class="form-row">
    <div class="form-group col-md-6">
        <label for="conference_startDate">Stard Date</label>
        <div class="input-group date" data-provide="datepicker">
            <input type='text' onkeydown="event.preventDefault()"
                   name="conference_startDate" value="{{ old('conference_startDate') }}"
                   class="form-control" placeholder="DD/MM/YYY" />
            <span class="input-group-addon"><i class="fa fa-calendar text-primary" aria-hidden="true"></i></span>
        </div>
    </div>
    <div class="form-group col-md-6">
        <label for="conference_endDate">End Date</label>
        <div class="input-group date" data-provide="datepicker">
            <input type='text' class="form-control"
                   value="{{ old('conference_endDate') }}" name="conference_endDate" placeholder="DD/MM/YYY"/>
            <span class="input-group-addon"><i class="fa fa-calendar text-primary" aria-hidden="true"></i></span>
        </div>
    </div>
</div>

Proxied laravel ( with cors enabled ) breaks sessions

I am using laravel to create a restAPI and I came across a problem. I am developing the front-end in a different port and so I had to use a proxy to get to the backend. More specifically I am using the angular’s cli to create the proxy. I have installed barryvdh/laravel-cors module to get rid of cors errors but I came accross a problem. Everytime I am hitting a backend route through the proxy, a new session is created. What could be the problem? What is the correct way to tell laravel to ignore origin header? The problem is not there when I am using postman since it does not include this header and gets trusted by the backend.

Best practice in deleting records in Laravel? PhpStorm throws warning

When writing the following code in PhpStorm it warns “Unhandled \Exception”:

Flight::where('flight_number', $flight_number)->delete();

This is pretty much exactly how it’s documented on Laravel’s website. I don’t believe this throws an error if no records are found to be deleted?

Should this really be wrapped in a try/catch, with an empty catch since it’s not really an error? Especially since in my instance it’s fine if no records are found to be deleted.

Convert MySQL Query to Laravel query

I would to convert mysql query to laravel query.

MySQL:

SELECT * FROM scadenze WHERE created_at IN(SELECT MAX(created_at) FROM scadenze GROUP BY processo_id)

Laravel ( i got an error, Object of class stdClass could not be converted to string):

DB::table('scadenze')->whereIn('created_at', DB::select("( SELECT MAX(created_at) FROM scadenze GROUP BY processo_id )"))->get();

Laravel 4.2 getting error array_merge(): Argument #2 is not an array

I am having a trouble with my project, It shows the error array_merge(): Argument #2 i have tried using composer update but the update do not work because it shows the same error in the console, i don’t know what cause the problem, i have not touched it and anyone else should be working on it, so if any help would be thankfull.

im using laravel 4.2 this is the error stack

6. ErrorException
…/­vendor/­laravel/­framework/­src/­Illuminate/­Foundation/­ProviderRepository.php188
5. Illuminate\Exception\Handler handleError
<#unknown>0
4. array_merge
…/­vendor/­laravel/­framework/­src/­Illuminate/­Foundation/­ProviderRepository.php188
3. Illuminate\Foundation\ProviderRepository loadManifest
…/­vendor/­laravel/­framework/­src/­Illuminate/­Foundation/­ProviderRepository.php50
2. Illuminate\Foundation\ProviderRepository load
…/­vendor/­laravel/­framework/­src/­Illuminate/­Foundation/­start.php210
1. require
…/­bootstrap/­start.php60
0. require_once
/­home/­{myproject}/­public_html/

The error does not come from a code i write myself, but from the core of laravel in /­vendor/­laravel/­framework/­src/­Illuminate/­Foundation/­ProviderRepository.php188 i have read that the service.json file maybe corrupted, i have deleted it and try to run composer update but the error persist and the update does not run

Laravel5.5 redirect back() not working

I have a Laravel application that i have been working on for awhile , the problem i am facing now is that when validation fails on any form including the default registration and login forms or ust a redirect back after any process, the site redirects to ‘/’ (the home route) instead of redirecting back to the previous page. i have tried clearing cache but that didn’t help.

The application works fine on local machine, also i have a middleware that enforces secure urls on production environment

if (!$request->secure() && env('APP_ENV') === 'production') { return redirect()->secure($request->getRequestUri()); }