How does the reader know which table of my comic will be used for my operation?

Asked

Viewed 94 times

1

I have a table in my comic called lostanimals_, created without using Migrations.

I created a class in the basement for her:

namespace App;
use Illuminate\Database\Eloquent\Model;

class AnimalPerdido extends Model
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id', 'id_usuario', 'lat', 'lng', 'id_tipo_pet', 'raca', 'id_sexo', 'data', 'possui_identificador', 'id_cores', 'informacoes_adicionais' 
    ];

}

I created a controller to return all data from this table:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App/AnimalPerdidoController;

class AnimalPerdidoController extends Controller
{
    //Retorna todos os cadastros
    public function index()
    {
        $animaisPerdidos = AnimalPerdido::->get();
        return response()->json($animaisPerdidos);
    }
}

And on my route:

Route::get('animalperdido', 'AnimalPerdidoController@index');

How can I speak pro How my lost animal table will use this model/controller?

  • Sought in the documentation? Which part was in doubt?

  • Inside your Controller, put: use App\AnimalPerdido; this will be enough for your controller to identify your model.

  • So, my question I posted on the question.. I would like to know how the Variable will apply the controller/model that I created in the table that already exists in the database, because I am not using Migrations... How would he find which table he will perform?

  • So my question is more to know how my controller will know that it has to fetch the data in the lost animals_table as it was not used Migrations.

  • Only declaring a protected property $table = 'lost animals' is sufficient?

2 answers

4

I took it on the basis of Documentation official, but I added some notes:

Table names

Note that we did not say what was the name of the model’s table Flight. By convention, the plural of the class in snake_case (instead of uppercase, use an _ as a separator), but you can set another table manually in the model this way, via a property table:

<?php

    namespace App;
    use Illuminate\Database\Eloquent\Model;
    class Flight extends Model
    {
        /**
         * Aqui vai a tabela que é associada com o model.
         *
         * @var string
         */
        protected $table = 'my_flights'; 
    }

In summary, the class statement adds this property:

protected $table = 'nome_da_tabela';

already with the table name in snake_case.


Notes:

  • When the documentation speaks in plural, it is the "dumb plural", only adds a s at the end of the string

    NOTE: has a pluralization class for English, which is a bad idea, and obviously in Portuguese it will look terrible if it is not adapted.

  • The conversion into snake_case is basically a case by case exchange, and insertion of a _ to separate words: AbcDef => abc_def

1


Try to put a variable protected $table = 'nome_da_sua_tabela:

namespace App;
use Illuminate\Database\Eloquent\Model;

class AnimalPerdido extends Model
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id', 'id_usuario', 'lat', 'lng', 'id_tipo_pet', 'raca', 'id_sexo', 'data', 'possui_identificador', 'id_cores', 'informacoes_adicionais' 
    ];

    protected $table = animais_perdidos

}

Browser other questions tagged

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