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.
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 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
So I keep getting this error when trying to get the name of the user who created a post.
My models look like this:
class User extends Authenticatable {
use Notifiable, CanResetPassword;
public function posts() {
return $this->hasMany('App\Post');
}
}
&
class Post extends Model {
public function user() {
return $this->belongsTo('App\User', 'user_id');
}
}
I try to display the name by doing this in my view:
#foreach($posts as $post)
<tr>
<td>{{ $post->user->name }}</td>
</tr>
#endforeach
And my controller looks like this:
public function getAdminIndex() {
$posts = Post::orderBy('id', 'desc')->get();
$comments = Comment::all();
$likes = Like::all();
$users = User::all();
return view('admin.index', ['posts' => $posts, 'comments' => $comments, 'likes' => $likes, 'users' => $users]);
}
Can anyone please tell me what I'm doing wrong?
Thank you very much!
It means not all posts have user, so do something like this:
<td>{{ optional($post->user)->name }}</td>
Or:
<td>{{ empty($post->user) ? 'No user' : $post->user->name }}</td>
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
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]);
}