Doubt with Laravel MVC

0

I made the presentation to the pfc teacher but she said that I broke the mvc pattern, calling the sql in the controller, and requested that I send it to the model and retrieve the query for the controller and send the email.

But how can I get started with laravel? Can anyone help me?

Controller     

namespace App\Http\Controllers\Site;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Agenda;
use Mail;

class AgendamentoController extends Controller {

    public function index(){
        $registros = Agenda::all();
       // dd($agenda);
        return view('site.agenda',compact('registro'));    
    }

    public function adicionar(Request $request){
        $agendamento = new Agenda($request->all());
        $agendamento->imovel_id = $request->imovel_id;
        $agendamento->nome = $request->nome;
        $agendamento->email = $request->email;
        $agendamento->data = $request->data;
        $agendamento->save();

        \Session::flash('mensagem',['msg'=>'Agendamento enviado com sucesso!','class'=>'green white-text']);

        return redirect()->route('site.home');
    }

    public function aprovado(Request $request){


    }

    public function recusado(Request $request){

    }
      /*  $imovel = Imovel::find($id);
        $dados = $request->all();

        return view('site.agenda')->with(‘results’, $results);        //        

        if($imovel->agendas()->count()){
            echo $imovel;

        }
        $imovel = $dados['imovel_id'];     
        $dia = $dados['data'];
        $email = $dados['email'];
        $nome = $dados['nome'];
        $aprovado = 'Aprovada';
        $reprovado = 'Reprovada';


        //$sql = Agenda::where('imovel_id', '=', $imovel)->where('data', '=', $dia)->first();

        if ($sql != null) {      

            Mail::send('mail', ['name'=> $nome, 'imovel'=> $imovel, 'status'=>$reprovado, 'data'=>$dia, 'email'=>$email], function($m) use($email, $nome) {

                $m->to($email, $nome)->subject('Solicitação de Agendamento Hoouzer');
                $m->from('[email protected]','Hoouzer Imoveis');
            });


         \Session::flash('mensagem',['msg'=>'Já existe agendamento para este dia!','class'=>'red white-text']);

          return redirect()->route('site.home');
        }else {

            $agendamento = new Agenda();
            $agendamento->imovel_id = $dados['imovel_id'];
            $agendamento->nome = $dados['nome'];
            $agendamento->email = $dados['email'];
            $agendamento->data = $dados['data'];
            $agendamento->save();


            Mail::send('mail', ['name'=> $nome, 'imovel'=> $imovel, 'status'=>$aprovado, 'data'=>$dia, 'email'=>$email], function($m) use($email, $nome) {

                $m->to($email, $nome)->subject('Solicitação de Agendamento Hoouzer');
                $m->from('[email protected]','Hoouzer Imoveis');
            });

            \Session::flash('mensagem',['msg'=>'Agendamento enviado com sucesso!','class'=>'green white-text']);

            return redirect()->route('site.home');
        }       



    }*/
}

MODEL

namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Http\Controllers\site\AgendamentoController;

class Agenda extends Model{
/*
        private $imovel;
        private $dia;
        private $email;
        private $nome;

        function getImovel() {
                return $this->imovel;               
        }

        function getDia() {
                return $this->dia;
        }

        function getEmail() {
                return $this->email;               
        }

        function getNome() {
                return $this->nome;
        }

        function setImovel($imovel) {
                $this->imovel = $imovel;
        }

        function setDia($dia) {
                $this->dia = $dia;
        }

        function setEmail($email) {
                $this->email = $email;
        }

        function setNome($nome) {
                $this->nome = $nome;
        } */


        protected $fillable = ['imovel_id','nome','email','data'];
        protected $guarded = ['corretor_id', 'created_at', 'update_at'];
        protected $primarykey = 'id';
        protected $table = 'agendas';
         /*

        public function verificarAgendamento(Request $request) {
                $dados = $request->all();
                $imovel = $dados['imovel_id'];     
                $dia = $dados['data'];
                $dadosAgenda = Agenda::where('imovel_id', '=', $imovel)->where('data', '=', $dia)->first();


        return ucfirts($dadosAgenda);
        }

        private $imovel;

        function getImovel($value){
                return ucfirst($value);
        }

        function setFirstNameAttribute($value){
                $this->attributes['first_name'] = strtolower($value);
        }*/
}

