I get the error when trying to make a post call to /api/subject/search
I assume it's a simple syntax error I'm missing
I have my api routes defined below
Route::group(array('prefix' => 'api'), function()
{
Route::post('resource/search', 'ResourceController');
Route::resource('resource', 'ResourceController');
Route::post('subject/search', 'SubjectController');
Route::resource('subject', 'SubjectController');
Route::resource('user', 'UserController');
Route::controller('/session', 'SessionController');
Route::post('/login', array('as' => 'session', 'uses' => 'SessionController#Store'));
});
And my controller is mostly empty
class SubjectController extends \BaseController
{
public function search()
{
$subjects = [];
if((int)Input::get('grade_id') < 13 && (int)Input::get('grade_id') > 8)
$subjects = Subject::where('name', 'like', '%HS%')->get();
else
$subjects = Subject::where('name', 'not like', '%HS%')->get();
return Response::json([
'success' => true,
'subjects' => $subjects->toArray()
]);
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* 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 int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
You need to specify the method.
try
Route::post('subject/search', 'SubjectController#search');
See the named route example:
Laravel Docs
In your case I think search is not resolved by the controller to load the search() method. You are also sending a POST for search functionality and I guess it's better to do a GET request since POST and PUT are for storing data.
Conventions
When creating API's it's a good thing to stick to naming conventions and patterns.
http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api
Solution
Your route could be simpler like this: api.yourdomain.com/api/subject?search=term1,term2. Doing this with a GET query makes it going to the index() method. There you can check the GET params and do your search stuff and return.
Check this for the cleanest and truely RESTful way to make an API in Laravel:
How do I create a RESTful API in Laravel to use in my BackboneJS app
I got same error when accessing object at index of an empty array in view blade php file.
Related
I am trying to do something that seems to go out of the box with how laravel-nova works ...
I have a Batch model/ressource that is used by super admins. Those batch reeports belongs to sevral merchants. We decided to add a layer of connection to are portal and allow merchants to log in and see there data. So obviously, when the merchant visites the batch repport page, he needs to see only data related to it's own account.
So what we did was add the merchant id inside the batch page like this:
nova/resources/batch?mid=0123456789
The problem we then found out is that the get param is not send to the page it self but in a subpage called filter ... so we hacked it and found a way to retreive it like this:
preg_match('/mid\=([0-9]{10})/', $_SERVER['HTTP_REFERER'], $matches);
Now that we have the mid, all we need to do is add a where() to the model but it's not working.
Obviously, this appoach is not the right way ... so my question is not how to make this code work ... but how to approche this to make it so that merchants can only see his own stuff when visiting a controller.
All i really need to is add some sort of a where('external_mid', '=' $mid) and everything is good.
The full code looks like this right now:
<?php
namespace App\Nova;
use App\Nova\Resource;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\Currency;
use Laravel\Nova\Fields\BelongsTo;
use App\Nova\Filters\StatementDate;
use Laravel\Nova\Http\Requests\NovaRequest;
class Batch extends Resource
{
/**
* The model the resource corresponds to.
*
* #var string
*/
//
public static function query(){
preg_match('/mid\=([0-9]{10})/', $_SERVER['HTTP_REFERER'], $matches);
if (isset($matches['1'])&&$matches['1']!=''){
$model = \App\Batch::where('external_mid', '=', $matches['1']);
}else{
$model = \App\Batch::class;
}
return $model;
}
public static $model = $this->query();
/**
* The single value that should be used to represent the resource when being displayed.
*
* #var string
*/
public static $title = 'id';
/**
* The columns that should be searched.
*
* #var array
*/
public static $search = [
'id','customer_name', 'external_mid', 'merchant_id', 'batch_reference', 'customer_batch_reference',
'batch_amt', 'settlement_date', 'fund_amt', 'payment_reference', 'payment_date'
];
/**
* Indicates if the resource should be globally searchable.
*
* #var bool
*/
public static $globallySearchable = false;
/**
* Get the fields displayed by the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function fields(Request $request)
{
return [
ID::make()->hideFromIndex(),
Text::make('Customer','customer_name'),
Text::make('MID','external_mid'),
Text::make('Batch Ref #','batch_reference'),
Text::make('Batch ID','customer_batch_reference'),
Text::make('Batch Date','settlement_date')->sortable(),
Currency::make('Batch Amount','batch_amt'),
Text::make('Funding Reference','payment_reference')->hideFromIndex(),
Text::make('Funding Date','payment_date')->hideFromIndex(),
Currency::make('Funding Amount','fund_amt')->hideFromIndex(),
// **Relationships**
HasMany::make('Transactions'),
BelongsTo::make('Merchant')->hideFromIndex(),
// ***
];
}
/**
* Get the cards available for the request.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function filters(Request $request)
{
return [
];
}
/**
* Get the lenses available for the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function actions(Request $request)
{
return [];
}
}
In Laravel Nova you can modify the result query of any Resource by adding the index Query method. This method allows you to use Eloquent to modify the results with any condition you define.
I understand you just need to maintain the $model property with the model with the default definition and modify the results in the indexQuery method:
...
public static $model = \App\Batch::class;
public static function indexQuery(NovaRequest $request, $query)
{
// Using the same logic of the example above. I recommend to use the $request variable to access data instead of the $_SERVER global variable.
preg_match('/mid\=([0-9]{10})/', $_SERVER['HTTP_REFERER'], $matches);
if (isset($matches['1'])&&$matches['1']!=''){
return $query->where('external_mid', '=', $matches['1']);
}else{
return $query;
}
}
...
About the use of the PHP Global Variable, I recommend you to use the laravel default request() to look into your URL. You can use something like this $request->mid to read the value from the mid value in the URL.
I am using laravel 5.5 and I am trying to use a automatic route to the controller but it isn't working
In the web.php(the routing file for this version)
I have the follow line
Route::resource('panel', 'panel');
Route::resource('/', 'HomeController');
In the panel I have the follow actions
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class panel extends Controller{
public function index(){
return \View::make('panel.index');
}
public function registrar(){
return \View::make('panel.registrar');
}
}
but it's only calling the index() view
the registrar() view is not being called when user acess the url
site.com/panel/registrar
the follow erro is printing in the screen
"Method [show] does not exist on [App\Http\Controllers\panel]."
I tried to use the base_controller but it don't work too
"Class 'App\Http\Controllers\Base_Controller' not found"
is there an way to identify these actions ?
Resource routing sets up 7 specific routes, that is 7 specific methods you need on the controller, 7. If you dont want all 7 of those routes you have to define it that way.
Resource routing is not implicit controllers. It does not look at the method on the controller then make routes .. Resource routing is a 'specific' thing. We do not have implicit controllers any more in Laravel as there is really no point.
Laravel 5.5 Docs - Controllers - Resource Controllers
You have routes that are created that point to methods that don't exist, that is what the error is.
Also, the first argument to Route::resource is a resource 'name', not a PATH. It is not technically a URI. It is a name of a resource.
Route::resource('/', ...) // not a name
This is a resource controller with basic CRUD operations, so in order to work you have to define the rest methods like in your case you should add a method show() and then render the view you want in that method.
A resource controller must have the following methods defined:
class TestController 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)
{
//
}
}
And base controller obviusly it is not Base_Controller but its Controller
For more please reffer here Laravel 5.5 Resource Controllers
Change this resourse to simple get , if you don't need all resource methods
Route::get('/panel', 'panel#index');
Route::get('/panel/registrar', 'panel#registrar');
And use home instead just / to get unconflicted url
Route::resource('home', 'HomeController');
I have a form which submits data to the database. And it submits data to the database. But Laravel yields an error when it tries to redirect it to another method in the same controller.
ReflectionException in RouteDependencyResolverTrait.php line 57:
Internal error: Failed to retrieve the default value
Here is the controller I use. Please check the public function store(Request $request) method.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Inbox;
class ContactUsController 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()
{
return view('pages.contactus');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// validate the data
// store the data in the database
// redirect to another page
$this->validate($request, [
'name' => 'required|max:255',
'email' => 'required',
'telephone' => 'required',
'message' => 'required',
]);
$inbox = new Inbox;
$inbox->name = $request->name;
$inbox->email = $request->email;
$inbox->telephone = $request->telephone;
$inbox->message = $request->message;
$inbox->isnew = 1;
$inbox->category = $request->category;
$inbox->save();
// redirects to the route
//return redirect()->route('contactus.show', $inbox->id);
return redirect()->action(
'ContactUsController#show', ['id' => 11]
);
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
print_r($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 I tried, both of following methods in two different occasions. But Laravel shows the same error each time.
return redirect()->route('contactus.show', $inbox->id);
return redirect()->action(
'ContactUsController#show', ['id' => 11]
);
And here is the route file web.php.
Route::get('contactus', 'ContactUsController#create');
Route::get('contactus/show', 'ContactUsController#show');
Route::post('contactus/store', 'ContactUsController#store');
I have no idea what cause this problem. Any suggestion will be helpful.
Thanks!
You are passing the id value to the router in the redirect, but you are not telling the router to use this value in the route. Therefore, the router does not know to pass the id value into the show method.
Change the route to look like this:
Route::get('contactus/{id}', 'ContactUsController#show');
Then your redirect should work, and you should be redirected to (using the ID from your example) /contactus/11.
You don't have the $id defined on the route. You can either add that to the route with:
Route::get('contactus/show/{id}', 'ContactUsController#show');
OR
You can pass it as a get parameter and retrieve it in the request:
public function show(Request $request)
{
print_r($request->input('id'));
}
I had same error and it was because there was also parameter $request instead of Request $request
I would like to keep my authorization as DRY as possibile.
Currently for my update method I am using Laravel 5 FormRequest.
Example:
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
$commentId = $this->route('comment');
return Comment::where('id', $commentId)
->where('user_id', Auth::id())->exists();
}
The problem is that this gets triggered just for update method:
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update(UpdateRequest $request, $id)
{
// ...
}
How can I use the same authorize logic for edit method?
Otherwise everyone can just type:
/resources/ID/edit
^
not allowed
Only the owner should see that page, not everyone
Note I can't add that same UpdateForm to the edit method, because otherwise validation could be triggered too
/**
* Show edit form for the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function edit(UpdateRequest $request, $id)
{
// ...
}
You have something called middleware in laravel 5.
You can use it in the route so that it gets executed before the action has been called.
I think for what you need you will have to create your own middleware.
http://laravel.com/docs/master/middleware
After you created one you have to tell laravel to use the middleware and then add it to your route like so
route::get('test', ['middleware' => 'yourmiddleware', 'uses' => 'yourController#index']);
I am trying to make the laravel 4 controller in mac terminal by
php artisan controller:make UserController
It work's and insert the controller in the folder.
In my route.php i add:
Route::controller('users', 'UserController');
In my UserController in index i make
return "Hello world"
But when i am entering localhost/users it don't show anything, either in /users/create.
What can i do?
Trace errors:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
open: /Applications/XAMPP/xamppfiles/htdocs/salety/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php
* #param Exception $e
* #return void
*/
protected function handleRoutingException(\Exception $e)
{
if ($e instanceof ResourceNotFoundException)
{
throw new NotFoundHttpException($e->getMessage());
}
UserController
<?php
class UserController extends \BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
return "Hello world!";
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* 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 int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
You need to change Index to getIndex when using RESTful controllers.
What you've created using the artisan command is a resource controller.
To get this to work, change your routes.php file to this:
Route::resource('users', 'UserController');
This will make the /users route a resource and allow it to respond properly.
Be sure to look at the documentation on resource controllers and be sure to pay attention to the Actions Handled By Resource Controller section, as this gives you the key to what methods are used for which URI's.
Well for restfull controllers you need to use this form getIndex , getCreate , postRegister..etc , you can either use Route::controller() or Route::resource()
after changing stuff in your routes.php you need to run
php composer dump-autoload
to refresh the autoloading files with edited routes.