mardi 3 août 2021

How to get prducts category wise in laravel?

Similar question was asked here

I am working on apis. My products table contain more than ten thousand products of different categories. I need to decrease query execution time so for that purpose I've to modify an api that it will fetch only 20 products of each category and group the whole api response category wise. Api response will be

{
"data": {
"products": {
  "Beauticians & Style": [
    {
      "Title": "Product A",
      "Price": "0.00",
      "DiscountPrice": 0
    },
    {
      "Title": "Product B",
      "Price": "0.00",
      "DiscountPrice": 0
    }
  ],
  "Groceries": [
    {
      "Title": "Product G",
      "Price": "0.00",
      "DiscountPrice": 0
    },
    {
      "Title": "Product R",
      "Price": "0.00",
      "DiscountPrice": 0
    },
    {
      "Title": "Product O",
      "Price": "0.00",
      "DiscountPrice": 0
    },
    {
      "Title": "Product C",
      "Price": "0.00",
      "DiscountPrice": 0
    }
  ],
  "Women's Fashion": [
    {
      "Title": "Product W",
      "Price": "0.00",
      "DiscountPrice": 0
    },
    {
      "Title": "Product O",
      "Price": "0.00",
      "DiscountPrice": 0
    },
    {
      "Title": "Product M",
      "Price": "0.00",
      "DiscountPrice": 0
    }
  ]
}
}

My Controller's code

$products= Category::whereHas('products', function($q){
                $q->take(20);
            })->get();

Category Model

class Category extends Model
{
    use HasFactory;
    public function products()
    {
        return $this->hasMany('App\Models\Product', 'CategoryID', 'CategoryID');
    }
}

I've tried this but not getting the exact resulrt. Anyone help me.



via Chebli Mohamed

How to stop tinyMCE 5 to add ../../../../ before an absolute url received from alexusmai filemanager?

I am using Laravel 8, tinyMCE 5 and Alexusmai filemanager. My images are in the /public/assets/images folder. The 'assets' disk is setup in config/filesystems.php :

    // assets folder in public path
    'assets' => [
        'driver' => 'local',
        'root' => public_path('assets'),
        'url' => '/public/assets',
        'visibility' => 'public',
    ]

FM returns the correct relatrive url, i.e. public/assets/images/xxx.jpg in a form field as a stand alone.

In tinyMCE 5, the same url is returned to the editor (the image is visible) but as soon as I look in the source3 code, the img scr is replaced by "../../../../public/assets/images/xxx.jpg". Why? How can I stop tiny to do that?



via Chebli Mohamed

lundi 2 août 2021

How to convert non readable url to readable url?

enter image description here

I have a page of the product list with some options. One of the options is Category. When I click on Kid's category then the url like this 'http://localhost/netshop/productList?cat=Kid%27s&brand=&type=&price=&off=&pagi=50'.

When try to check the url using url()->current() method get the above url. Where 'cat=Kid%27s' is non-readable. I want to readable url. How to solve this problem?



via Chebli Mohamed

selectRaw changing return result conditionally

Say if I have gender stored as intergers, how to format gender as string?

->selectRaw('
  User.email as "Email",
  User.gender as "Gender"
')


via Chebli Mohamed

Laravel converts integer to NULL when returning Model as JSON

This is the weirdest mistake I've ever seen in my life, I don't know how to explain it, but it's happening.

When returning a list of models from the controller:

public function index()
{
     return MyModel::orderBy('name')->get();
}

I'm getting the following feedback in the browser: return with error

Now if I switch to:

public function index()
{
     return MyModel::orderBy('name')->get()->toArray();
}

I get the following feedback in the browser:

expected return

After trying to find a solution so that I don't need to call the toArray() function, I found that by overriding the jsonSerialize() method as in the example below, in the MyModel class, it correctly converts the cost_id to a number.

public function jsonSerialize()
{
    return parent::jsonSerialize();
}

If I put the field as string in the $casts, it works, but converts the numbers to string.

My Model:

enter image description here

My backend:

  • PHP 5.6.40
  • Laravel 5.4
  • Database is DBMaker


via Chebli Mohamed

Laravel eloquant 'whereDoesntHave' get posts not only commented by one user

I need to get the posts, without the posts which only commented by specific user.

Relation in Post model,

public function comments(){
  return $this->hasMany(Comment::class);
}

This is my code,

public function getPostsWithoutCommenter(){
  $userId = 1;
  $posts = \App\Post::whereDoesntHave("comments", function($subQuery) use($userId){
    $subQuery->where("user_id", "=", $userId);
  })->get();
}

This code omits 'Posts which a specific User has commented'. But I want to "omit posts Only that user commented". Which means if some other user also commented I want that in resulset. Is there an eloquant way to do this?



via Chebli Mohamed

Laravel Make weekly Dynamic Json Array

I have 2 dates like start date and end date and one string like 2w2,1w1,3w3,4w3

In this 2w2 => first 2 means 2 class in 1 week and second 2 class in 2nd week, which means 2 classes till 2 weeks, if 1w1 then 1 class in 1 week,3w3 then 3 class till 3 weeks. now I want to make JSON array likes, week start date, weekend date, and frequency according to that week, Please suggest to me how to make it.

 $week_array = [{"startdate":"2021-07-18","enddate":"2021-07-24","frequency":2},
                {"startdate":"2021-07-25","enddate":"2021-07-31","frequency":2},
                {"startdate":"2021-08-01","enddate":"2021-08-07","frequency":1},
                {"startdate":"2021-08-08","enddate":"2021-08-14","frequency":3}]

Week starts from Sunday to Saturday.



via Chebli Mohamed