laravel log debug is not working.I checked A response is returned from the server side - laravel-5

laravel log debug is not working.
I create app with laravel5 and vue.js.
I send a request to the server side with vue and catch this with laravel Controller.php.
I code Log::debug to PostsController.php, but the log is not written in laravel.log.
I try use Illuminate\Support\Facades\Log; and use Log; , but both are not working.
Do you find anything wrong with my code?
Thank you for your help.
// web.php
Route::get('/posts/items', 'PostsController#items');
PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Model\Post;
use App\Model\Item;
// use Log;
use Illuminate\Support\Facades\Log;
use App\Http\Resources\Post as PostResource;
use App\Http\Resources\Item as ItemResource;
class PostsController extends Controller
{
public function items(Request $request)
{
Log::info('kita');
Log::debug('kurukao');
logger()->debug('debug');
$post_id = $request->input('post_id');
return $post_id;
}
//vue
methods: {
mouseenter(id) {
this.itemId = id;
},
mouseleave(id) {
this.itemId = null;
},
getItems() {
axios.get("/posts/items", {
post_id: this.$route.params.id
})
.then((res) => {
// console.log(res.data)
console.log('ok')
this.postDataList = res.data.data;
// console.log(this.postDataList)
if(this.postDataList != null){
this.itemDataList = this.postDataList[0]['items'];
}
});
},

Related

Unable to redirect after a user registers in Laravel Jetstream

I am successfully able to redirect whenever a user logs in. I am using the Login Response method. But when I am trying to do the same thing for the user when a user registers, it shows the URL in the browser, but I need to refresh to view the page to make it load. Something weird thing is happening here.
Following the below approach:
https://talltips.novate.co.uk/laravel/laravel-8-conditional-login-redirects
To understand the problem, please check the gif below. Login Response is working the way I expected, but Register Response is not working; it is behaving weirdly.
Login Response
Register Response
LoginResponse.php
namespace App\Http\Responses;
use Illuminate\Support\Facades\Auth;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
class LoginResponse implements LoginResponseContract
{
public function toResponse($request)
{
if (Auth::user()->hasAnyRoles(['Administrator', 'Employee'])) {
return redirect()->route('backend.dashboard');
}
return redirect()->route('frontend.dashboard');
}
}
RegisterResponse.php
<?php
namespace App\Http\Responses;
use Illuminate\Support\Facades\Auth;
use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract;
class RegisterResponse implements RegisterResponseContract
{
public function toResponse($request)
{
if(Auth::user()->hasAnyRoles(['Administrator', 'Employee'])) {
return redirect()->route('backend.dashboard');
}
return redirect()->route('frontend.dashboard');
}
}
JetstreamServiceProvider.php
public function boot() {
$this - > configurePermissions();
Jetstream::deleteUsersUsing(DeleteUser::class);
// Register New LoginResponse
$this - > app - > singleton(
\Laravel\ Fortify\ Contracts\ LoginResponse::class,
\App\ Http\ Responses\ LoginResponse::class);
// Register New RegisterResponse
$this - > app - > singleton(
\Laravel\ Fortify\ Contracts\ RegisterResponse::class,
\App\ Http\ Responses\ RegisterResponse::class);
}
AuthServiceProvider.php
public function boot() {
$this - > registerPolicies();
Gate::define('access-backend', function($user) {
return $user - > hasAnyRoles(['Administrator', 'Employee']);
});
Gate::define('access-frontend', function($user) {
return $user - > hasRole('Client');
});
}
Web.php
Route::middleware(['auth:sanctum', 'verified'])->group(function () {
\
Route::prefix('backend')->name('backend.')->middleware(['can:access-backend'])->group(function () {
Route::get('/dashboard', \App\Http\Livewire\Backend\Dashboard::class)->name('dashboard');
});
Route::prefix('frontend')->name('frontend.')->middleware(['can:access-frontend'])->group(function () {
Route::get('/dashboard', \App\Http\Livewire\Frontend\Dashboard::class)->name('dashboard');
});
});
Create your own RegisterResponse.php
use Illuminate\Http\JsonResponse;
use Laravel\Fortify\Contracts\RegisterResponse as Response;
class RegisterResponse implements Response
{
public function toResponse($request)
{
return $request->wantsJson()
? new JsonResponse('', 201)
: redirect(config('fortify.user'));
}
and register it in the JetstreamServiceProvider.php file, inside boot method
$this->app->singleton(
\Laravel\Fortify\Contracts\RegisterResponse::class,
\App\Http\Responses\RegisterResponse::class
);

V8Js::compileString():14026: ReferenceError: window is not defined

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
class AppController extends Controller
{
private function render() {
$renderer_source = File::get(base_path('node_modules/vue-server-renderer/basic.js'));
$app_source = File::get(public_path('js/entry-server.js'));
$v8 = new \V8Js();
ob_start();
$v8->executeString('var process = { env: { VUE_ENV: "server", NODE_ENV: "production" }}; this.global = { process: process };');
$v8->executeString($renderer_source);
$v8->executeString($app_source);
return ob_get_clean();
}
public function get() {
$ssr = $this->render();
return view('app', ['ssr' => $ssr]);
}
}
I followed https://dzone.com/articles/server-side-rendering-with-laravel-amp-vuejs-25 this page and
installed V8Js.
But $v8->executeString($app_source); make an error that
V8Js::compileString():14026: ReferenceError: window is not defined.
I have no idea how can I handle this error..
the problem was bootstrap.js using window object. THX!

Why broadcasting channel public not working? Laravel

I use laravel 5.3
I make routes/channels.php like this :
<?php
Broadcast::channel('messages', function() {
return true;
});
If I input the data cart and click submit, it will run this :
this.$http.post(window.BaseUrl + '/guest/add-notification', {cart_data: JSON.stringify(data)});
It will call function on the controller
The function like this :
public function addNotification(Request $request){
$input = $request->only('cart_data');
$data = json_decode($input['cart_data'], true);
event(new CartNotificationEvent($data));
}
Then it will call event
The event like this :
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class CartNotificationEvent
{
use InteractsWithSockets, SerializesModels;
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function broadcastWith()
{
return [
'message' => $this->data,
];
}
public function broadcastAs()
{
return 'newMessage';
}
public function broadcastOn()
{
return new Channel('messages');
}
}
On the client, I do like this :
Echo.channel('messages')
.listen('.newMessage', (message) => {
console.log('test')
console.log(message);
});
When all the code is executed, I check on the console, the console.log not display
Why is it not working?
If I see the whole code that I make, it seems the process is correct
class CartNotificationEvent implements ShouldBroadcast is missing.

Laravel 5.3 How to use Braintree\WebhookNotification

I am using Laravel 5.3 and trying to add new webhook event handler in WebhookController
Here is my controller
namespace App\Http\Controllers;
use Braintree\WebhookNotification;
use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
use Log;
use App\Models\BraintreeMerchant;
class WebhookController extends CashierController
{
public function handleSubMerchantAccountApproved(WebhookNotification $notification)
{
if( isset($_POST["bt_signature"]) && isset($_POST["bt_payload"]))
{
$notification = Braintree_WebhookNotification::parse($_POST["bt_signature"], $_POST["bt_payload"]);
$notification->kind == Braintree_WebhookNotification::SUB_MERCHANT_ACCOUNT_APPROVED;
// true
$notification->merchantAccount->status;
// "active"
$notification->merchantAccount->id;
// "blue_ladders_store"
$notification->merchantAccount->masterMerchantAccount->id;
// "14ladders_marketplace"
$notification->merchantAccount->masterMerchantAccount->status;
}
}
}
but getting the following error message:
BindingResolutionException in Container.php line 763:
Target [Braintree\WebhookNotification] is not instantiable.
I've found the answer. this is how to implement Braintree webhook controller.
<?php
namespace App\Http\Controllers;
use Braintree\WebhookNotification;
use Laravel\Cashier\Http\Controllers\WebhookController as CashierController;
use Illuminate\Http\Request;
class WebhookController extends CashierController
{
public function handleSubMerchantAccountApproved(Request $request)
{
$notification = WebhookNotification::parse($request->bt_signature, $request->bt_payload);
$merchantId = $notification->merchantAccount->id;
$result_merchant_status = $notification->merchantAccount->status;
}
}

Serious issues understanding namespacing in Laravel 5

I am having problems understanding namespacing. It just feels so stupid that for every Facade or helper I want to include, I need to included it in the controller file.
So, say I have this exerpt of my controller:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Auction;
use App\Bid;
use App\BidBuy;
use App\Http;
use Redirect;
use Session;
use Illuminate\Http\Request;
use Omnipay\Omnipay;
class PayPalController extends Controller {
private $data;
public function getPayment()
{
redirectIfNotLoggedIn();
}
The line with redirectIfNotLoggedIn(); is a function I want to call that is stored in App/Http/helpers.php
This is the file: helpers.php
<?php namespace App\Http;
class HelperFunctions {
// Filter for guest only pages
public function redirectIfLoggedIn() {
if (Session::has('steamid')) {
return Redirect::to('/');
} else {
return false;
}
}
// Filter for logged users only pages
public function redirectIfNotLoggedIn() {
if (Session::has('steamid')) {
return true;
} else {
return Redirect::to('/');
}
}
}
Back in the controller I have included: use App\Http;
When I go to call that function I get this:
FatalErrorException in PayPalController.php line 20:
Call to undefined function App\Http\Controllers\redirectIfNotLoggedIn()
EDIT:
Now I have: HelperFunctions.php located at App/Helpers/HelperFunctions.php:
<?php namespace App\Helpers;
use Session;
use Redirect;
// Filter for guest only pages
function redirectIfLoggedIn() {
if (Session::has('steamid')) {
return Redirect::to('/');
} else {
return false;
}
}
// Filter for logged users only pages
function redirectIfNotLoggedIn() {
if (Session::has('steamid')) {
return true;
} else {
return Redirect::to('/');
}
}
Excerpt of composer.json:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"psr-0": {
"Ignited\\LaravelOmnipay": "src/"
},
"files": [
"app/Helpers/HelperFunctions.php"
]
},
And this is my controller:
<?php namespace App\Http\Controllers;
//use App\Helpers\HelperFunctions;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Auction;
use App\Bid;
use App\BidBuy;
use Redirect;
use Session;
use Illuminate\Http\Request;
use Omnipay\Omnipay;
class PayPalController extends Controller {
private $data;
public function getPayment(\App\Helpers\HelperFunctions $helper)
{
$helper->redirectIfNotLoggedIn();
}
.. rest of the controller code here ..
}
Do not use helpers for this. You are violating pretty much everything related to OOP in doing so.
Instead, use Middleware.
In your controller, simply require the auth middleware:
public function __construct()
{
$this->middleware("auth");
}
You are trying to solve a solved problem. If you need to do something before executing a route/controller method, middleware (the Laravel 5 replacement for route filters) should be used.
You will likely have to customise your own middleware, however, then add it to the middleware array in app/Http/Kernel.php. Just use php artisan make:middleware CustomAuthMiddleware.
You could use your HelperFunctions via method injection in your controller. Like this:
public function getPayment(\App\Http\HelperFunctions $helper)
{
$helper->redirectIfNotLoggedIn();
}
It's not tested.

Resources