mercredi 19 juillet 2023

Laravel specify channel in logger()->

I have this multiple channels in logging.php, want to specify the logger()->'s channel so that I can specify the log. I know Log::channel('channel name')-> but I want to use logger(). I'm using laravel 5.6



via Chebli Mohamed

lundi 17 juillet 2023

AWS SQS crashed in Laravel/Lumen

I have setup lumen microservice v9.0 and installed the following dependecies

"php": "^8.0",
"aws/aws-sdk-php": "^3.263",
"dusterio/laravel-aws-worker": "^0.1.36",
"illuminate/redis": "^9.52",
"laravel/lumen-framework": "^9.0",
"predis/predis": "^2.1",
"pusher/pusher-php-server": "^7.2"

When I dispatch job from one microservice to this microservice, SQS job received but when is job received its crash with following error

ERROR: Unresolvable dependency resolving [Parameter #3 [ callable $isDownForMaintenance ]] in class Illuminate\Queue\Worker {"exception":"[object] (Illuminate\Contracts\Container\BindingResolutionException(code: 0): Unresolvable dependency resolving [Parameter #3 [ callable $isDownForMaintenance ]] in class Illuminate\Queue\Worker at /var/app/current/vendor/illuminate/container/Container.php:1118)

Even two days before this code was working fine but today after deployment its working stop and getting crashed. Can anyone help me to resolve this issue I have spent a lot of time to fix this issue but failed

bootstrap/app.php

$app->register(Dusterio\AwsWorker\Integrations\LumenServiceProvider::class);


via Chebli Mohamed

samedi 15 juillet 2023

Validation - Laravel [closed]

{
    "status": true,
    "message": {
        "main_1": {
            "chalet_main_facility_sub_facilities": [
                "sub_1",
                "sub_2",
                "sub_3"
            ]
        },
        "main_2": {
            "chalet_main_facility_sub_facilities": [
                "sub_1",
                "sub_2",
                "sub_3"
            ]
        },
        "ma": {
            "chalet_main_facility_sub_facilities": [
                "sub_1"
            ]
        }
    }
}

the array key "main_1", "main_2" and "ma" must be required|string|max:45 ??

I have a database like this chalet_main_facilities contains a title this title is 45 max length and each chalet_main_facilities contains chalet_main_facility_sub_facilities contains a title this title is 75 max length

chalet_main_facilities Table

chalet_main_facility_sub_facilities Table

laravel validator ↴

$validator = Validator($request->all(), [
    'chalet_main_facilities' => 'required|array|min:1',
    'chalet_main_facilities.*' => 'required|array|distinct|size:1',
    'chalet_main_facilities.*.chalet_main_facility_sub_facilities' => 'required|array|min:1',
    'chalet_main_facilities.*.chalet_main_facility_sub_facilities.*' => 'required|string|min:3|max:255',

]);


via Chebli Mohamed

Error authenticating user and displaying validation messages in Laravel

I'm facing difficulties with user authentication and displaying validation messages in Laravel. When attempting to log in, the user is not authenticated correctly, and the validation error messages are not displayed.

I followed the official Laravel documentation for authentication and validation, but I'm not getting the expected result even with the help of artificial intelligence. Here are some of my code snippets:

public function store(Request $request)
{
    $request->validate([
        'email' => 'required|email',
        'password' => 'required',
    ]);

    $credentials = $request->only('email', 'password');

    if (auth()->attempt($credentials)) {
        $user = auth()->user();
        $request->session()->put('user', $user);
        return redirect()->route('home');
    }

    throw ValidationException::withMessages([
        'email' => 'Invalid email or password.',
    ]);
}

Here is the HTML code:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
    <h1>Login</h1>
    <form action="/login" method="post">
    @csrf
        <input type="email" name="email" placeholder="Email">
        <input type="password" name="password" placeholder="Password">
        <input type="submit" value="Login">
    </form>
</body>
</html>

And finally, the routes:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\LoginController;

Route::get('/users', [UserController::class, 'index'])->name('users.index');
Route::get('/users/create', [UserController::class, 'create']);
Route::post('/users', [UserController::class, 'store']);
Route::get('/users/{user}', [UserController::class, 'show']);
Route::get('/users/{user}/edit', [UserController::class, 'edit']);
Route::put('/users/{user}', [UserController::class, 'update']);
Route::delete('/users/{user}', [UserController::class, 'destroy'])->name('users.destroy');

Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('/login', [LoginController::class, 'index'])->name('login');
Route::post('/login', [LoginController::class, 'store']);
?>

I attempted to authenticate the user using the credentials provided in the login form. I expected Laravel to correctly authenticate the user if the credentials were correct. I also set validation rules in the store() method of the LoginController using $request->validate([...]). I expected that if the email and password fields were not filled correctly, Laravel would display the error messages on the login.blade.php view.

When I submit the login form with correct credentials, Laravel doesn't authenticate the user correctly and redirects back to the login page without displaying any error messages. When I submit the form with empty fields or invalid data, the corresponding error messages are also not displayed on the view. Instead, the form is redirected back to the login page without any feedback.

I believe something is missing or configured incorrectly because Laravel is not authenticating the user correctly and not displaying the validation messages.

I would appreciate any help you can provide to solve this issue.



via Chebli Mohamed

mardi 11 juillet 2023

ErrorException: htmlspecialchars() expects parameter 1 to be string laravel . Not sending any data to blade

I am getting this exception even though I am not sending any data. Sometime it works when I logout and login again after getting error. Any clue what might be wrong?

Only things I am accessing in the blade are-



I tried commenting these, Then also I am getting the same error.

Menu item

<li class="menuItems" id="support"><a href="">Admin Support</a></li>

web.php

Route::get('/support','AdminSupport@support')->name('support');

Controller

public function support()
  {
    return view('Admin.support');
  }


via Chebli Mohamed

lundi 10 juillet 2023

laravel gates permission foreach

excuse me, i'm a junior developer and im still learn about laravel gates concept. i have a tables structure like this.

  1. Users = id, email, password, role_id.

  2. role = id, name, description.

  3. permission = id, name, slug.

  4. role_permission = permission_id, role_id. and this is my code

    $permission = Permission::all();

     foreach($permission as $p){
         Gate::define($p->slug, function(User $user) use ($p) {
             return $user->role_id === ???;
         });
     }
    

My Code

what should i return to protect gates, thanks.

im still learning about laravel gates concept, so i hope the other senior developers help me to explain about gates



via Chebli Mohamed

dimanche 2 juillet 2023

undefined variable client_details in laravel

I am new in laravel, I need assistance in displaying data from database on laravel blade view but I am getting the error, "undefined variable client_details" even though it is defined in my controller.

below is what I did in my controller:

namespace paylesotho\Http\Controllers;

use Illuminate\Http\Request;
use paylesotho\Utility_data;
use DB;


class UtilityManagementController extends Controller
{
    //display clients data on a table

    public function index(){
        $clients = Utility_data::latest()->paginate(6);
        return view('manage_utility', ['client_details' => $clients]);
    }
}

below is what I did in my routes

Route::get('/manage_utility', function () {
    return view('manage_utility');
});

Route::get('statistics/', 'UtilityManagementController@index')->name('index');

below is my blade file

div class="container" style="margin-top: 7%;"> 
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>ID Number</th>
                    <th>Client Name</th>
                    <th>Phone Number</th>
                    <th>Address </th>
                    <th>Float Amount</th>
                    <th>Actions</th>
                </tr>
                </thead>
                <tbody>
                @foreach ($client_details as $item)
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>
                            <a href="" class="btn btn-primary" data-toggle="modal" data-target="">Edit</a>
                            <a href="" class="btn btn-danger" data-action="" data-toggle="modal" data-target="">Delete</a>    
                        </td>
                    </tr> 
                @endforeach 
                </tbody> 
            </table>
            <div class="d-flex">
            {!! $client_details->links('pagination::bootstrap-4') !!}
            </div> 


via Chebli Mohamed