Back

Adding Data to database using Eloquent Model


Im new to laravel and i wish to add some data to my database, im following tutorial from lynda.com Up And Running With Laravel, the problem is the version he is using is Laravel 4 and there is so much difference with Laravel 5, I kept ob having errors Class Not Found, refering to my Painting class which is a Eloquent Model, HELP ME PLEASE.

dnamotoring replied 3 years ago

Hopefully this example can guide you:

app\MyModels\Painting.php

<?php namespace App\MyModels;

use Illuminate\Database\Eloquent\Model;

class Painting extends Model {

    protected $connection = 'mysql';
    protected $primaryKey = 'id';
    protected $table = 'painting';
    protected $fillable = array(
        'name',
        'artist',
        'price'
    );

    public $timestamps = false;

}

app\Http\Controllers\MyController.php

<?php namespace App\Http\Controllers;
use App\MyModels\Painting;

class MyController extends Controller {

	public function index()
	{
		$art = new Painting;
		$art->name = 'Mona Lisa';
		$art->artist = 'Leonardo de Vinci';
		$art->price = 300000000;
		$art->save();
		
	}

}

Make sure your mysql is setup with correct credentials in app\config\database.php and a painting table exists.

This should get you started.

rajanhossain replied 1 year ago

This worked really very well. I just started laravel and wanted to store data in db without going through a lot of reading.

Rustem Ibragimov replied 9 months ago

Thank you)))


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us