Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hidayat3676's avatar

how to insert data into db using model

i have the following code

Model

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Match extends Model
{

     protected $fillable = array(['first_team',
'second_team',
'tournament_id',
'stadium_id',
'date',
'start_date',
'status',
'end_date',
'spectators',
'season',
'round',
'match_abandoned',
'match_drawn',
'start_time']);

Controller

 public function store(Request $request)
    {
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://www.thesportsdb.com/api/v1/json/1/searchevents.php?e=Arsenal_vs_Chelsea",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => array(
                "Cache-Control: no-cache",
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);
        $data = json_decode($response,1);
        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {

            $match = new Match;
            foreach ($data['events'] as $event){
            $match->id = $event['idEvent'];
            $match->first_team = $event['strHomeTeam'];
            $match->second_team = $event['strAwayTeam'];
            $match->tournament_id  = $event['idLeague'];
            $match->date = $event['dateEvent'];
            $match->start_date = $event['strDate'];
            $match->spectators = $event['intSpectators'];
            $match->seasion = $event['strSeason'];
            $match->round       = $event['intRound'];
            $match->start_time   =    $event['strTime'];
            $match->save();
            }


        }

    }


migration



 public function up()
    {
        Schema::create('matches', function (Blueprint $table) {
            $table->increments('id');
            $table->string("first_team");
            $table->string("second_team");
            $table->unsignedInteger('tournament_id');
            $table->foreign('tournament_id')->references('id')->on('tournaments');
            $table->unsignedInteger('stadium_id');
            $table->foreign('stadium_id')->references('id')->on('stadiums');
            $table->date("date");
            $table->date("start_date");
            $table->string("status");
            $table->date("end_date");
            $table->string("spectators");
            $table->string("season");
            $table->integer("round");
            $table->integer("match_abandoned");
            $table->integer("match_drawn");
            $table->time("start_time");
            $table->timestamps();
        });
    }

i am getting error this while inserting record into db Declaration of App\Models\Match::update(App\Models\Request $request, $id) should be compatible with Illuminate\Database\Eloquent\Model::update(array $attributes = Array, array $options = Array)

0 likes
4 replies
Grelav's avatar

if you are want to create a record in the database for each of your data events

set the new statment inside the loop and remove the line where you specify the id your id field is auto increment.


            foreach ($data['events'] as $event){
        $match = new Match;
            // remove this line: $match->id = $event['idEvent'];
            $match->first_team = $event['strHomeTeam'];
            $match->second_team = $event['strAwayTeam'];
            $match->tournament_id  = $event['idLeague'];
            $match->date = $event['dateEvent'];
            $match->start_date = $event['strDate'];
            $match->spectators = $event['intSpectators'];
            $match->seasion = $event['strSeason'];
            $match->round       = $event['intRound'];
            $match->start_time   =    $event['strTime'];
            $match->save();
            }
1 like
Snapey's avatar

Your $match->id is set as an auto incrementing field therefore you should not be trying to set it.

If you need the ['idEvent'] then you probably need a separate column for it.

Alternatively, change the id column on your model to the correct type to store ['idEvent'] and set public $incrementing=false; on your model

hidayat3676's avatar

the time i'm getting is not in the mysql format how can i change this time format to the mysql format to store it into table.

Snapey's avatar

Use Carbon to parse it into a Carbon object then just pass Carbon to the Eloquent model

$match->start_date = Carbon\Carbon::createFromFormat('d/m/y',$event['strDate']);

Please or to participate in this conversation.