VIEW

 @extends('layouts.site')

    @section('content')

    <div class="container">
        <div class="row section">
            <h3 align="center">Agenda</h3>
            <div class="divider"></div> 
        </div>
        <div class="row section">
            <div class="col s12 m7">

                <div class="video-container">

                </div>


                    <img class="responsive-img" src="">

            </div>
            <div class="col s12 m5">
            <h4>agenda</h4>
                <blockquote>
                   desc
                </blockquote>   


                <form class="col s12" action="{{ route('site.agenda.adicionar') }}" method="post">

                    <input type="hidden" name="_token" value="{{ csrf_token() }}">

                    <div class="input-field">
                        <input type="text" name="imovel_id" class="validate" value=""  pattern="[0-9]+$" >
                    </div>
                    <div class="input-field">
                        <input type="text" name="nome" class="validate" require pattern="[A-Za-zÀ-ú ]+$">
                        <label>Nome</label>
                    </div>
                    <div class="input-field">
                        <input type="email" name="email" class="validate" require>
                        <label>E-mail</label>
                    </div>
                    <div class="input-field">
                        <input type="date" name="data" class="validate" require max="2020-12-31" min="date("Y-m-d");">
                    </div>          
                    <button class="btn blue">Enviar</button>            
                </form>

            </div>
        </div>
    </div>
    @endsection

routes

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/


Route::get('/',['as'=>'site.home', 'uses'=>'Site\HomeController@index']);

Route::get('/sobre',['as'=>'site.sobre', 'uses'=>'Site\PaginaController@sobre']);

Route::get('/contato',['as'=>'site.contato', 'uses'=>'Site\PaginaController@contato']);

Route::post('/contato/enviar',['as'=>'site.contato.enviar', 'uses'=>'Site\PaginaController@enviarContato']);

//////////////////////////////////

Route::get('/agenda',['as'=>'site.agenda', 'uses'=>'Site\AgendamentoController@index']);

Route::post('/agenda/adicionar',['as'=>'site.agenda.adicionar', 'uses'=>'Site\AgendamentoController@adicionar']);

//////////////////////////////////
Route::get('enviaremail','Site\MailController@basic_email');
Route::get('agendar','Site\MailController@agendarEmail');
///////////////////////////
Route::get('/imovel/{id}/{titulo?}',['as'=>'site.imovel', 'uses'=>'Site\ImovelController@index']);



Route::get('/busca',['as'=>'site.busca', 'uses'=>'Site\HomeController@busca']);




Route::get('/admin/login',['as'=>'admin.login', function(){
    return view('admin.login.index');
}]);

Route::post('/admin/login',['as'=>'admin.login', 'uses'=>'Admin\UsuarioController@login']);

