lundi 25 février 2019

Laravel Eloquent increment without updating timestamps

I have an Eloquent model on which I would like to increment a single attribute. So far I've been using the following line of code to achieve this:

Thread::where('id', $threadId)->increment('like_count');

This however has the unwanted side-effect of updating the updated_at timestamp. I've found the following way of updating a record without altering the timestamp:

    $thread = Thread::where('id', $threadId)->first();

    $thread->timestamps = false;
    $thread->like_count++;
    $thread->save();

But that suddenly looks a lot less concise. Therefore, I would like to know of there's a way to use the increment method without updating timestamps.



via Chebli Mohamed

Choosing between MVC and scalability pattern for Web App

I want to realize a web app that performs 2 principles action:

1 - It allows to some users (500 - 1000), USERS A, to upload posts in the app’s db. These posts have photos (with total weight about 5 MB) and the total number of these posts will be about 10K.

2 - It allows to many users (1K per day), USERS B, to research the posts uploaded by USERS A. At the research it’s possible insert advanced filters.

I want know if is necessary to dev the application in scalability way or if is possible to dev the app with the normal MVC pattern. To dev the app i want use Laravel and the app will be hosted by Amazon Server. What do you suggest me?



via Chebli Mohamed

Laravel map and filter getting wrong response

I have a eager loading relationship query.Like

$transactions = User::with(['point','children']->get();

Now I am adding a filter on my datatable

    ->addColumn('total', function($user){
        $sum = 0;
        $filtered = $user->map(function($user,&$sum) {
            $find = $user->children->filter(function($children) use($user) {
                return $children->id == $user->id;
            });
            $sum += $find->point->available;
            return $sum;
        });
        return $sum;
    })

Here if any user id matched with children id then sum of all mathced user available point value.

But I am getting error

[15:22:58] LOG.error: BadMethodCallException:
 Call to undefined method App\User::map() in



via Chebli Mohamed

How to create tilesets in mapbox

I wan to create my own tilesets in mapbox Like this http://a.tiles.mapbox.com/v4/descentas.aac5a694/page.html?access_token=pk.eyJ1IjoiY2FydGVzdXJtZXN1cmUiLCJhIjoiY2pydnZyeWJ0MDV2azQ5bnY5cnFtbGhzOSJ9.8B1cpphfANSsqR0VeIc87Q#3/31.13/-277.38

http://a.tiles.mapbox.com/v4/descentas.aac5a694/page.html?access_token=pk.eyJ1IjoiY2FydGVzdXJtZXN1cmUiLCJhIjoiY2pydnZyeWJ0MDV2azQ5bnY5cnFtbGhzOSJ9.8B1cpphfANSsqR0VeIc87Q#3/31.13/-277.38

But i don't know how can i create, also i did't get any help from anyone.

If you have any ideas please share.

other wise let me know how can i use styles as tilesets.



via Chebli Mohamed

How to clear primary key id generator in eloquent/mysql?

In my laravel 5.7 / mysql 5.7 app using eloquent I can clear rows in table using model :

Model::truncate();

But can I to clear primary key id generator, so first row would have value for id=1 ?

Thanks!



via Chebli Mohamed

Read the word document content from google drive

I am trying fetch content from google drive. i have used google sdk. problem is i used get() function for the retrieve the content. but it give only file metadata not any actual content. Please anyone help me on this?

$googl = new Googl(); 
    $this->client = $googl->client();
    $this->client->setAccessToken(session('user.token'));
    $this->drive = $googl->drive($this->client);

$file = $this->drive->files->get("fileId"); 
return $file;

please help me on this.



via Chebli Mohamed

Laravel - Trying to achieve preview option for an Excel file before submission, but it only works one specific way

I'm working with an Excel improtation function (Using maatwebsite/excel package) and I have that part covered, but I also need to have a function that'd allow apreview "sneak peek" before the file is uploaded.

I did manage to make it work (pic - https://imgur.com/rySv3Ck), but it ONLY works a certain way - code:

public function MakeImportPreview(Request $request)
{
    $dataPreview = Excel::toCollection(new ClientsImport(), $request->file('file'));

    $randomDataPreviews = $dataPreview->get(0)
        ->take(20)
        ->reject(function($row) {
            return
                !isset($row['name'])
                || !isset($row['email'])
                || is_null($row['name'])
                || is_null($row['email'])
                || empty($row['name'])
                || empty($row['email']);
        })
        ->toArray();


    return view('clients.preview', compact('randomDataPreviews'));
}

From the ClientsImportPreview class:

class ClientsImportPreview implements ToCollection, WithHeadingRow, WithLimit
{

public function collection(Collection $rows)
{
   return $rows->get(0)->reject(function($row) {
       return
           !isset($row['name'])
           || !isset($row['email'])
           || is_null($row['name'])
           || is_null($row['email'])
           || empty($row['name'])
           || empty($row['email']);
   })->take(20);
}


public function limit(): int
{
    return 20;
}

}

Basically, usage of reject and checking for emptiness/not being set was the only way I could make it work.

Is there a way to make this work by looking for the populated fields, as opposed to non-populated ones?

For example, if I try the following code instead:

public function MakeImportPreview(Request $request)
{
    $dataPreview = Excel::toCollection(new ClientsImport(), $request->file('file'));

    $randomDataPreviews = $dataPreview->get(0)
        ->take(20)
        ->map(function($row) {
            return
                isset($row['name'])
                || isset($row['email'])
                || !is_null($row['name'])
                || !is_null($row['email'])
                || !empty($row['name'])
                || !empty($row['email']);
        })
        ->toArray();


    return view('clients.preview', compact('randomDataPreviews'));
}



  class ClientsImportPreview implements ToCollection, WithHeadingRow, WithLimit
{

public function collection(Collection $rows)
{
   return $rows->get(0)->reject(function($row) {
       return
           !isset($row['name'])
           || !isset($row['email'])
           || is_null($row['name'])
           || is_null($row['email'])
           || empty($row['name'])
           || empty($row['email']);
   })->take(20);
}

I then get the following result -https://imgur.com/6iFKH4Y.

Any help is greatly appreciated. Thanks in advance!



via Chebli Mohamed