laravel 5.5 FormRequest class is redirecting to me i need send array errors response - laravel

I have a problem when i validate a request with a FormRequest extended class. Because is redirecting when a bad request is recived and i need a response with the validation errors.
I'm using:
PHP 7.1.1 (cli) (built: Jan 18 2017 18:51:14) ( ZTS MSVC14 (Visual C++ 2015) x86 )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies.
Laravel v5.5.2.
My FormRequest class:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class BillRequest 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 [
'testfield' => 'required'
];
}
}
My Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\BillRequest;
use App\Bill;
class BillController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(BillRequest $request)
{
$bills = Bill::paginate(10);
return $bills;
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(BillRequest $request)
{
$bill = new Bill($request->all());
$bill->save();
return response('', 201);
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$bill = Bill::find($id);
$bill->customer->person;
$bill->vehicle;
$bill->items;
return response($bill, 200);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(BillRequest $request, $id)
{
$bill = Bill::find($id);
$bill->fill($request->all());
$bill->save();
return response('', 200);
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$bill = Bill::find($id);
$bill->delete();
return response('', 204);
}
}
Route (api.php):
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::group(['prefix' => 'admin' ], function () {
Route::resource('bills', 'BillController', [
'only' => ['index', 'update', 'show']
]);
});
Finally, the response with the field 'testfield' (in the request) is the JSON with the data paginated. But when i send the request without the field then redirect to localhost:8000/

I solved the problem. It's for a missing header in the request.
Content-Type:application/json
X-Requested-With:XMLHttpRequest

I experience the same problem using Laravel 5.5.3
There are many people who recommend to overwrite the response method in the custom FormRequest class, anyway this doesn't seem to work any longer since the method failedValidation is called before. This method throws an Illuminate\Validation\ValidationException.
You can catch this exception in app/Exceptions/Handler.php

To validate json in Laravel, check Laravel documentation
https://laravel.com/docs/5.5/validation#available-validation-rules

Related

Call to undefined method Illuminate\\Auth\\Access\\Response::authorize()

I'm trying to use Response::authorize() in ContactPolicy.php
but I got this error
Call to undefined method Illuminate\Auth\Access\Response::authorize()
<?php
namespace App\Policies;
use App\models\User;
use App\Contact;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;
class ContactPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can permanently delete the contact.
*
* #param \App\models\User $user
* #param \App\Contact $contact
* #return mixed
*/
public function forceDelete(User $user)
{
return $user->role === 'admin'
? Response::allow()
: Response::authorize();
}
}
I visit this website to check Response class Illuminate/Auth/Access/Response.html
then I checked file in my project vendor\laravel\framework\src\Illuminate\Auth\Access\Response.php
in my files I didn't find it ... all i find is this
<?php
namespace Illuminate\Auth\Access;
class Response
{
/**
* The response message.
*
* #var string|null
*/
protected $message;
/**
* Create a new response.
*
* #param string|null $message
* #return void
*/
public function __construct($message = null)
{
$this->message = $message;
}
/**
* Get the response message.
*
* #return string|null
*/
public function message()
{
return $this->message;
}
/**
* Get the string representation of the message.
*
* #return string
*/
public function __toString()
{
return (string) $this->message();
}
}
how this file miss the rest of methods the laravel docs talk about ??
tihs method requires laravel >= 6.x
this method not available in laravel 5
so make sure the laravel version in your project matches the laravel version that you browsing in laravel docs or in laravel API

"Class 'App\Models\Post' not found"

