samedi 19 décembre 2020

Loop through Categories and its Products in PHP Laravel

I have to create an array with the categories array and its related products i'm fetching category with all related products using this query

  $categoryData= Category::with('product')->get();

Data is beign fetched properly, using this loop to get data in the format given below:

 foreach($categoryData as $row){
            $categoriesData[] = [
                'id' => $row->id,
                'title' =>  $row->title,
            ];
          foreach($row->product as $rowproduct){
            $categoriesData['product_details'][] = [
             'product_name' => $rowproduct->name,
             'product_price' => $rowproduct->price
      ];
}
}

Format (It should be something like this) :

{
    "categoriesData": {
        "0": {
            "id": 1,
            "category_name" : "Name 1",
        "product_details": [
            {
                "id": 1,
                "product_name": Product Name,
            },
        ],
        "1": {
            "id": 2,
            ""category_name" ": "Name 2",
            "product_details": [
            {
                "id": 1,
                "product_name": product name,
            },
        },
      
   

but through present loop all product is getting saved in one array. It is not saving inside category.

Any help is highly appreciated



via Chebli Mohamed

vendredi 18 décembre 2020

Laravel pagination showing duplicate records

I am using laravel pagination in the query i am using order by with a column and the multiple rows has the same value for this column so when i change the page via pagination some records repeats. Is there any solution to fix this problem.



via Chebli Mohamed

not working in laravel

I am displaying images in laravel blade with this method

<img class="docu-image" src="" alt="Second slide">

on localhost it is working fine but on live server it isn't working

When add public in url by inspect element then it shows image .

What could be the reason



via Chebli Mohamed

"Interface 'Illuminate\Contracts\Auth\Access\Gate' not found" I can't figure out what is wrong here

So basically I'm building an application using Laravel 5.6, under php 7.1.2, and I got this error:

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) Interface 'Illuminate\Contracts\Auth\Access\Gate' not found

So obviously I checked the incriminated file and stumbled on this piece of code which I don't understand in Illuminate\Contracts\Auth\Access\Gate.php

use Illuminate\Contracts\Auth\Access\Gate as GateContract;

class Gate implements GateContract {

I don't understand why the file is using itself, and why the Gate class is trying to implement itself despite the fact that it's defined as a class and not an interface. Thus I don't understand how I am supposed to solve this mess.

If anyone could shed some light on this I would be grateful.



via Chebli Mohamed

Aws db name is displaying (-) .what should i give in my laravel project?

I have created db on aws rds all the other infor mation is available but db_name is displaying ``(-)``` I dont know how i should give it in my laravel env file

env

DB_DATABASE=-

enter image description here



via Chebli Mohamed

Laravel many to many relationship need to execute a query to get users list where role_id = 5

I have the below tables

  1. users : id | name
  2. projects : id | name
  3. user_project : id | user_id | project_id

My Models contains

  1. User.php

    public function projects() { return $this->belongsToMany(Project::class); }

  2. Project.php

    public function users() { return $this->belongsToMany(User::class); }

In my controller file, I want to get a list of users using the User model whose role_id is 5.

Something like below

User::query()->roles(5);

*Help will be appreciated



via Chebli Mohamed

Running commands from Controller async

There is a migration task. User uploads file to the server, then it should be saved and migration command should be run async. The first path works well, there is an issue with the second part. I've tried to put all code to console command and run it with

Artisan::call('user:migrate', ['user_id' => $userId]);

or

Artisan::queue('user:migrate', ['user_id' => $userId]);

the script works, but not async, controller's function waits for the end. Also I've tried to create a Job and call it via:

$this->dispatch(new UserMigration($user));

and had the same result, script works but not async. Please help to realize how queues work and that approach is better for my task. I've not created any queue migrations and configuration, because need this step just async calling.



via Chebli Mohamed