How to pass variable from controller to model in Laravel

You can pass variable from controller to model in Laravel by defining a method in model and calling the model method from controller with argument.

Answers 1
  • 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');
        }
    
    }
    
    0
  • Back to code snippet queries related laravel