Laravel 5.3 trying to get property of non-object - laravel

I just updated my application into version 5.3 and in many routes I'm getting
trying to get property of non-object
Like for example I have a resource called post and another called Article where each post can have one Articles. And each Article has one writer. When I do
$article->post->writer;
I get trying to get property 'writer' of non-object
Before updating from 5.2 this route as well as other routes worked perfectly. Am i missing something here? should i do something after composer update has been run successfully?
Edit: The article, the post and the writer all exist in the DB. When I try to return $article I get this.
{
"article": []
}
When i switch back to laravel 5.2 I get the proper article
Edit 2: it is routing bidnings problem. I'm using implicit model binding, so when i pass the ID of the article to my route, Laravel fetches the resource. but Now apparently it doesn't do it. I followed the instructions here for the bindings https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0 but Laravel can't still get the article

It was name bindings issue. Laravel 5.3 needs some configuration in kernel.php as well as in the group route https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0
The group should have 'middleware' => ['bindings']

Related

Laravel not picking up the correct route

I am developing an application using Laravel 8.x. I have an api.php file that I am using. It was all working fine for the last 5 months. Today I added a new route to the api.php file and that route is not getting picked up by the Laravel. I have the following items in the api.php file
//Products
Route::get('/products/{currency_id?}/{type?}/{eventtype?}', [Controllers\ProductsController::class, 'getproduct2']);
Route::get('/products/search/{query}/{itemtype?}/{itemcategory?}', [Controllers\ProductsController::class, 'findproduct']); //getproduct
Route::post('/products/findproductbyid/{product_id}', [Controllers\ProductsController::class, 'getproduct']);
Route::post('/products/create', [Controllers\ProductsController::class, 'setproduct']);
Route::post('/products/update', [Controllers\ProductsController::class, 'putproduct']);
And it shows up the URL list also
but when I access it (findproductbyid) in the browser it is not getting picked up. it shows the results of /products/{currency_id}/{type}/{eventtype}
Please tell me to know why is it so? I tried clearing Laravel cache, etc.
Thanks in advance
UPDATE: I tried changing the GET to POST, but it seems it is not getting updated to POST
You don't define url parameters with $, here you done {$product_id}
Change it to the following.
Route::get('/products/findbyproductid/{product_id}', [...]);
Rearrange the routes like this and try it out
Route::post('/products/findproductbyid/{product_id}', [Controllers\ProductsController::class, 'getproduct']);
Route::get('/products/search/{query}/{itemtype?}/{itemcategory?}', [Controllers\ProductsController::class, 'findproduct']); //getproduct
Route::get('/products/{currency_id?}/{type?}/{eventtype?}', [Controllers\ProductsController::class, 'getproduct2']);

Route [x] not defined when using auth middleware

I am making a website with laravel but i am getting an error while using middleware auth. it says Route[x] not defined.
i am using laravel 5.8 please some one help me
The route you have written that has no named route so you need to specify name for that route like this
Route::get('x', function() {
//
})->name('x')->middleware('auth');
for more information you can check here https://laravel.com/docs/7.x/urls#urls-for-named-routes

Merging laravel dingo with laravel-modules and caching config

Here is the problem.
I started a dingo project and am using laravel-modules in it. Every module has its own routing files. Using the project in development environment, everything works fine.
But when I run php artisan config:cache, when a request comes to laravel, it return the response The version given was unknown or has no registered routes. As I see, the problem is dingo just check the default api.php and web.php files to find the route. But module routes are not stored in that files. I store them in Modules/module_name/route/api.php file (as laravel-modules suggested).
Any suggestion would be welcome.
change api file of module with version param in group session like this:
$api = app('Dingo\Api\Routing\Router');
$api->group(['version' => 'v1'], function ($api) {
...
});

Laravel 5.3 Route::resource without REST

I upgrade Laravel 5.1 to 5.3 and have some problem with routes.
In Laravel 5.1 I have route like:
Route::controllers([
'pages/{page_type}' => 'Admin\AdminPagesController',
]);
And in controller I have methods like:
getIndex($type)
postIndex($type, Request $request)
getAdd($type)
postAdd(Request $request)
getEdit($type, $id)
postEdit(Request $request, $id) and others...
But in 5.3 when I created routes:
Route::resource('pages/{page_type}', 'Admin\AdminPagesController');
I got an error
NotFoundHttpException in RouteCollection.php line 161:
or
Route pattern "/master/pages/{page_type}/{{page_type}}" cannot reference variable name "page_type" more than once.
and it generate me route in RESTful
Can anyone help me?
Thanks.
Since there is not alternative to ::controller you need to create separate route for each action if you don't want to use rest:
Route::get('pages/{page_type}', 'Admin\AdminPagesController#getIndex');
Route::post('pages/{page_type}', 'Admin\AdminPagesController#postIndex');
....
Seems that Route::controllers method has been removed in Laravel 5.2, I can't find it in the documentation since then, and doesn't exist in the Illuminate\Routing\Router.php file in Laravel 5.3
You will have to create each route separately for your case. Or you can simply use the Route::resource method, what do you have against it? You can add extra methods to a resource declaring them before the Route::resource call.

Laravel 4 - changing resource root routing path

In a Laravel 4 installation, Using Jeffrey Way's Laravel 4 Generators, I set up a 'tweet' resource, using the scaffolding command from his example:
php artisan generate:scaffold tweet --fields="author:string, body:text"
This generated the model, view, controller, migration and routing information for the tweet type. After migrating the database, visiting http://localhost:8000/tweets works fine, and shows the expected content.
The contents of the routes.php file at this point is:
Route::resource('tweets', 'TweetsController');
Now I would like to move the url for tweets up one level into admin/tweets, so the above url should become: http://localhost:8000/admin/tweets. Please note that I am not treating 'Admin' as a resource, but instead just want to add it for hypothetical organizational purposes.
Changing the routes.php file to:
Route::resource('admin/tweets', 'TweetsController');
Does not work, and displays the following error:
Unable to generate a URL for the named route "tweets.create" as such route does not exist.
Similarly when using the following:
Route::group(array('prefix' => 'admin'), function() {
Route::resource('tweets', 'TweetsController');
});
As was suggested in this stackoverflow question.
Using php artisan routes reveals that the named routes also now have admin prefixed to them, turning tweets.create into admin.tweets.create.
Why is the error saying that it cannot find tweets.create? shouldn't that automatically be resolved (judging by the routes table), to use admin.tweets.create?
How can I change my routing so that this error no longer occurs?
I just tested with new resource controller and it works fine for me.
The problem is not with the Route, its with the named routes used in your application.
check your view files there are link to route like link_to_route('tweets.create', 'Add new tweet'), this is creating the error because when you add admin as prefix tweets.create doesn't exists so change it to admin.tweets.create every where, in your controller also where ever named route is used.

Resources