Route::group(['middleware'=>'auth'], function(){

    Route::get('/admin/login/sair',['as'=>'admin.login.sair', 'uses'=>'Admin\UsuarioController@sair']);

    Route::get('/admin',['as'=>'admin.principal', function(){
        return view('admin.principal.index');
    }]);

    Route::get('/admin/usuarios',['as'=>'admin.usuarios', 'uses'=>'Admin\UsuarioController@index']);
    Route::get('/admin/usuarios/adicionar',['as'=>'admin.usuarios.adicionar', 'uses'=>'Admin\UsuarioController@adicionar']);
    Route::post('/admin/usuarios/salvar',['as'=>'admin.usuarios.salvar', 'uses'=>'Admin\UsuarioController@salvar']);
    Route::get('/admin/usuarios/editar/{id}',['as'=>'admin.usuarios.editar', 'uses'=>'Admin\UsuarioController@editar']);
    Route::put('/admin/usuarios/atualizar/{id}',['as'=>'admin.usuarios.atualizar', 'uses'=>'Admin\UsuarioController@atualizar']);
    Route::get('/admin/usuarios/deletar/{id}',['as'=>'admin.usuarios.deletar', 'uses'=>'Admin\UsuarioController@deletar']);

    Route::get('/admin/usuarios/papel/{id}',['as'=>'admin.usuarios.papel', 'uses'=>'Admin\UsuarioController@papel']);
    Route::post('/admin/usuarios/papel/salvar/{id}',['as'=>'admin.usuarios.papel.salvar', 'uses'=>'Admin\UsuarioController@salvarPapel']);
    Route::get('/admin/usuarios/papel/remover/{id}/{papel_id}',['as'=>'admin.usuarios.papel.remover', 'uses'=>'Admin\UsuarioController@removerPapel']);

    Route::get('/admin/paginas',['as'=>'admin.paginas', 'uses'=>'Admin\PaginaController@index']);
    Route::get('/admin/paginas/editar/{id}',['as'=>'admin.paginas.editar', 'uses'=>'Admin\PaginaController@editar']);
    Route::put('/admin/paginas/atualizar/{id}',['as'=>'admin.paginas.atualizar', 'uses'=>'Admin\PaginaController@atualizar']);

    Route::get('/admin/tipos',['as'=>'admin.tipos', 'uses'=>'Admin\TipoController@index']);
    Route::get('/admin/tipos/adicionar',['as'=>'admin.tipos.adicionar', 'uses'=>'Admin\TipoController@adicionar']);
    Route::post('/admin/tipos/salvar',['as'=>'admin.tipos.salvar', 'uses'=>'Admin\TipoController@salvar']);
    Route::get('/admin/tipos/editar/{id}',['as'=>'admin.tipos.editar', 'uses'=>'Admin\TipoController@editar']);
    Route::put('/admin/tipos/atualizar/{id}',['as'=>'admin.tipos.atualizar', 'uses'=>'Admin\TipoController@atualizar']);
    Route::get('/admin/tipos/deletar/{id}',['as'=>'admin.tipos.deletar', 'uses'=>'Admin\TipoController@deletar']);

    Route::get('/admin/cidades',['as'=>'admin.cidades', 'uses'=>'Admin\CidadeController@index']);
    Route::get('/admin/cidades/adicionar',['as'=>'admin.cidades.adicionar', 'uses'=>'Admin\CidadeController@adicionar']);
    Route::post('/admin/cidades/salvar',['as'=>'admin.cidades.salvar', 'uses'=>'Admin\CidadeController@salvar']);
    Route::get('/admin/cidades/editar/{id}',['as'=>'admin.cidades.editar', 'uses'=>'Admin\CidadeController@editar']);
    Route::put('/admin/cidades/atualizar/{id}',['as'=>'admin.cidades.atualizar', 'uses'=>'Admin\CidadeController@atualizar']);
    Route::get('/admin/cidades/deletar/{id}',['as'=>'admin.cidades.deletar', 'uses'=>'Admin\CidadeController@deletar']);

    Route::get('/admin/imoveis',['as'=>'admin.imoveis', 'uses'=>'Admin\ImovelController@index']);
    Route::get('/admin/imoveis/adicionar',['as'=>'admin.imoveis.adicionar', 'uses'=>'Admin\ImovelController@adicionar']);
    Route::post('/admin/imoveis/salvar',['as'=>'admin.imoveis.salvar', 'uses'=>'Admin\ImovelController@salvar']);
    Route::get('/admin/imoveis/editar/{id}',['as'=>'admin.imoveis.editar', 'uses'=>'Admin\ImovelController@editar']);
    Route::put('/admin/imoveis/atualizar/{id}',['as'=>'admin.imoveis.atualizar', 'uses'=>'Admin\ImovelController@atualizar']);
    Route::get('/admin/imoveis/deletar/{id}',['as'=>'admin.imoveis.deletar', 'uses'=>'Admin\ImovelController@deletar']);

    Route::get('/admin/galerias/{id}',['as'=>'admin.galerias', 'uses'=>'Admin\GaleriaController@index']);
    Route::get('/admin/galerias/adicionar/{id}',['as'=>'admin.galerias.adicionar', 'uses'=>'Admin\GaleriaController@adicionar']);
    Route::post('/admin/galerias/salvar/{id}',['as'=>'admin.galerias.salvar', 'uses'=>'Admin\GaleriaController@salvar']);
    Route::get('/admin/galerias/editar/{id}',['as'=>'admin.galerias.editar', 'uses'=>'Admin\GaleriaController@editar']);
    Route::put('/admin/galerias/atualizar/{id}',['as'=>'admin.galerias.atualizar', 'uses'=>'Admin\GaleriaController@atualizar']);
    Route::get('/admin/galerias/deletar/{id}',['as'=>'admin.galerias.deletar', 'uses'=>'Admin\GaleriaController@deletar']);

    Route::get('/admin/slides',['as'=>'admin.slides', 'uses'=>'Admin\SlideController@index']);
    Route::get('/admin/slides/adicionar',['as'=>'admin.slides.adicionar', 'uses'=>'Admin\SlideController@adicionar']);
    Route::post('/admin/slides/salvar',['as'=>'admin.slides.salvar', 'uses'=>'Admin\SlideController@salvar']);
    Route::get('/admin/slides/editar/{id}',['as'=>'admin.slides.editar', 'uses'=>'Admin\SlideController@editar']);
    Route::put('/admin/slides/atualizar/{id}',['as'=>'admin.slides.atualizar', 'uses'=>'Admin\SlideController@atualizar']);
    Route::get('/admin/slides/deletar/{id}',['as'=>'admin.slides.deletar', 'uses'=>'Admin\SlideController@deletar']);

    Route::get('/admin/papel',['as'=>'admin.papel', 'uses'=>'Admin\PapelController@index']);
    Route::get('/admin/papel/adicionar',['as'=>'admin.papel.adicionar', 'uses'=>'Admin\PapelController@adicionar']);
    Route::post('/admin/papel/salvar',['as'=>'admin.papel.salvar', 'uses'=>'Admin\PapelController@salvar']);
    Route::get('/admin/papel/editar/{id}',['as'=>'admin.papel.editar', 'uses'=>'Admin\PapelController@editar']);
    Route::put('/admin/papel/atualizar/{id}',['as'=>'admin.papel.atualizar', 'uses'=>'Admin\PapelController@atualizar']);
    Route::get('/admin/papel/deletar/{id}',['as'=>'admin.papel.deletar', 'uses'=>'Admin\PapelController@deletar']);
    Route::get('/admin/papel/permissao/{id}',['as'=>'admin.papel.permissao', 'uses'=>'Admin\PapelController@permissao']);
    Route::post('/admin/papel/permissao/{id}/salvar',['as'=>'admin.papel.permissao.salvar', 'uses'=>'Admin\PapelController@salvarPermissao']);
    Route::get('/admin/papel/permissao/{id}/remover/{id_permissao}',['as'=>'admin.papel.permissao.remover', 'uses'=>'Admin\PapelController@removerPermissao']);

    Route::get('/admin/papel/permissao/{id}',['as'=>'admin.papel.permissao', 'uses'=>'Admin\PapelController@permissao']);

    Route::post('/admin/papel/permissao/salvar/{id}',['as'=>'admin.papel.permissao.salvar', 'uses'=>'Admin\PapelController@salvarPermissao']);

    Route::get('/admin/papel/permissao/remover/{id}/{id_permissao}',['as'=>'admin.papel.permissao.remover', 'uses'=>'Admin\PapelController@removerPermissao']);


});
    
asked by anonymous 22.06.2018 / 17:36

1 answer

1

Laravel uses the default active record , which allows us to only persist and query data without the need for an extra layer to the bank specifically.

I do not see any problem in calling some puny queries like Model::find() and Model::all() , for example.

What she wanted to say is here:

$sql = Agenda::where('imovel_id', '=', $imovel)->where('data', '=', $dia)->first();

Using this snippet of code as an example, the teacher might have meant that you should do this in model , creating a method for returning date schedules to a specific property, for example:

>
// na model
public function AgendamentosPorImovel($imovel_id, $data)
{
    return $this->where('imovel_id', $imovel_id)
                ->where('data', $data)
                ->first();
}

So in controller you would only have to call this method, thus eliminating the responsibility of your controller .     

22.06.2018 / 20:06