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
Related
I know this question has been asked about earlier versions of Laravel, but I"m using 5.7.x (the latest) and what worked in 5.2 might not be applicable in my case. Basically, I'm trying to create a Post form validated by a custom FormRequest. Here are my source files.
post.create.blade.php
<html>
#include('header')
<body>
<h1>Add a New Post</h1>
{!! Form::open(['route' => 'save_post']) !!}
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null) !!}
</div>
<div class="form-group">
{!! Form::label('body', 'Body:') !!}
{!! Form::textarea('body', null) !!}
</div>
{!! Form::submit('Create', ['class' => 'btn btn-info']) !!}
{!! Form::close() !!}
<div class="alert alert-danger">
<ul>
#if($errors->any())
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
#endif
</ul>
</div>
</body>
</html>
web.php
Route::get('post/create', function () {
return view('post_create');
});
PostController.php
<?php
namespace App\Http\Controllers;
use App\Post;
use App\Http\Requests\PostCreateRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
public function index()
{
return Post::paginate(1);
}
public function show($id)
{
return Post::find($id);
}
public function store(PostCreateRequest $request)
{
$post = Post::create($request->all());
$post->save();
return response()->json($post, 201);
}
public function update(Request $request, $id)
{
$post = Post::findOrFail($id);
$post->update($request->all());
return response()->json($post, 200);
}
public function delete(Request $request, $id)
{
$post = Post::findOrFail($id);
$post->delete();
return response()->json(null, 204);
}
PostCreateRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Foundation\Validation\ValidatesRequests;
class PostCreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name' => 'required|string|max:255',
'body' => 'required|string|max:4096'
];
}
}
Clearly the validator is working. When I fill out the name and body, it adds a post to the SQL database on the backend. And when it fails, it goes back to the post create view. The problem is that the validator errors don't show up in the view even though I have it coded in. What exactly is going on? Is there some sort of bug in Laravel?
UPDATE: Oh, and in case anyone's curious, I'm using Ubuntu. Some sources suggest this used to matter before. I'm not sure if it still does.
I am trying to execute 2 queries at the same time on the same function on my application. This function on the controller registers a new house in the system and at the same time, its suppose to feed a field in the Users table that has been null, and this field will then be updated with the new id of the house i have just created. For some reason, the first query works fine but i have been struggling with the second. I believe my logical is fine, maybe the Laravel syntax is making me a bit confused. Can someone help me?
This is my controller
<?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());
$house_admin = Auth::user()->id;
$house = House::where('house_admin', $house_admin)->first();
User::findOrFail($house_admin)->update(['house_id' => $house->id]);
return redirect('home');
}
public function show($id){
}
public function edit($id){
}
public function update(Request $request, $id){
}
public function destroy($id){
}
}
This is my form
//i believe the problem is not here, but anyway
#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
And finnaly, this my router web.php
//i also believe the problem is not here, but in my controller
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 need to add the house_id field to the $fillable array in the User model:
protected $fillable = ['house_id', 'other_fields'];
Also, you have two foreign keys instead of one which is redundant. You might want to keep only house_admin foreign key.
I'm looking for some help. I've searched on other topics, and saw what is the problem approximatively, but didn't succeed to fix it on my code.
Now the question is: I have NotFoundHttpException when i try to submit an update on my code.
Here is the Controller and my function update
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use App\T_collaborateurs_table;
class testing extends Controller
{
public function index()
{
$user = T_collaborateurs_table::all();
return view ("read", compact("user"));
}
public function create()
{
return view("create");
}
public function store(Request $Request)
{
T_collaborateurs_table::create(Request::all());
return redirect("index");
}
public function show($id)
{
$user=T_collaborateurs_table::find($id);
return view("show", compact("user"));
}
public function edit($id)
{
$user=T_collaborateurs_table::find($id);
return view("update", compact("user"));
}
public function update(Request $Request, $id)
{
$user = T_collaborateurs_table::find($id);
$user->update(Request::all());
return redirect("index");
}
}
Now the routes
Route::get("create", "testing#create");
Route::post("store", "testing#store");
Route::get("index", "testing#index");
Route::get("show/{id}", "testing#show");
Route::get("edit/{id}", "testing#edit");
Route::patch("update/{id}", "testing#update");
And now the view update.blade.php
<body>
{{Form::model($user, ['method'=>'patch', 'action'=>['testing#update',$user->id]])}}
{{Form::label('Id_TCa', 'ID')}}
{{Form::text('Id_TCa')}}
{{Form::label('Collaborateur_TCa', 'collab')}}
{{Form::text('Collaborateur_TCa')}}
{{Form::label('Responsable_TCa', 'resp')}}
{{Form::text('Responsable_TCa')}}
{{Form::submit("update")}}
{{Form::close()}}
</body>
Here the route:list
I'm sorry if my words are not very understable...
Thank you all for your time.
{{Form::model($user, ['method'=>'PATCH', 'action'=> ['testing#update',$user->id]])}}
Or try to use 'route' instead of 'action',to use 'route' you just need a little edit in your update route.
Route::patch("update/{id}", array('as' => 'task-update', 'uses'=>'testing#update'));
in your view:
{{Form::model($user, ['method'=>'PATCH', 'route'=>['task-update',$user->id]])}}
And please follow the convention of class naming. Your class name should be 'TestingController' or 'Testing'.
You could try method spoofing by adding
{{ method_field('PATCH') }}
in your form and change the form method to POST
{{ Form::model($user, ['method'=>'POST', 'action'=>['testing#update', $user->id]]) }}
add the id as an hidden field
{{ Form::hidden('id', $user->id) }}
access the id in the controller as
public function update(Request $Request)
{
$id = Input::get('id');
$user = T_collaborateurs_table::find($id);
$user->update(Request::all());
return redirect("index");
}
also need to modify your route accordingly
Route::patch("update", "testing#update");
Try using on function update:
return redirect()->route('index');
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
I want to pass the selected item's $id to my controller for doing changing on it .
it's my index.blade.php (view)code
<table>
#foreach($posts as $p)
<tr>
<td>{{$p->title}}</td>
<td>{{substr($p->body,0,120).'[...]'}}</td>
<td>{{HTML::link('posts_show',' Preview',array($p->id))}}</td>
<td>{{HTML::link('posts_edit','Edit',array($p->id))}}</td>
<td>
{{Form::open(array('method'=>'DELETE','url'=>array('posts.delete',$p->id)))}}
{{Form::submit('Delete')}}
{{Form::close()}}
</td>
</tr>
#endforeach
</table>
but it doesnt pass $id to my controller's methods.
thanks for your time.
What you need to do is to set route parameter. Your route should be like that.
Route::get('post','postController#index');
Route::get('posts_create', function (){ return View::make('admin.newPost'); });
Route::get('posts_show/{id}','postController#show');
Route::get('posts_edit/{id}','postController#edit');
Route::post('posts_delete/{id}','postController#destroy');
If you want to use named route {{ Form::open(array('url' => route('posts.edit', $p->id))) }}, you need to set name like that.
Route::post('posts_edit/{id}', array('uses' => 'postController#edit',
'as' => 'posts.edit'));
You can check routing in laravel official documentation.
Edit
For now, your form in view look like that.
{{ Form::open(array('url' => route('posts.edit', $post->id), 'method' => 'POST')) }}
In route,
Route::post('posts_edit/{id}', array('uses' => 'postController#edit',
'as' => 'posts.edit'));
In PostController,
public function edit($id)
{
// do something
}
I hope it might be useful.
Looks like your route has a name posts.destroy, if that is the case you should use route instead of url as a parameter
{{Form::open(array('method'=>'DELETE','route'=>array('posts.destroy',$p->id)))}}
it's my route:
Route::get('post','postController#index');
Route::get('posts_create', function (){ return View::make('admin.newPost'); });
Route::get('posts_show','postController#show');
Route::get('posts_edit','postController#edit');
Route::post('posts_delete','postController#destroy');
it's my postController:
class postController extends BaseController{
public function show($id){
$post=Post::find($id);
$date=$post->persian_date;
return View::make('posts.show')->with('post',$post)->with('date',$date);
}
public function edit($id){
$post=Post::find($id);
if(is_null($post)){
return Redirect::route('posts.index');
}
return View::make('posts.edit')->with('post',$post);
}
public function update($id){
$input=array_except(Input::all(),'_method');
Post::find($id)->update($input);
return Redirect::route('posts.index');
}
public function destroy($id)
{
Post::find($id)->delete();
return Redirect::route('posts.index');
}