I'm trying to let my users made posts in my social media website. I already have a 'App\Models\Post'. How do I solve it??
Also the error appears when I try to submit the post, and the trouble is in the line that says: "$post = new Post();"
Ok, so here says that it looks like my post is mostly code, so I'll write no sense things so this pritty little thing go off. I'm not a native english speaker, so if you find a spelling or grammatical error please correct me :)
Here is the code of my Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post ;
use App\User;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('makePost');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function makePost(Request $request)
{
$this->validate($request, array(
'post' => 'required',
'title' => 'nullable|max:50',
'label' => 'nullable|max:25',
));
$post = new Post();
$post->post = $request->post;
$post->title = $request->title;
$post->label = $request->label;
$post->save();
return redirect()->route('index');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
And here is my Post Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function Users() {
return $this->belongsTo(User::class);
}
}
You defined the namespace in your Model wrong, if its in the Model directory change it to:
namespace App\Models;
If not you can always change your controller to:
use App\Post;
Try to use:
use App\Post;
Instead of use App\Models\Post ;

Laravel - empty result by trying to show a specific thread

My database is filled with data and showing all data works. But if I try to show a specific thread only, I get an empty result and I can't figure out why.
web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/threads', 'ThreadsController#index');
Route::get('/threads/{threads}', 'ThreadsController#show');
ThreadsController#index works, but ThreadsController#show doesn't work.
ThreadsController.php
<?php
namespace App\Http\Controllers;
use App\Thread;
use Illuminate\Http\Request;
class ThreadsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$threads = Thread::latest()->get();
return view('threads.index', compact('threads'));
//return $threads;
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function show(Thread $thread)
{
//return view('threads.show', compact('thread'));
return $thread;
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function edit(Thread $thread)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Thread $thread)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function destroy(Thread $thread)
{
//
}
}
This is an example from laracast tutorial: link
Why this example doesn't work? I can't find any typo in my code.
My version:
php artisan --version
Laravel Framework 5.4.32
Rename your show route to the following:
Route::get('/threads/{thread}', 'ThreadsController#show');
You are loading a Thread object, and not a Threads object.

how i can make a Resource Controllers

when I want create a Resource Controllers by this command : " php artisan make:controller sectionController3 "
I get a Basic Controllers
I don't add --plain
but he gives me Basic Controllers
sorry ^^
When you create a new controller from the command line laravel automatically insert a new resource controller like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class sectionController3 extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
After this you can create a new route for this controller:
resource('section-controller-3', 'sectionController3');
and this would handle all the RESTful actions about your sectionController3
Have a look to Laravel Docs HTTP Controller to get familiar with how Controllers and Routes work in Laravel.
Hope this helps!

Form request not working for controllers within subfolder of Controller folder

My folder structure for controllers is:
and I use following route code to access those controllers.
here I have allocated sub-folders inside Controller folder and updated namespaces as per needed on files and routes accordingly, this works perfectly, however when I use request file for validating form. It gives error:
Argument 1 passed to App\Http\Controllers\site\usersController::store()
must be an instance of Illuminate\Http\Request, string given
but when I move all controllers file to Controller folder and don't use sub-folder,form request validation works.My usersController and UserRegReq request files are:
usersController.php
<?php
namespace App\Http\Controllers\zcms;
use Illuminate\Http\Request;
use App\Services\FieldService;
use App\Services\UserService;
use App\Http\Requests;
use App\Http\Requests\UserRegReq;
use App\Http\Controllers\Controller;
class usersController extends Controller {
public function __construct(FieldService $field, UserService $user)
{
$this->field = $field;
$this->user = $user;
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
//
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function throwLogin()
{
return view('zcms.pages.login');
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
$field = $this->field->fieldList();
return view('zcms.users.addnew', compact('field'));
}
/**
* Store a newly created resource in storage.
*
* #param Request $request
* #return Response
*/
public function store(UserRegReq $request)
{
return "hello";
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param Request $request
* #param int $id
* #return Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
UserRegReq.php
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class UserRegReq 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 [
'field_id' => 'required',
'name' => 'required',
'username' => 'required',
'password' => 'required'
];
}
// public function messages(){
// return [
// 'field_id.required'=>'The related Field of your Job is required',
// ];
// }
}
What should I do beside updating namespace in routes and controller file to make everything work?

Resources