With or without whereHas, two queries will be run. One to select all users another one to load the related models. When you add the whereHas condition a subquery is added, but it's still two queries.

However, syntactically you could optimize this a bit by adding a query scope to your model (or even a base model if you want to use this more often):

public function scopeWithAndWhereHas($query, $relation, $constraint){
return $query->whereHas($relation, $constraint)
->with([$relation => $constraint]);
}

Usage:

User::withAndWhereHas('submissions', function($query) use ($id){
$query->where('taskid', $id);
})->get();

If you don’t want to add a scope, like above you can do something like this as well

$callback = function($query) {
$query->where('something', '=', 'something');
}
$submissions = Post::whereHas('submissions', $callback)->with(['submissions' => $callback])->get();

Magento is a robust e-commerce platform, powering big brands to get the most out of their online business. It is loaded with a large number of useful features that help store owners to enhance their online store’s performance, and to give their customers an incredible web shopping experience.

Minimum requirements: PHP 7.1.3+, MySQL 5.6+, 2GB+ of RAM. For more details check out this link.

Install Magento using Composer

You can install Magento in two ways:

  1. Download them Magento and install it manually
  2. Install using Composer.

Whichever route you choose, you will need to register to install Magento. click here to register.

composer create-project --repository=https://repo.magento.com/ magento/project-community-edition…

  1. you get following error when you do
php artisan cache:clear
“Failed to clear cache. Make sure you have the appropriate permissions.”

Solution: create a folder manually storage/framework/cache/data.

You need to manually create data folder inside storage/framework/cache/


Form inputs along with multiple image upload using Dropzone

Today I had to upload multiple images as a field along with other inputs. Most of the tutorials found online have the implementation of dropzone as the only item in the form. But I needed to implement dropzone with an existing form.

For brevity, I have included two input fields and a dropzone.

Frontend Code:

Frontend Implementation of form with dropzone

Backend implementation:

// packagesController@store
public function
store(Request $request)
{
$input = $request->all();

$package = Package::create($input);
if($request->has('file')){
foreach($request->file as $photo){
$filename = $photo->storeAs('public/uploads/packages/' . $package->id, $photo->getClientOriginalName());
$package->images()->create(['name' => $filename]);
}
}

Caveat:

If the form has validation error, you need to remove images and add them again.


You need to append request (as array) in the result before printing the pagination links

{{$result->appends(request()->toArray())->links()}}

$result is search result.

You can also filter out things you don’t want to send:

{{$result->appends(request()->except(['page', '_token']))->links()}}

Here are simple solutions to the errors thrown by laravel.

  • In project folder, you should have .env file. If you don’t rename .env.example to .env and run artisan key:generate command.
mv .env.example .envphp artisan key:generate
  • Also check if you have autoload.php in vendor folder
composer install

OR

composer update
  • provide write access to storage folder.
chmod -R 775 storage

amrit shrestha

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store