I've been learning Laravel and am trying to get a form in the login view to bind to a model and then have the model passed to a controller, I'm then passing the model to another the dashboard view to display the username attribute of it to test if it is working. The username is not being displayed therefore the model doesn't seem to be binding.
I've looked through the documentation and can't figure out what else I need to do/what I have done wrong.
Thanks
Model
<?php
/**
* Created by PhpStorm.
* User: Theo
* Date: 25/01/2018
* Time: 19:35
*/
namespace App\Http\Controllers\Models;
class User
{
public $id;
public $username;
public $password;
}
Controller
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Models\User;
use Illuminate\Http\Request;
class LoginController extends Controller
{
public function index(){
$user = new User();
return view('pages.login.index')->with('user', $user);
}
public function attemptLogin(User $user){
return view('pages.dashboard.index')->with('user', $user);
}
}
Login view
@extends('layouts.default')
@section('content')
<div id="login-container">
<div class="login-box">
{!! Form::model($user, ['action' => 'LoginController@attemptLogin']) !!}
{!! Form::text('username', @$user->username) !!}
{!! Form::password('password', @$user->password) !!}
{!! Form::submit('Login') !!}
{!! Form::close() !!}
</div>
</div>
@stop
Dashboard view
@extends('layouts.default')
@section('content')
<h3>Welcome to the dashboard! {{{ $user->username }}}</h3>
@stop
Routes
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('players');
});
/*
Route::get('/players/{uid}', function ($uid) {
return view('players' . $uid);
});
*/
Route::get('dashboard', 'DashboardController@index');
Route::get('login', 'LoginController@index');
Route::post('login/attemptLogin', 'LoginController@attemptLogin');
change
<?php
/**
* Created by PhpStorm.
* User: Theo
* Date: 25/01/2018
* Time: 19:35
*/
namespace App\Http\Controllers\Models;
class User
{
public $id;
public $username;
public $password;
}
to this. put the file into app folder and add extends Model
<?php
namespace App;
/**
* Created by PhpStorm.
* User: Theo
* Date: 25/01/2018
* Time: 19:35
*/
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $fillable = ['username'];
protected $hidden = ['password'];
}
You should create model using artisan command line by typing php artisan make:model User
which will then create a model User
for you in app/
folder.
You are creating model inside app/Http/Controllers
which is used for storing Controllers only.
If you want a basic auth system you can run php artisan make:auth
which will genrate a bootstrap boilerplate with basic auth system
You don't have any route parameters.
In order to use Route-Model Binding, you need to either use a resource route or specify a route parameter equal to the controller method argument's name.
So if you have:
public function attemptLogin(User $user) {}
To have an instance of User injected, you need to have a route parameter that matches $user:
Route::post('login/attemptLogin/{user}', 'LoginController@attemptLogin');
However, I'm not exactly sure why you'd be trying to attempt a login into a specific user rather than provide the username as a request variable.
If you change the namespace of your User model to App\Http\Controllers\Models\User
you need to update the config/auth.php
file.
Search for this code:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],
and change the model section to: 'model' => App\Http\Controllers\Models\User::class,
BTW: Its not a good idea moving your Models below the Http
folder because you mixing the HTTP layer with your business layer.