This question already has answers here:
Laravel blank white screen
(35 answers)
Closed 6 years ago.
I have this view page index.blade.php
#section('main')
<h1>All Users</h1>
<p><a href={!! url('users\create') !!}>Add new user</a></p>
#if ($users->count())
<table border="2">
<tr><td>s.n.</td><td>name</td><td>email</td><td>options</td>
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td><a href={{ url('users\{id}\edit', $user->id)}}>Edit</a>
<td>
{!! Form::open(array('method' => 'DELETE',
'route' => array('users.destroy', $user->id))) !!}
{!! Form::submit('Delete', array('class' => 'btn btn-danger')) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</table>
#else
There are no users
#endif
#stop
and controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
class usercontroller extends Controller
{
public function index()
{
$users=User::all();
return view('users.index', compact('users'));
}
public function create()
{
return View('users.create');
}
public function store()
{
$input = Input::all();
$validation = Validator::make($input, User::$rules);
if ($validation->passes())
{
User::create($input);
return Redirect::route('users.index');
}
return Redirect::route('users.create')
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
public function show($id)
{
//
}
public function edit($id)
{
$user = User::find($id);
if (is_null($user))
{
return Redirect::route('users.index');
}
return View('users.edit', compact('user'));
}
public function update($id)
{
$input = Input::all();
$validation = Validator::make($input, User::$rules);
if ($validation->passes())
{
$user = User::find($id);
$user->update($input);
return Redirect::route('users.show', $id);
}
return Redirect::route('users.edit', $id)
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
public function destroy($id)
{
User::find($id)->delete();
return Redirect::route('users.index');
}
}
and
routes.php
Route::resource('users', 'UserController');
but when i enter laravel/public/users it displays the white blank page. same problem with any other routes such as:
Route::get('/users/edit/{id}','UserController#edit');
Route::put('/users/{id}','UserController#update');
Route::delete('/users/{id}','UserController#delete');
the problem occurred when i follow these steps https://laravelcollective.com/docs/5.2/html to use forms.
. earlier the error was 'class form' not found.
the app/User.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
class User extends BaseModel
{
protected $primarykey='id';
protected $table='users';
protected $fillable=array('name','email','password');
public static $rules = array(
'name' => 'required|min:5',
'email' => 'required|email'
);
}
and basemodel
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
class BaseModel extends Model {
public function selectQuery($sql_stmt) {
return DB::select($sql_stmt);
}
public function sqlStatement($sql_stmt) {
DB::statement($sql_stmt);
}
}
at first change your controller file name app/Http/Controller/usercontroller.php to app/Http/Controller/UserController.php.then in UserController.php you edit your class name like below:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
class UserController extends Controller
Then make sure that you have a model name app/User.php which contains all the table field and table name.In app/Http/routes.php you set another route
Route::get('users','UserController#index');
then you can enter laravel/public/users
Related
relation data does not appear in the laravel view
> View
<td>
#foreach($p->jenis as $a)
{{ $a->jenis_penyedia }},
#endforeach
</td>
coba
> Model Jenis.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Jenis extends Model
{
protected $table="tbl_jenis_penyedia";
protected $fillable=['id_jenis_penyedia','jenis_penyedia'];
public function profile(){
return $this->belongsTo('App\Profile');
}
}
> Model Profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
protected $table="tbl_profil_penyedia";
protected $fillable=['id_profil_penyedia','id_jenis_penyedia','nama', 'no_ktp', 'file', 'npwp', 'bank', 'no_rek', 'email', 'no_telp', 'keahlian', 'pengalaman', 'alamat', 'pendidikan'];
public function jenis(){
return $this->hasMany('App\Jenis');
}
}
Model
> Controller
$profile = Profile::all();
return view('profile/homeprofile',['profile' => $profile]);
Controller
no error
only data does not appear
Check your db migration file. define this in create_tbl_profil_penyedia_table :
Migrate it.
$table->unsignedInteger('id_jenis_penyedia')->unsigned();
$table->foreign('id_jenis_penyedia')->references('id_jenis_penyedia')->on('tbl_jenis_penyedia');
Edit your Jenis model
public function jenis(){
return $this->hasMany('App\Jenis', 'id_jenis_penyedia');
}
According to your controller your view should work like this:
#foreach($profile as $p)
#foreach($p as $a)
{{ $a->jenis->jenis_penyedia }},
#endforeach
#endforeach
I have this application using Laravel and im trying to register some information from the Form Class to my DB through the store method in the controller, but for some reason it throws me some error. I cant even print the request coming from the form, as usual. Can someone point me a possible mistake tht i am making? I am new to Laravel
This is my form on a view called create.blade.php
#extends('layouts.app')
#section('content')
<p><b>Register your house</b></p>
{!! Form::open(['method'=>'post', 'action'=>'AdminHouseController#store']) !!}
{!! Form::text('house_address', null,['placeholder'=>'House Address']) !!}
<input type="hidden" name="house_admin" value="{{Auth::user()->id}}">
{!! Form::number('nflatmates', null, ['placeholder'=>'How many flatmates']) !!}
{!! Form::submit('Register', ['class'=>'ui-btn buttonDefault']) !!}
{!! Form::close() !!}
#stop
This is my controller AdminHouseController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\House;
use App\User;
class AdminHouseController extends Controller
{
public function index(){
}
public function create($role_id){
if($role_id == 1){
return view('admin.house.create');
}else{
return redirect('home');
}
}
public function store(Request $request){
House::create($request->all());
return redirect('home');
}
public function show($id){
}
public function edit($id){
}
public function update(Request $request, $id){
}
public function destroy($id){
}
}
And this is my router file web.php
use App\User;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/house/{role_id}', 'AdminHouseController#create')->name('house');
Route::post('store', [
'uses' => 'AdminHouseController#store'
]);
you might be missing the {{ csrf_field() }} in the form this used to protect the form from tampering
the error is:
Route [users.destroy,$user->id] not defined. (View:
C:\xampp\htdocs\laravel\resources\views\users\index.blade.php)
in index.blade.php
#section('main')
<h1>All Users</h1>
<p><a href={!! url('users\create') !!}>Add new user</a></p>
#if ($users->count())
<table border="2">
<tr><td>s.n.</td><td>name</td><td>email</td><td>options</td>
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{!! $user->email !!}</td>
<td><a href={!! url('users\edit\{id}', $user->id) !!}>Edit</a></td>
<td><a href={!! url('users\delete\{id}', $user->id) !!}>Delete</a></td>
<td>
{!! Form::open(array('method' => 'DELETE',
'route' => array('users.destroy,$user->id'))) !!}
{!! Form::submit('Delete', array('class' => 'btn btn-danger')) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</table>
#else
There are no users
#endif
#stop
and the controller is:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
class UserController extends Controller
{
public function index()
{
$users=User::all();
return view('users.index', compact('users'));
}
public function create()
{
return View('users.create');
}
public function store()
{
$input = Input::all();
$validation = Validator::make($input, User::$rules);
if ($validation->passes())
{
User::create($input);
return Redirect::route('users.index');
}
return Redirect::route('users.create')
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
public function show($id)
{
//
}
public function edit($id)
{
$user = User::find($id);
if (is_null($user))
{
return Redirect::route('users.index');
}
return View('users.edit', compact('user'));
}
public function update($id)
{
$input = Input::all();
$validation = Validator::make($input, User::$rules);
if ($validation->passes())
{
$user = User::find($id);
$user->update($input);
return Redirect::route('users.show', $id);
}
return Redirect::route('users.edit', $id)
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
public function destroy($id)
{
User::find($id)->delete();
return Redirect::route('users.index');
}
}
and when i delete the form in view file index.blade.php the white blank page appears. earlier i didnot install html entities and it worked well except for the part of form.
Use this:
'route' => array('users.destroy', $user->id)
Then make sure you have users.destroy route in your routes.php file.
I am new to PHP and Laravel. I am making a Laravel login app which prints "success" after comparing the entered value and database value.
My problem is: when both value are matched it prints "success" but when they are not matched it shows ModelNotFoundException .
I have created an exception for this but again same error occurs.
Thank you in advance!
Here is my route.php code
route.php
Route::get('register', 'RegisterController#register');
Route::post('register/show', 'RegisterController#show');
Route::post('register/store', 'RegisterController#store');
Route::get('register/login', 'RegisterController#login');
Here is my register controller which get value from index.blade.php and from database named registers which has two columns username and password
RegisterController.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Register;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Request;
class RegisterController extends Controller {
public function register()
{
return view('register.index');
}
public function store()
{
$input = Request::all();
Register::create($input);
return redirect('register');
}
public function show()
{
try
{
$result2 = Request::get('username');
$result = Register::where('username', $result2)->firstOrFail();
return view('register.show', compact('result','result2'));
}
catch(ModelNotFoundException $e)
{
return "login fail" . redirect('register/login');
}
}
public function login()
{
return view('register.login');
}
}
Register.php
use Illuminate\Database\Eloquent\Model;
class Register extends Model {
protected $fillable = ['username', 'password'];
}
CreateRegistersTable
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRegistersTable extends Migration {
public function up()
{
Schema::create('registers', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('password');
$table->timestamps();
});
}
public function down()
{
Schema::drop('registers');
}
}
index.blade.php
#extends('master')
#section('login')
{!! Form::open(['url' => 'register/store']) !!}
{!!Form::label('username','Name:')!!}
{!!Form::text('username', null)!!}
<br>
{!!Form::label('password','Password:')!!}
{!!Form::text('password', null)!!}
{!!Form::submit('submit')!!}
{!! Form::close() !!}
#stop
login.blade.php
#extends('master')
#section('register')
{!! Form::open(['url' => 'register/show']) !!}
{!!Form::label('username','Name:')!!}
{!!Form::text('username', null)!!}
<br>
{!!Form::label('password','Password:')!!}
{!!Form::text('password', null)!!}
{!!Form::submit('submit')!!}
{!! Form::close() !!}
#stop
** show.blade.php **
#extends('master')
#section('show')
<?php
if( $result->username == $result2 )
{
echo "success";
}
?>
#stop
Have you added the error trapping logic to your app/Exceptions/Handler.php file?
Check out http://laravel.com/docs/5.0/eloquent ; specifically where it states:
Retrieving A Model By Primary Key Or Throw An Exception
Sometimes you may wish to throw an exception if a model is not found. To do this, you may use the firstOrFail method:
$model = User::findOrFail(1);
$model = User::where('votes', '>', 100)->firstOrFail();
Doing this will let you catch the exception so you can log and display an error page as necessary. To catch the ModelNotFoundException, add some logic to your app/Exceptions/Handler.php file.
use Illuminate\Database\Eloquent\ModelNotFoundException;
class Handler extends ExceptionHandler {
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException)
{
// Custom logic for model not found...
}
return parent::render($request, $e);
}
}
I am new to laravel and I am trying to use user model and controller doing singup => post after submitting the form I would like to get user info to store into users table, but I kept getting this error Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException from \laravel\framework\src\Illuminate\Routing\RouteCollection.php I am not sure what part I messed up, any suggestion would help. Thank you. :
// ====route
Route::get('/signup', 'UserController#getSignup');
Route::get('/login', 'UserController#getLogin' );
Route::resource('user', 'UserController');
// ====model
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password', 'remember_token');
}
//====user migration table
public function up() {
Schema::create('users', function($table) {
$table->increments('id');
$table->string('email')->unique();
$table->boolean('remember_token');
$table->string('password');
$table->timestamps();
});
}
//====userController
<?php
class UserController extends BaseController {
public function __construct() {
$this->beforeFilter('guest', array('only' => array('getLogin','getSignup')));
}
public function getSignup() {
return View::make('user_signup');
}
public function postSignup() {
# Step 1) Define the rules
$rules = array(
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6'
);
# Step 2)
$validator = Validator::make(Input::all(), $rules);
# Step 3
if($validator->fails()) {
return Redirect::to('/signup')
->with('flash_message', 'Sign up failed; please fix the errors listed below.')
->withInput()
->withErrors($validator);
}
$user = new User;
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
try {
$user->save();
}
catch (Exception $e) {
return Redirect::to('/signup')
->with('flash_message', 'Sign up failed; please try again.')
->withInput();
}
# Log in
Auth::login($user);
return Redirect::to('/list')->with('flash_message', 'Welcome to Foobooks!');
}
public function getLogin() {
return View::make('user_login');
}
public function postLogin() {
$credentials = Input::only('email', 'password');
if (Auth::attempt($credentials, $remember = true)) {
return Redirect::intended('/')->with('flash_message', 'Welcome Back!');
}
else {
return Redirect::to('/login')
->with('flash_message', 'Log in failed; please try again.')
->withInput();
}
return Redirect::to('login');
}
public function getLogout() {
# Log out
Auth::logout();
# Send them to the homepage
return Redirect::to('/');
}
}
//=====user_signup.blade.php
#extends('_master')
#section('title')
Sign up
#stop
#section('content')
<h1>Sign up</h1>
#foreach($errors->all() as $message)
<div class='error'>{{ $message }}</div>
#endforeach
{{ Form::open(array('url' => '/signup')) }}
Email<br>
{{ Form::text('email') }}<br><br>
Password:<br>
{{ Form::password('password') }}<br>
<small>Min: 6</small><br><br>
{{ Form::submit('Submit') }}
{{ Form::close() }}
#stop
'MethodNotAllowedHttpException' means that the route that is being hit isnt set up for the http type that has been sent.
So for /signup, you allow a GET request but your form is using POST to send data to it. Make a route that accepts POST to use the relevant controller and method.
Route::post('/signup', 'UserController#postSignup');
Edit:
Ah, I think it may be Route::resource('user', 'UserController') that's causing the problem. Laravel Docs says that ::resource('user', 'UserController') should have restful actions in your controller like UserController#index, UserController#create, UserController#store, etc.
Your controller can be autopopulated with the relevant actions using 'php artisan controller:make UserController' Note: this may delete what you currently have in place
If you're not utilising Route::resource to handle requests though, go ahead and remove it.