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.
Related
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 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
Im trying to update the user object using a form facade in laravel, after submiting i get the error:
ErrorException in Grammar.php line 102:
Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array,
string given, called in
C:\Laravel Projects\ExpenseTool\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 105 and defined
My request:
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class UserRequest extends Request {
/**
* 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|min:3',
'email'
];
}
}
My HomeController index:
public function index()
{
$user = Auth::user();
return view('Expenses.home', compact( 'user'));
}
My form in view Expenses.home:
{!! Form::model($user, ['method' => 'PUT', 'action' => ['HomeController#update', $user->id]]) !!}
<div class="form-group">
{!! Form::label('Name', 'Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('Email', 'Email:') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Update', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
my route:
Route::resource('home', 'HomeController', ['only' => ['update']]);
my HomeController#update:
public function update($id, UserRequest $request)
{
$loggedUser = Auth::user();
$userDB = User::get($id);
$username = $request->input('name');
$userDB->name = $username;
$userDB->update();
return redirect('home');
}
What am i doing wrong?
you have to change
`public function update($id, UserRequest $request)
{
$loggedUser = Auth::user();
// you are cool to use findOrFail
$userDB = User::find($id);
$username = $request->input('name');
$userDB->name = $username;
$userDB->update();
return redirect('home');
}`
your error is causes by the get() method require you to pass an array of columns you would like to retrieves from database
Edit3: Could a reason be because both controllers are leading to the same page?
Edit2: Still not working after the answers I got.
Edit: Error one is solved, now I'm getting:
Undefined variable: project (View:
/var/www/resources/views/pages/showProject.blade.php)
Can this be because both variables are leading to the same page? The projects variable was working perfectly before the comment system.
public function index()
{
$projects = Project::all();
return view('pages.projects', compact('projects'));
}
Project variable declare.
I'm trying to get my comments from my database to show on a specific 'project page' in the laravel 5 project I'm working on. The idea is that the user can add art projects and other users can comment on them, but whenever I try to visit the page I get
Undefined variable: comments (View:
/var/www/resources/views/pages/showProject.blade.php)
This is my controller
public function index()
{
$comments = Comment::all();
return view('pages.showProject', compact('comments'));
}
public function store()
{
$input = Request::all();
$comment = new Comment;
$comment->body = $input['body'];
$comment->project_id = $input['project_id'];
$comment->user_id = Auth::user()->id;
$comment->save();
return redirect('projects/'.$input['project_id']);
}
These are my routes
// add comment
Route::post('projects/{id}','CommentController#store');
// show comments
Route::post('projects/{id}','CommentController#index');
And my view
#if (Auth::check())
<article> <!--Add comment -->
<br/>
{!! Form::open() !!}
{!! form::text('body', null, ['class' => 'form-control']) !!}
<br/>
{!! Form::Submit('Post Comment', ['class' => 'btn btn-primary form-control']) !!}
{!! Form::hidden('project_id', $project->id) !!}
{!! Form::close() !!}
<br/>
</article>
<article>
#foreach ($comments as $comment)
<article>
<p>Body: {{ $comment->body }}</p>
<p>Author: {{ $comment->user->name }}</p>
</article>
#endforeach
</article>
#else
<p>Please log in to comment</p>
#endif
The Model
class Comment extends Model
{
//comments table in database
protected $guarded = [];
// user who has commented
public function author()
{
return $this->belongsTo('App\User','user_id');
}
// returns post of any comment
public function post()
{
return $this->belongsTo('App\Project','project_id');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
public $timestamps = false;
}
Is there any way I can solve this?
Thanks in advance
First, you need to make sure that you are aliasing your 'Comment' model in your controller. This is done with the use statement.
use App\Comment;
class CommentController extends Controller
{
public function index()
{
$comments = Comment::all();
return view('pages.showProject', compact('comments'));
}
}
Second, you will need to change your route for showing comments from a POST request to a GET request. At the moment you are making identical routes and furthermore GET is the correct request type for retrieving data.
Route::get('projects/{id}','CommentController#index');
Third, you are referencing a $project variable in your view, but never passing it in from the controller. That needs to reference something.
I think your relationship is wrong. Try this:
Comment model:
class Comment extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
User model:
class User extends Model
{
public function comments()
{
return $this->hasMany('App\Comment');
}
}
In the view you can use:
#foreach($comments as $comment)
<p>{{ $comment->user->name }}</p>
#endforeach
I needed to empty the show in the commentsController and only use it for saving the comments. For showing them I made an extra function in my projectsController. It ended up being this;
public function show($id)
{
$project = Project::findOrFail($id)->load("User");
$input = Request::all();
//-----------------------DB-----------------------------//
$project_comments = DB::table('comments')
->select('body', 'name')
->where('project_id', '=', $id)
->join('users', 'users.id', '=', 'user_id')
->get();
//-----------------------DB-----------------------------//
return view('pages.showProject', ['project' => Project::findOrFail($id), 'comments' => $project_comments]);
}
My Route:
Route::controller('admin' , 'CategoriesController');
Controller:
<?php
class CategoriesController extends BaseController {
/**
* Display a listing of the resource.
* GET /categories
*
* #return Response
*/
public function __construct() {
$this->beforeFilter('csrf', array('on' =>'post' ));
}
public function getIndex()
{
return View::make('categories.index')
->with('categories', Category::all());
}
/**
* Show the form for creating a new resource.
* GET /categories/create
*
* #return Response
*/
public function postCreate()
{
$validator = Validator::make(Input::all(), Category::$rules);
if ($validator->passes()) {
$category = new Category;
$category->name = Input::get('name');
$category->save();
return Redirect::to('admin/categories/index')
->with('message' , 'Category created');
}
return Redirect::to('admin/categories/index')
->with('message' , 'Something went wrong')
->withErrors($validator)
->withInput();
}
/**
* Remove the specified resource from storage.
* DELETE /categories/{id}
*
* #param int $id
* #return Response
*/
public function postDestroy()
{
$category = Category::find(Input::get('id'));
if ($category){
$category->delete();
return Redirect::to('admin/categories/index')
->with('message' , 'category deleted');
}
return Redirect::to('admin/categories/index')
->with('message' , 'something went wronng');
}
}
Model:
<?php
class Category extends \Eloquent {
protected $fillable = array('name');
public static $rules = array('name'=>'required|min:3');
}
view/index.blade :
#section('content')
<div id="admin">
<h1>Categories Admin panel</h1>
<p>Here you can view, delete, and create new categories.</p>
<h2>Categories</h2>
<ul>
#foreach($categories as $category)
<li>
{{ $category->name }}
{{ Form::open(array('url'=>'admin/categories/destroy', 'class' => 'form-inline'))}}
{{ Form::hidden('id' , $category->id) }}
{{ Form::submit('delete')}}
{{ Form::close();}}
</li>
#endforeach
</ul>
<h2>Create New Category</h2>
#if($errors->has)
<div id="form-errors">
<p>The following erros</p>
<ul>
#foreach($errors->all() as $error)
<li>{{ $errors }}</li>
#endforeach
</ul>
</div><!--end form-errors-->
#endif
{{ Form::open(array('url'=>'admin/categories/create'))}}
<p>
{{ Form::label('name')}}
{{ Form::text('name')}}
</p>
{{ Form::submit('Create Category', array('class' => 'secodary-cart-btn'))}}
{{ Form::close()}}
</div><!--end admin-->
#stop
The Problem is: When I run this code in my browser it show undefined variable: category in index.blade.php.
Please check your route you are trying to access in your browser.
Problem is, you are trying to use RESTful Controllers. In that case, you have to access the URL in the restful method.
If you want to access the getIndex() Method in your restful controller, then in your browser, you have to access
http://www.yoursite.com/admin/index
Basically, Route::controller('admin','CategoriesController'); Means, when you have /admin/ in your URL, it will access this CategoriesController. Then the verbs get followed by the URL path will be used.
If you want to take the non RESTful route, then do it one of the following way
Pass the $categories array like this in your route
Route::get('/', function(){
return View::make('categories.index')->with('categories',Category::all());
});
OR
Route::get('/',array('uses'=>'CategoriesController#getIndex'));
And then in your CategoriesController
public function getIndex(){
return View::make('categories.index')
->with('categories', Category::all());
}
Should Fix it :)