Pass value from controller to model in laravel

Updated at 09-Aug-2021, By samar

Pass value from controller to model in laravel

In this tutorial, we will try to find the solution to "Pass value from controller to model in laravel" through programming.

You can pass values or parameters from controller to model in laravel by creating a model instance and calling the model method by using $object->methodName($parameters).
  • Pass parameter values from controller to admin model in laravel

    //app\Http\Controllers\AdminController.php
    
    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Models\Admin;
    
    class AdminController extends Controller
    {
        /**
         * Handle an authentication attempt for admin.
         *
         * @return Response
         */
        public function userAuthentication(Request $request)
        {
            $admin = new Admin();
            $admin->checkUserAuthentication($request);
        }
    }
    
    //app\Models\Admin.php
    <?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;
    class Admin extends Model
    {
    
        public function checkUserAuthentication($request)
        {
            // Do something with $request
            $request->input('username');
        }
    
    }
    

Back to code snippet queries related laravel