Showing data in Laravel Chatter Package is showing Undefined Variable Error - php

I am working on adding a comment like stackoverflow using chatter discussion package by devdojo so here is the problem I am writing code to show comments but an undefined variable error is coming up.
Error Page ScreenShot
public function show(Chatterreply $chatterreply ,$id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
return view('chatter::discussion', compact('chatterreplies'));
echo "<pre>"; print_r('$chatterreplies'); die;
}
In Web.php route is
/*
* Post routes.
*/
Route::group([
'as' => 'posts.',
'prefix' => $route('post', 'posts'),
], function () use ($middleware, $authMiddleware) {
// All posts view.
Route::get('/', [
'as' => 'index',
'uses' => 'ChatterPostController#index',
'middleware' => $middleware('post.index'),
]);
// Create post view.
Route::get('create', [
'as' => 'create',
'uses' => 'ChatterPostController#create',
'middleware' => $authMiddleware('post.create'),
]);
// Store post action.
Route::post('/', [
'as' => 'store',
'uses' => 'ChatterPostController#store',
'middleware' => $authMiddleware('post.store'),
]);
//Adding Comments
Route::post('/reply/{id}', [
'as' => 'store',
'uses' => 'ChatterreplyController#store',
'middleware' => $authMiddleware('post.reply.store'),
]);
//showing Comment
Route::get('/reply/{id}', [
'as' => 'show',
'uses' => 'ChatterreplyController#show',
'middleware' => $middleware('post.show'),
]);

First, I would suggest putting your debugging statements (...print_r...) before the return statement in your controller action, this way :
public function show(Chatterreply $chatterreply ,$id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
echo "<pre>"; print_r('$chatterreplies'); die();
// or use the laravel helper
dd($chatterreplies)
return view('chatter::discussion', compact('chatterreplies'));
}
You should see the content of the $chatterreplies variable.
If this is ok, check your controller name in web.php because it seems that it should be ChatterReplyController#show instead of ChatterreplyController#show (is the R letter in ChatterreplyController#show capital or not ? ) if you're following the camelCase convention as in ChatterPostController#store for instance.

Related

Similar route in group of route with different guard in laravel

I defined two different guard in auth.php file like this :
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'website' => [
'driver' => 'session',
'provider' => 'websites',
]
]
There is two different section routes in web.php routes. one for website admn and another for normal users that is a member of those websites. like this :
Route::prefix('/website/{website}')->middleware('auth:website')->group(function () {
Route::group(['prefix' => 'banner_ads'], function () {
Route::get('banner_adsDatatable', ['as' => 'banner_adsDatatable', 'uses' => 'BannerAdsController#banner_adsDatatable']);
});
Route::resource('/banner_ads', 'BannerAdsController');
Route::prefix('/member/{member}')->middleware('auth:web')->group(function () {
Route::group(['prefix' => 'banner_ads'], function () {
Route::get('banner_adsDatatable', ['as' => 'banner_adsDatatable', 'uses' => 'BannerAdsController#banner_adsDatatable']);
});
Route::resource('/banner_ads', 'BannerAdsController');
});
});
Problem is that I have a resource controller (banner ads) that is shared with to users (website admins and members). As you can see I must to add it twice time.
But beacause for normal user I defined banner ads controller again, when I call banner_ads.update for example always return unAuthenticated user.
I do not know what can I do for solve this problem.
Seems like your auth:web middleware is inside auth:website. This is basically checking for an authenticated admin who is trying to access routes of an authenticated user.
You are trying to access routes as a normal user. So it is not working. Just replace your code with this:
Route::prefix('/website/{website}')->middleware('auth:website')->group(function () {
Route::group(['prefix' => 'banner_ads'], function () {
Route::get('banner_adsDatatable', ['as' => 'banner_adsDatatable', 'uses' => 'BannerAdsController#banner_adsDatatable']);
});
Route::resource('/banner_ads', 'BannerAdsController');
});
Route::prefix('/member/{member}')->middleware('auth:web')->group(function () {
Route::group(['prefix' => 'banner_ads'], function () {
Route::get('banner_adsDatatable', ['as' => 'banner_adsDatatable', 'uses' => 'BannerAdsController#banner_adsDatatable']);
});
Route::resource('/banner_ads', 'BannerAdsController');
});

Extracting values from route in Laravel

I want to retrieve contact_id and hash values from my route. In my PictureController I have pictures function where I want to use them.
My route:
Route::get('/static/contacts/{contact_id}/picture/{hash}', [
'as' => 'platform.api.contacts.picture.hash',
'uses' => 'PictureController#pictures'
]);
I suppose this is not enough?
public function picture ($contactId, $hash)
Your Route contains little wrong.
Route::get('/static/contacts/{contact_id}/picture/{hash}', [ 'as' => 'platform.api.contacts.picture.hash', 'uses' => 'PictureController#pictures' ]);
Insted of use
Route::get('/static/contacts/{contact_id}/picture/{hash}', [ 'as' => 'platform.api.contacts.picture.hash', 'uses' => 'PictureController#picture' ]);
Your function name was wrong as you used in controller. Look at the function name in route pictures and controller you used picture.

Dynamic Route name Laravel 5.2

I want to create dynamic route name for my app. Here is my route file
Route::group(['prefix' => '{team}/dashboard', 'middleware' => 'isMember'], function() {
Route::get('/user', array('uses' => 'UserController#index', 'as' => 'user.index'));
Route::get('/user/edit/{id}', array('uses' => 'UserController#edit', 'as' => 'user.edit'));
Route::patch('/user/{id}', array('uses' => 'UserController#update', 'as' => 'user.update'));
Route::delete('/user/{id}', array('uses' => 'UserController#destroy', 'as' => 'user.delete'));
it's not simple if i have to define route like this
'route' => ['user.delete', $team, $user->id]
or
public function destroy($team,$id) {
// do something
return redirect()->route('user.index', $team);
}
I want to generate route name like "$myteam.user.delete" or something more simplier like when i define "user.delete" it includes my team name.
How i can do that? is it possible?
You could do that by setting as. Also using resource routes will be handy.
$routeName = 'team.';
Route::group(['as' => $routeName], function(){
Route::resource('user', 'UserController');
});
Now you can call like
route('team.user.index');
More on resource routes here https://laravel.com/docs/5.3/controllers#resource-controllers
try this:
Route::delete('/user/{team}/{id}', array('uses' => 'UserController#deleteTeamMember', 'as' => 'myteam.user.delete'));
Now call the route as:
route('myteam.user.delete', [$team, $id]);

Laravel advanced routing system

I'm trying to implement a simple CMS with Laravel 5.2 which basically handles two kinds of routes. The first one is used to browse a website view, that has to be {view}.html. The controller iterates the database records and if it can't find that page, will return a 404 error page:
Route::get('/{page}', [
'as' => 'page',
'uses' => 'Website\WebsiteController#showPage'
])->where(['page' => '.+(\.html)']);
For example these routes will match:
www.mydomain.ext/homepage.html
www.mydomain.ext/about.html
www.mydomain.ext/news.html
www.mydomain.ext/contact.html
and so on. The second one is a route group for the admin control panel:
Route::group([
'prefix' => env('ADMIN_PREFIX', 'admin'),
'as' => env('ADMIN_PREFIX', 'admin') . '::',
'middleware' => ['auth']
], function() {
/*
* ADMIN ROUTES
*/
});
So all the routes in this group will be something like:
www.mydomain.ext/admin/dashboard
www.mydomain.ext/admin/user/1
www.mydomain.ext/admin/page/2
and so on.
From what I've found here:
Laravel matches routes from the top down. So all you need to do is put 'campaigns/add' above the wildcard route.
And that's what I've done:
routes.php
Route::group([
'prefix' => Localization::setLocale(),
'middleware' => ['localeSessionRedirect', 'localizationRedirect']
// LaravelLocalization (https://github.com/mcamara/laravel-localization)
], function() {
Route::auth();
// admin routes
Route::group([
'prefix' => env('ADMIN_PREFIX', 'admin'),
'as' => env('ADMIN_PREFIX', 'admin') . '::',
'middleware' => ['auth']
], function() {
/*
* ADMIN ROUTES
*/
});
Route::get('/{page}', [
'as' => 'page',
'uses' => 'Website\WebsiteController#showPage'
])->where(['page' => '.+(\.html)']);
});
But when I try to call an admin route, Laravel throws this error:
Missing argument 1 for App\Http\Controllers\Website\Core\WebsiteCoreController::showPage()
So I suppose I'm doing something wrong... Any suggestions on how to fix my code?
Thanks everyone in advance

Pass parameter to controller from route

In my routes file, I have;
Route::get('/{token}/student', [
'uses' => 'SurveyController#resumeSurvey',
'as' => 'student',
]);
Route::get('/{token}/city', [
'uses' => 'SurveyController#resumeSurvey',
'as' => 'city',
]);
So the route is either "student" or "city". How do I determine which one in my controller method? Should I even be structuring my routes like this? Should I just point them to two different methods?
I can easily pass in {token} for example with just;
public function resumeSurvey($token)
{
Inside a controller you can get a current route name by getting a route object Illuminate\Routing\Route, at first place, then calling its method getName.
The next two ways are the same.
public function resumeSurvey($token)
{
$routeName = Route::getCurrentRoute()->getName();
$routeName = $this->getRouter()-> getCurrentRoute()->getName());
}
You should use different methods if you would like different things to do.
Example:
Route::get('/{token}/student', [
'uses' => 'SurveyController#resumeStudent',
'as' => 'student',
]);
Route::get('/{token}/city', [
'uses' => 'SurveyController#resumeCity',
'as' => 'city',
]);
And in your controller you should have two methods:
public function resumeStudent($token) {
}
public function resumeCity($token) {
}
Then your first route goes to resumeStudent and the ohter route to resumeCity.

Categories