Consume data from an existing table in Laravel

Asked

Viewed 1,448 times

0

I have a base of use of Laravel, but I always used this creating the tables by Migrations.

I know consume data from a php database too, but in this case I have to build the class for such.

I am trying to make an application with Laravel that will consume data from an existing database, in this case what is the correct way to use this data in the application? I create the same Migration if I create the existing bank?

2 answers

1

As Migrations are required if you are going to create or modify tables in the dataset.

To manipulate these tables, you can Query Builder, that is, without the need for a Model, for example:

$users = DB::table('users')->get();
$user = DB::table('users')->where('name', 'John')->first();
DB::table('users')->where('id', 1)->update(['votes' => 1]);

If you need the Model in its implementation, it is good to point out that the Model Laravel has settings by default, for example, the table name, which column is primary key and whether the table has columns created_at and updated_at. Tables created outside the standards are required to specify. When in doubt, look at the documentation of Eloquent.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Pessoas extends Model{
    protected $table = 'pessoas';
    protected $primaryKey = 'id_pessoa';
    public $timestamps = false;
}

-1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.