Method name must be a string error in laravel 5.2 - laravel

Im getting this error when try to insert data into databse in laravel 5.2.
Check out through this code and find what i did as error
Controller.php
public function store(Request $request){
$this->$request([
'title'=>'required',
'description' => 'required'
]);
Item::create($request->all());
return redirect()->route('itemCRUD.index')
->with('success', 'Item created Successfully');
}
create.php (short code)
{!! Form::open(array('route' => 'itemCRUD.store','method'=>'POST')) !!}
{!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}
{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px')) !!}
<button type="submit" class="btn btn-primary">Submit</button>
{!! Form::close() !!}
please help me to find out the error

Change:
$this->$request([
'title'=>'required',
'description' => 'required'
]);
to
$this->validate($request, [
'title'=>'required',
'description' => 'required'
]);
Hope this helps!

Related

laravelcollective select option in laravel

How to show value select option when install laravelcollective /html in laravel?
blade
<div class="row">
<div class="col-md-6 form-group">
<label for="target">Categoory</label>
{!! Form::select('categories', $categories, null, [
'class' => 'form-control',
'id' => 'target',
]) !!}
</div>
</div>
controller
public function index()
{
$categories = Category::all();
return view('user::admin.users.index', compact('categories'));
}
demo
You should print your categories like this:
{!! Form::select('categories', $categories->pluck('name', 'id'), null, [
'class' => 'form-control',
'id' => 'target',
]) !!}

Email Sending and Validation Failing in Laravel 5.4

I am creating a contact form using bootstrap3 build on Laravel 5.4. When I click the submit button, I expect an email to be sent to my inbox or if there are errors they should be validated on the back end and errors displayed at the top of the form. I am using Laravel collective to build the form, when I fill the form and click the submit button, the page only reloads and no validation happens or in case of correct input no email is sent. Please assist?
Form Section
<div class="col-sm-4 wow animated fadeInLeft">
<div id="success" class="col-sm-12">
#if(Session::has('success'))
<span class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</span>
#endif
#if(count($errors) > 0)
<span class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</span>
#endif
</div>
{!! Form::open(array('route' => 'index.post', 'method' => 'POST','class' => 'contact-form')) !!}
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => ''))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => ''))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Routes File
Route::post('/', 'PagesController#postIndex') ->name('index.post');
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
PagesController
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#pwebk.com');
});
Session::flash('success', 'Hello '.$name.', Your Form was successfully sent');
return redirect()->route('pages.index');
}
public function getIndex(){
return view('pages.welcome');
}

Alert Message not showing in Laravel 5.4

I have got a contact form that is build using Laravel 5.4, parsley.js and Bootstrap 3, it works fine but it does not display a success message at the top of the form on successful delivery of a message or display any errors in case there is an error. Please assist?
Contoller
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#kapsol.com');
});
Session::flash('success', 'Hello $name, Your Form was successfully sent');
return redirect()->route('pages.index');
}
Index.blade.php
<div class="col-sm-4">
{!! Form::open(array('route' => 'index.post', 'class' => 'contact-form', 'data-parsley-validate' => '')) !!}
<div id="success">
<div class="col-sm-12">
#if(Session::has('success'))
<div class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</div>
#endif
#if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</div>
#endif
</div>
</div>
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => '', 'minlength' => '10'))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => '', 'type' => 'email'))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input', 'minlength' => '100')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Route
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
Route::post('/', 'PagesController#postIndex') ->name('index.post');
You are redirecting to the same route your form is posting. I assume that the route that render the view is a GET route and have another name, you should redirect to that route or the route that renders index.blade.php
This is why you have the with() method to chain the route() method on. So rather than use Session:flash(), you can simply add a with() method that flashes that message to the session for the next request:
return redirect()->route('pages.index')->with('success', 'Hello'. $name.', Your Form was successfully sent');
Or rather if your form was originally from the index page, then you simply don't have to remember the name of the route, simply use back() helper method, i.e:
return back()->with('success', 'Hello'. $name.', Your Form was successfully sent');

I am getting error message

Getting error : syntax error, unexpected '=>' (T_DOUBLE_ARROW)
my code is :
#extends('layouts.masters.main')
#section('page-content')
<div class="container">
#include('layouts.partials.nav')
{!! Form::open(['route' => 'post_register', 'id' => 'registration-form']) !!}
{!! Form::label('name', 'Full Name') !!}
{!! Form::text('name', null, ['id' => 'name', 'class' => 'form-control', 'placeholder' => 'Full Name', 'required']) !!}
{!! Form::label('email', 'Email Address') !!}
{!! Form::email('email',null,['id' => 'email', 'class' => 'form-control', 'placeholder' => 'Email Address', 'required']) !!}
{!! Form::label('password', 'password') !!}
{!! Form::password('password',['id' => 'password', 'class' => 'form-control', 'placeholder' => 'password', 'required']) !!}
{!! Form::button('Register','class' => 'btn btn-lg btn-primary btn-block', 'type' => 'submit')!!}
{!! Form::close() !!}
</div> <!-- /container -->
#stop
please help ... i can't uderstand where i do the mistake in this code
When you use "=>" the content should be inside an array (class, type, etc..)
So, try this;
{!! Form::button('Register', ['class' => 'btn btn-lg btn-primary btn-block', 'type' => 'submit']) !!}

Laravel validation not showing the default error messages

I am working on a laravel project and I am having some issues with the validation.
The default error messages dont appear instead I get to see the validation requirements like this: http://gyazo.com/681e9d8e2e176a29d90db041354f7177
this is my code:
routes.php (I put all the code in here for now)
Route::filter('checkLogin', function()
{
if(Input::GET('email') != ""){ //register
$rules =
array(
'username' => 'required|max:64|min:3|unique:users',
'password' => 'required|max:64|min:6',
'fname' => 'required|max:255|alpha',
'lname' => 'required|max:255|alpha',
'email' => 'required|max:255|email',
'phone' => 'max:24|min:9',
'zip' => 'required',
'street' => 'required|max:255|alpha',
'housenumber' => 'required|max:6|numeric',
'country' => 'required',
'avatar' => 'max:32'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()) {
return Redirect::to('/')->withInput()->withErrors($rules);
}
}
});
this is how the code from the view:
<div class="fields">
<div class="field">
<i class="fa fa-user"></i>
{{ Form::text('username', null, ['placeholder' => 'Username', 'tabindex' => 1]) }}
{{ $errors->first('username') }}
</div>
<div class="field">
<i class="fa fa-lock"></i>
{{ Form::password('password', ['placeholder' => 'Password', 'tabindex' => 2]) }}
{{ $errors->first('password') }}
</div>
</div>
When validation fails, you are returning your $rules as the errors. Change this line:
return Redirect::to('/')->withInput()->withErrors($rules);
to this:
return Redirect::to('/')->withInput()->withErrors($validator);

Resources