laravel 5 how to pass data to controller to model

16,068

You can simply pass parameters to your models. In your controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\AdminLoginModel;

class AdminLoginController extends Controller
{
    /**
     * Handle an authentication attempt for admin user.
     *
     * @return Response
     */
    public function userAuthentication(Request $request)
    {
        $admin_model = new AdminLoginModel();
        $admin_model->checkAuthentication($request);
    }
}

In your model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AdminLoginModel extends Model
{

    public function checkAuthentication($request)
    {
        // Do something with $request
        $request->input('username');
    }

}
Share:
16,068
Mr.Happy
Author by

Mr.Happy

Updated on July 14, 2022

Comments

  • Mr.Happy
    Mr.Happy almost 2 years

    I am new for Laravel 5 and learning. Now I have created login form and I want to check if user is valid(match user from table) and take some action.

    Right now I am getting all form data from controller (AdminLoginController.php). Now I don't know how to pass to model for check if user is exists or not.

    view (login.blade.php)

    //I have used this form action {{ URL::to('administrator/userAuthentication') }}
    
    <form name="frmLogin" action="{{ URL::to('administrator/userAuthentication') }}" method="post">
        <input name="_token" type="hidden" value="{{ csrf_token() }}"/>
        <div class="form-group has-feedback">
            <input type="text" name="username" id="username"class="form-control" placeholder="Username">
            <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
        </div>
        <div class="form-group has-feedback">
            <input type="password" name="password" id="password" class="form-control" placeholder="Password">
            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
        </div>
        <div class="row">
            <?php /*<div class="col-xs-8">
                <div class="checkbox icheck">
                    <label>
                        <input type="checkbox"> Remember Me
                    </label>
                </div>
            </div><!-- /.col -->*/ ?>
            <div class="col-xs-4">
                <button type="submit" class="btn btn-primary btn-block btn-flat">Login</button>
            </div><!-- /.col -->
        </div>
    </form>
    

    controller (AdminLoginController.php)

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    
    class AdminLoginController extends Controller
    {
        /**
         * Handle an authentication attempt for admin user.
         *
         * @return Response
         */
        public function userAuthentication(Request $request)
        {
            echo "<pre>";
                return $request;
            echo "</pre>";
        }
    }
    

    model (AdminLoginModel.php)

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class AdminLoginModel extends Model
    {
    
        public function checkAuthentication()
        {
            // code
        }
    
    }
    

    I don't know I am doing right way or not so need your suggestion.

    Thanks.

  • ciruvan
    ciruvan over 8 years
    This is not an answer to OP's question, just you telling OP to go look somewhere else for an answer.
  • Pedram marandi
    Pedram marandi over 8 years
    I suggested laravel documents :) question is so newbe so need to learn documentation at first. @dinsdale
  • Iamzozo
    Iamzozo over 8 years
    But as Pedram marandi suggested, checkout the docs: laravel.com/docs/5.1/authentication. First the docs seems overhelming, but checkout the basics and authentication in services
  • Mr.Happy
    Mr.Happy over 8 years
    Thanks I will check laravel docs but can you please tell me that my coding style is proper or not for laravel?
  • Iamzozo
    Iamzozo over 8 years
    Yes, I think it's proper! :)