Randy_Johnson
3 months ago
298
5
General

Using Models

Posted 3 months ago by Randy_Johnson

I have been creating a website I am have come to the realisation that I haven't been using models, and now I am thinking, should I be using models.

Here is an example of two of my controllers, one being basecontroller and usercontroller, I have used this base class so that I can inherit for all my other controllers and just pass in data that is specific to each class. I don't know if this is the correct way to go about it but it was the quicker. I could not help but notice that all get methods would nicely fit into this section, where, post and put methods would go into its individual controller.

Now to the point, I haven't used models, and its concerning me, should I be using, and if you can whilst you are here, can you point out everything wrong with my work... Sorry for the big ask.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BaseController extends Controller
{
    protected $table;       // Database table name
    protected $cv;          // Create view
    protected $rv;          // Read view
    protected $rsv;         // Read single view
    protected $ev;          // Edit view
    protected $sf;          // Search functionality

    public function __construct() {
        $this->middleware('auth');
    }

    public function create() 
    {
        return view($this->cv);
    }

    public function read() 
    {
        if ($data = DB::table($this->table)->simplePaginate(15))
        {
            return view($this->rv, [$this->table => $data]);
        }
        else 
        {
            return abort(404);
        }
    }

    public function readSingle($slug) 
    {
      if ($data = DB::table($this->table)->where('id', $slug)->first())
      {
          return view($this->rsv, [$this->table => $data]);
      }
      else 
      {
          return abort(404);
      }
    }

    public function edit($slug) 
    {
        if ($data = DB::table($this->table)->where('id', $slug)->first())
        {
            return view($this->ev, [$this->table => $data]);
        }
        else 
        {
            return abort(404);
        }
    }

    public function delete($slug)
    {
        if (DB::table($this->table)->where('id', '=', $slug)->delete())
        {
            return redirect()->route('read-users');
        } 
        else 
        {
            return abort(404);
        }
    }

    public function search(Request $request)
    {
        if ($data = DB::table($this->table)->where('name', $request->input('search'))->first())
        {
            return view($this->rsv, [$this->table => $data]);
        }
        else 
        {
            return abort(404);
        }
    }
}
<?php

namespace App\Http\Controllers;

use App\User;
use Carbon\Carbon;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class UserController extends BaseController
{
    public function __construct() { 
        parent::__construct();
        $this->table = "users"; 
        $this->cv = "admin/user/create";
        $this->rv = "admin/user/read"; 
        $this->rsv = "admin/user/read-user";
        $this->ev = "admin/user/edit";
    }

    public function store(Request $request)
    {
        if(DB::table($this->table)->insert(
            [   
                'name' => $request->input('name'), 
                'email' => $request->input('email'),
                'password' => bcrypt($request->input('password')),
                'is_admin' => $request->input('is_admin'),
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now() 
            ]
        ))
        {
            return redirect()->route('read-users');
        }
        else 
        {
            return abort(404);
        }
    }

    public function update(Request $request)
    {
        if(DB::table($this->table)
            ->where('id', $request->input('id'))
            ->update(array
                (
                    'name' => $request->input('name'),
                    'email' => $request->input('email'),
                    'password' => bcrypt($request->input('password')),
                    'is_admin' => $request->input('is_admin'),
                    'updated_at' => Carbon::now()
                )
            ))
            {
                return redirect()->route('read-users');
            }
            else 
            {
                return abort(404);
            }
    }
}

Please sign in or create an account to participate in this conversation.