Obtain data of an object in Laravel

4

I know it's not the way to ask, but I'm starting in Laravel and they sent me an exercise in class and I do not know how to continue. They ask me to show a student (indicating their id), I have created the table and the model with artisan, I have filled two objects in the phpmyadmin database, in the model I have put this:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App/Alumno;
class Alumno extends Model{
    public function mostrar($id){
        $alumno= Alumno::find($id);
        return View::make(alumno);
    }
}

And in the file web.php the following:

Route::get('alumno/1','[email protected]');

I do not know exactly what needs to be done, even if the above is fine. I searched the internet, but I got more involved. Thanks in advance.

    
asked by Charly Utrilla 27.01.2018 в 13:18
source

2 answers

3

If you're working with Laravel you have to think about MVC (even though Laravel has more layers).

What we should do:

1 ° Create and define the model and controller

php artisan make:model Tabla -c

This generates a model in App/ and a controller in App/Http/Controllers .

In the model is where we will configure the data of the table something like

File App\Tabla.php     

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tabla extends Model
{
    protected $table = 'Tabla'; // Nombre de la tabla
    protected $primaryKey = 'id'; // Clave primaria

    // Columnas de la tabla
    protected $fillable = ['nombre','apellidos'];

}

In the controller is where we execute the logic, something like:

File App\Http\Controllers\TablaController.php

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
// Incluimos el modelo
use App\Tabla;

class TablaController extends Controller
{
// Creamos un método para atender la peticion llamado show
    public function show($id)
    {
        // Buscamos el id en la tabla
        $dato = Tabla::find($id);
        // retornamos la vista con los datos 
        return view('tabla.show')->with('dato', $dato);
        // with() nos permite pasar variables a la vista
        // el primer parámetros es el nombre con el que estará disponible en la vista
        // el segundo son los datos. 
    }
}

2 ° Create and define the route and view.

For the view we go to the path resources/views/ and create a new folder to save the views that our controller will use with the name tabla (for the example) and inside this we create a file with the name of the view with extension .blade.php (show.blade.php for the example).

Once the above is done we open the created file resources/views/tabla/show.blade.php and we can define the view, we will also have the variable $dato that we return next to the view in the controller.

For example

Id: {{ $dato->id }} <br>
Nombre: {{ $dato->nombre }} <br>
Apellidos: {{ $dato->apellidos }} <br>

Now we just need to define the route, for that we go to the file routes/web.php and create a route that points to our controller, for example

Route::get('/mi/ruta/{id}', '[email protected]');
// {id} Será la variable que recogeremos en el controlador
// en $id:  public function show($id)

To access this example we should use the url dominio.es/mi/ruta/1

    
answered by 27.01.2018 / 16:03
source
2

Some points for your example to work, you should first create a driver to handle this, in console run

php artisan make:controller AlumnoController

In the AlumnoController file created within App/Http/Controllers create a show method and add the code that has to show and delete that part of the model, with the modification of using findOrFail so that if given an id and this is not registered return an error 404

public function show($id)
{
    $alumno= Alumno::findOrFail($id);
    return view('alumno',compact('alumno'));
}

Then on your path routes.php point to that controller method by adding the id as a parameter to the route

Route::get('alumno/{id}','[email protected]')->name('alumno.show');

It would already subtract how you want to show in your view alumno.blade.php , for example

<h2>{{ $alumno->name }}</h2>

Now accessing the route from the browser will get the user depending on the parameter of URL .

Edit

How to consider, if your model is Student, Laravel interprets that the table will be alumnos , therefore if your table is called different it is necessary to specify the name using the attribute $table , additionally if you want to add Massively records to database using your model, you must specify in the array fillable the columns of your table.

class Tabla extends Model
{
    // nombre de la tabla
    protected $table = 'nombretabla'; 

    // Columnas de la tabla para insert masivo
    protected $fillable = ['nombre','apellido','edad'];

}
    
answered by 27.01.2018 в 14:17