nhayder
3 months ago
102
7
Laravel

Customer command is inserting data correctly to DB

Posted 3 months ago by nhayder

When a user clicks a button on the page i'm running a custom command to feed the database with set of data directly from controller.

in controller, I'm passing some data to command that customize the data like this


    public function addNewWidget(Request $request)
    {
        $request->validate([

            'name' => 'required|string',

            'type' => 'required|int:1,0',

            'page_id' => 'required|integer',

            'installer' => 'required|string',

            'widgetset' => 'required|string',

        ]);

Artisan::call('widget:'.$request->installer.' '.$request->page_id.' '.$wSort.'');

this is the custom command

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Widget;

class contactus extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'widget:contactus {pageID} {wSorting}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Soon';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $contactus = Widget::create([

            'name' => 'contactus',
            'page_id' => $this->argument('pageID'),
            'sorting' => $this->argument('wSorting'),
            'poster' => 'placeholder.png',
            'others' => '[email protected]', // this is always NULL in DB laravel is not inserting the email address
            'embed' => 'https://vimeo.com/11254455848',
       
         // ....
        ]);

    }
}


Thankfully, The data is getting inserted as intended, Except the others db field, It's always NULL and Never inserted the intended data => ([email protected]).

This is my migration


        Schema::create('widgets', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->nullable();
            $table->integer('page_id')->unsigned();
            $table->foreign('page_id')->references('id')->on('pages')->onDelete('cascade');
            $table->integer('sorting');
            $table->string('poster')->default('placeholder.png');
            $table->longText('others')->nullable();
            $table->string('embed')->nullable()->default('https://www.youtube.com/embed/Md_YnF09WDY');

        // .... contunue

to keep it short? only the Other db column is always null after running the command from controller where is should insert [email protected] email address,

Can someone see any issue with my code ???

all the best

Please sign in or create an account to participate in this conversation.