Inserción de registros en la tabla de colores
Comprar curso

12. Inserción de registros en la tabla de colores

En este módulo, aprenderás cómo insertar registros en la tabla de colores de tu base de datos. Desde la creación de los colores necesarios, hasta la implementación de los mismos en tu aplicación.


5 comentarios

Inicia sesión para comentar

Comentarios:

  • Salvador Castillo

    Salvador Castillo hace 1 mes

    public function run(): void
    {
       $products = Product::whereHas('subcategories', function (Builder $query){ //Metodo nos permite hacer consulta las relaciones
            $query->where('color',true)
                    ->where('size',false);
        })->get();

     

       BadMethodCallException

     Call to undefined method App\Models\Product::subcategories()
     

    Me manda ese error. buenos dias podrian ayudarme Victor

  • Luis Alberto Garcia Orozco

    Luis Alberto Garcia Orozco hace 1 mes

    Hola tengo este error   
    SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.sub_category_id' in 'where clause' (SQL: select * from `products` where exists (select * from `sub_categories` where `products`.`sub_category_id` = `sub_categories`.`id` and `color` = 1 and `size` = 0))        

     at C:\wamp64\www\E-CommercePrueba\PruebaECommerce\vendor\laravel\framework\src\Illuminate\Database\Connection.php:712
       708▕         // If an exception occurs when attempting to run a query, we'll format the error
       709▕         // message to include the bindings with SQL, which will make this exception a
       710▕         // lot more helpful to the developer instead of just the database's errors.
       711▕         catch (Exception $e) {
     ➜ 712▕             throw new QueryException(
       713▕                 $query, $this->prepareBindings($bindings), $e
       714▕             );
       715▕         }
       716▕     }

     • A column was not found: You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`.
       https://laravel.com/docs/master/migrations#running-migrations

     1   C:\wamp64\www\E-CommercePrueba\PruebaECommerce\vendor\laravel\framework\src\Illuminate\Database\Connection.php:368
         PDOException::("SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.sub_category_id' in 'where clause'")

     2   C:\wamp64\www\E-CommercePrueba\PruebaECommerce\vendor\laravel\framework\src\Illuminate\Database\Connection.php:368
         PDO::prepare("select * from `products` where exists (select * from `sub_categories` where `products`.`sub_category_id` = `sub_categories`.`id` and `color` = ? and `size` = ?)")

     

    no se porque me esta tomando como nombre de columna sub_category_id si en la base de datos lo tengo como subcategory_id

  • Jose Alfredo Mendoza Chirinos

    Jose Alfredo Mendoza Chirinos hace 3 meses

    Hola, me sale el siguiente error :(

     

     

     

    SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null (SQL: insert into `color_product` (`color_id`, `product_id`, `quantity`) values (1, ?, 10), (2, ?, 10), (3, ?, 10), (4, ?, 10))

     at C:\Game\www\ecomerce-snap\vendor\laravel\framework\src\Illuminate\Database\Connection.php:712
       708▕         // If an exception occurs when attempting to run a query, we'll format the error
       709▕         // message to include the bindings with SQL, which will make this exception a
       710▕         // lot more helpful to the developer instead of just the database's errors.
       711▕         catch (Exception $e) {
     ➜ 712▕             throw new QueryException(
       713▕                 $query, $this->prepareBindings($bindings), $e
       714▕             );
       715▕         }
       716▕     }

     1   C:\Game\www\ecomerce-snap\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
         PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null")    

     2   C:\Game\www\ecomerce-snap\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
         PDOStatement::execute()

     

  • Zangetsu

    Zangetsu hace 3 meses

    Buenas tengo este error al ejecutar el seeder

     

     SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.subcategory_id' in 'where clause' (SQL: select * from `products` where exists (select * from `sub_categories` where `products`.`subcategory_id` = `sub_categories`.`id` and `color` = 1 and `size` = 0))

     at C:\laragon\www\ecomer\vendor\laravel\framework\src\Illuminate\Database\Connection.php:760
       756▕         // If an exception occurs when attempting to run a query, we'll format the error
       757▕         // message to include the bindings with SQL, which will make this exception a
       758▕         // lot more helpful to the developer instead of just the database's errors.
       759▕         catch (Exception $e) {
     ➜ 760▕             throw new QueryException(
       761▕                 $query, $this->prepareBindings($bindings), $e
       762▕             );
       763▕         }
       764▕     }

     i   A column was not found: You might have forgotten to run your database migrations.
         https://laravel.com/docs/master/migrations#running-migrations

     1   C:\laragon\www\ecomer\vendor\laravel\framework\src\Illuminate\Database\Connection.php:414
         PDOException::("SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.subcategory_id' in 'where clause'")

     2   C:\laragon\www\ecomer\vendor\laravel\framework\src\Illuminate\Database\Connection.php:414
         PDO::prepare("select * from `products` where exists (select * from `sub_categories` where `products`.`subcategory_id` = `sub_categories`.`id` and `color` = ? and `size` = ?)")
     

     

    Mi ColorProductSeeder

     

    <?php

     

    namespace Database\Seeders;

     

    use App\Models\Product;

    use Illuminate\Database\Console\Seeds\WithoutModelEvents;

    use Illuminate\Database\Eloquent\Builder;

    use Illuminate\Database\Seeder;

     

    class ColorProductSeeder extends Seeder

    {

        /**

         * Run the database seeds.

         *

         * @return void

         */

        public function run()

        {

            $products = Product::whereHas('subcategory', function(Builder $query){

                $query->where('color', true)->where('size', false);

            })->get();
     

            foreach ($products as $product) {

                $product->colors()->attach([

                    1 => ['quantity'=> 10 ],

                    2 => ['quantity'=> 10 ],

                    3 => ['quantity'=> 10 ],

                    4 => ['quantity'=> 10 ]

                ]);

            }

        }

    }

  • Antonio Domínguez

    Antonio Domínguez hace 6 meses

    Buenas,

    Al ejecutar la migración de ColorProductSeeder tengo el siguiente error:

    SQLSTATE[42S02]: Base table or view not found: 1146 Table 'codersfree.product_subcategory' doesn't exist

      9   database/seeders/ColorProductSeeder.php:27
         Illuminate\Database\Eloquent\Relations\BelongsToMany::attach()

     

    Evidentemente esa tabla no existe, Me da en el método attach del Seeder

    26  foreach ($products as $product) {

    27  $product->colors()->attach([

    28  1 => [

    29  'quantity' => 10

    30  ], …

     

    Donde únicamente se utiliza subcategory es en la query anterior a este foreach:

    $products = Product::whereHas('subcategory',function(Builder $query){

    $query->where('color', 1) //he cambiado el true por 1

    ->where('size', 0);               // pero el error sigue igual

    })->get();

     

    En el modelo Product, la relación está definida así

    //Relación muchos a muchos

    public function colors(){

    return $this->belongsToMany(Subcategory::class);

    }