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
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
I am trying to save users to a followers table when one user follows another. When I try to get one user to follow another I get
Call to a member function follow() on integer
whenever I try to follow another user.
Follow Button/Form
{!! Form::open(['route' => 'follow_user']) !!}
{!! Form::hidden('id', $user->id) !!}
<button type="submit" class="btn btn-primary">Follow {{$user->name}}</button>
{!! Form::close() !!}
Route
Route::post('/follow', [
'as' => 'follow_user', 'uses' => 'FollowersController#store'
]);
Followers Controller
public function store()
{
$user1 = Auth::user()->id;
$user2 = Input::get('id');
$user1->follow($user2);
return redirect()->action('HomeController#index');
}
Methods I am using in User model
function followers()
{
return $this->belongsToMany('App\User', 'followers', 'user_id', 'follower_id');
}
function follow(User $user) {
$this->followers()->attach($user->id);
}
function unfollow(User $user) {
$this->followers()->detach($user->id);
}
You're trying to run follow() on a ID, not the User object (as you probably want).
This returns an integer:
$user1 = Auth::user()->id;
Maybe you want something like this:
$user1 = Auth::user();
$user2 = Input::get('id');
$user1->follow(User::find($user2));
Thanks to #blackpla9ue for the fix.
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'm trying to pass a variable from one view to a controller to another view. I'm not getting any errors, but when it gets to the last view, it doesn't show the variable like it's supposed to. In the first view, I'm just getting a name.
{{ Form::open(array('route' => 'form', 'method'=>'post')) }}
{{ $name = Form::text('name') }}
{{ Form::submit('Go!') }}
{{ Form::close() }}
Here is my HomeController.php.
public function view1()
{
return View::make('stuff');
}
public function postView1($name)
{
return Redirect::route('view2')->with($name);
}
public function view2($name)
{
return View::make('view2')->with($name);
}
routes.php
Route::get('/', array('as' => 'stuff', 'uses' => 'HomeController#stuff'));
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController#postView1'));
Route::get('view2/{name}', array('as' => 'view2', 'uses' => 'HomeController#view2'));
view2.blade.php
{{ $name = Input::get('name') }}
<p> Hello, {{ $name }} </p>
So why isn't it showing up?
First you should change your postView function into:
public function postView1()
{
return Redirect::route('view2', ['name' => Input::get('name')]);
}
And your route:
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController#postView1'));
into:
Route::post('form', array('as' => 'form', 'uses'=>'HomeController#postView1'));
Now, you should change your view2 function into:
public function view2($name)
{
return View::make('view2')->with('name',$name);
}
Now in your view2.blade.php you should be able to use:
<p> Hello, {{ $name }} </p>
You need to name the variable:
public function view2($name)
{
return View::make('view2')->with('name', $name);
}
class HomeController extends Controller {
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
}
public function index()
{
$data = array (
'title'=>'My App yo',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('home')->with($data);;
}
}
Try form will be if you using POST method why setting variable in route it will get directly on your function with post data.
{{ Form::open(array('url' => 'form', 'method'=>'post')) }}
{{Form::text('name') }}
{{ Form::submit('Go!') }}
{{ Form::close() }}
route :-
Route::post('form','HomeController#postView1');
controller function :-
public function postView1() {
$data = Input::all();
return Redirect::route('view2')->with('name', $data['name']);
}
and get data on view2 :-
<p> Hello, {{ $name }} </p>
For more follow HERE
Here's what other answers are missing, straight from Laravel docs:
Since the with method flashes data to the session, you may retrieve the data using the typical Session::get method.
So instead of {{$name}} write {{Session::get('name')}}.