mardi 29 août 2023

How to fetch data from database using multiple dropdown in form laravel 5.4?

I have blade view which is basically a form consists of 3 dropdown value such as Division,District and Upazila. Blade View

my Index blade looks like below image. Index Blade

after the submit button clicked,it shows me page not found.i don't have any idea why it didn't go to controller method. enter image description here

Route:

Route::get('booksnew/allinstbyupazila', [
                'as' => 'admin.booksnew.allinstbyupazila',
                'uses' => 'BooksNewController@allinstbyupazila']);

Controller:

public function allinstbyupazila(BooksRequest $request) {

        $module_name = $this->module_name;
        $module_icon = $this->module_icon;
        $module_model = $this->module_model;

        $module_action = "List";

        $title = "Upazila wise Institute List";
        $page_heading = $title;

        $division = $request->division_id;
        $district = $request->district_id;
        $upazila = $request->upazila_id;
        dd($request->all());

        if ($division == '' || $district == '' || $upazila == ''){
            Log::info("'$title' viewed by User:" . Auth::user()->name . '(ID:' . Auth::user()->id . ')');

            return view("backend.$module_name.upazila_input", compact('page_heading', 'title', 'module_name', 'module_icon', 'page_heading', 'module_action'));

        } else {
            if (Auth::user()->hasRole('Administrator')) {
            
                $$module_name = $module_model::where('division_id', $division)
                    ->where('district_id', $district)
                    ->where('upazila_id', $upazila)
                    ->get();
                
            }else

                return redirect()->back()->with('flash_warning', '<i class="fa fa-exclamation-triangle"></i> Do not have the permission');

        }

        Log::info("'$title' viewed by User:" . Auth::user()->name . '(ID:' . Auth::user()->id . ')');
        
        return view("backend.$module_name.index", compact('division', 'district','upazila', 'title', 'module_name', "$module_name", 'module_icon', 'page_heading', 'module_action'));

I try everything i could but didn't find any solution yet.i just need to fetch data from the dropdown value as parameter and show the fetched data in another blade view.help me asap.



via Chebli Mohamed

mercredi 23 août 2023

laravel sanctum api working fine with postman but not working with flutter

I'm New in Laravel but I know flutter here what I am trying to achieve I have created a Laravel Api where token is generated by sanctum and I getting the token properly when I test on postman but when I try this from flutter project I get the token but i get this error

{ "message": "Unauthenticated." }

here is my laravel function in controller

`\public function store(Request $request) { $validator = Validator::make($request->all(), [ 'phone_no' => 'required|digits:10', 'name' => 'sometimes|string|nullable', // Allow empty name 'address' => 'sometimes|string|nullable', // Allow empty address 'longitude' => 'sometimes|string|nullable', // Allow empty longitude and latitude 'latitude' => 'sometimes|string|nullable', // Allow empty longitude and latitude 'accuracy' => 'sometimes|string|nullable', // Allow empty longitude and latitude 'referby_coupon' => 'sometimes|string|nullable', // Allow empty referby_coupon ]); // Check for existing user with the same phone number $user = UserModel::where('phone_no', $request->phone_no)->first();

if ($validator->fails()) {
    return response()->json([
        'status' => 422,
        'errors' => $validator->messages()
    ], 422);
} else {
    if ($user) {
          $user->name = $user->name === 'new' ? '' : $user->name;
            $user->address = $user->address === 'new' ? '' : $user->address;
            $user->longitude = $user->longitude === 'new' ? '' : $user->longitude;
             $user->latitude = $user->latitude === 'new' ? '' : $user->latitude;
              $user->accuracy = $user->accuracy === 'new' ? '' : $user->accuracy;
             $user->referby_coupon = $user->referby_coupon === 'new' ? '' : $user->referby_coupon;
              $token = $user->createToken($request->id)->plainTextToken;

        return response()->json([
            'status' => 422,
            'message' => 'User already exists',
            'token' => $token,
            'data' => $user
        ], 422);
    } else {
        // Generate random coupon
        $coupon = $this->generateRandomCoupon();

    $user = UserModel::create([
            'id' => $request->id,
            'name' => $request->name,
            'phone_no' => $request->phone_no,
            'address' => $request->address,
            'longitude' => $request->longitude,
            'latitude' => $request->latitude,
            'accuracy' => $request->accuracy,
            'own_coupon' => $coupon, // Insert the generated coupon here
            'referby_coupon' => $request->referby_coupon,
        ]);

        if ($user) {
         $token = $user->createToken($request->id)->plainTextToken;

            return response()->json([
                'status' => 200,
                'token' => $token,
                'message' => 'User Created Successfully'
            ], 200);
        } else {
            return response()->json([
                'status' => 500,
                'message' => 'Something went wrong'
            ], 500);
        }
    }
}
}

private function generateRandomCoupon($length = 4) { $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $excludedWords = ['CAT', 'DOG', 'SEX','sex', 'FUCK','fuck']; // Add any words you want to exclude here $coupon = '';
do {
    $coupon = '';
    for ($i = 0; $i < $length; $i++) {
        $coupon .= $characters[rand(0, strlen($characters) - 1)];
    }
} while (UserModel::where('own_coupon', $coupon)->exists() || in_array($coupon, $excludedWords, true));

return $coupon;

}`

this is model class

`

protected $table = 'users';
<?php

namespace App\Models; use Laravel\Sanctum\HasApiTokens; use Illuminate\Notifications\Notifiable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Auth\User as Authenticatable; use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;

class UserModel extends Authenticatable { use HasApiTokens, HasFactory, Notifiable;

protected $fillable = [
    'id',
    'name',
    'phone_no',
    'address',
    'longitude',
    'latitude',
    'accuracy',
    'own_coupon',
    'referby_coupon',
    
    
];

protected $primaryKey = "id";

public $timestamps = false;

}`

this is my flutter code

Future<void> setData(String name, String address, String phoneNo,
      String longitudeLatitude, String referByCoupon) async {
    DateTime now = DateTime.now();
    String formattedDate = DateFormat('yyyymmddhhmmsskkmm').format(now);
    var formData = FormData.fromMap({
      'id': formattedDate,
      'name': 'new',
      'address': 'new',
      'phone_no': MyLogin.phoneNoTransfer,
      'longitude': 'new',
      'latitude': 'new',
      'accuracy': 'new',
      'own_coupon': "new",
      'referby_coupon': referByCoupon.isNotEmpty ? referByCoupon : 'new'
    });
    try {
      Dio dio = Dio(BaseOptions(validateStatus: (statusCode) {
        if (statusCode == 422) {
          return true;
        }
        if (statusCode == 200) {
          return true;
        }
        return false;
      }));
      final response = await dio.post(
        options: Options(headers: {
          HttpHeaders.contentTypeHeader: "application/json",
        }),
        '$startUrl/public/api/user',
        data: formData,
      );
      print(response);
      print(response.statusCode);

      String token = response.data['token'];

      if (response.statusCode == 200) {
        SharedPreferencesPersonalData().setToken(token);
        SharedPreferencesPersonalData().setID(int.parse(formattedDate));
        SharedPreferencesPersonalData().setPhoneNo(phoneNo);
      } else if (response.statusCode == 422) {
        final result = response.data['data'];

        print(result);

        int apiId = int.parse(result['id'].toString());
        String apiPhoneNo = result['phone_no'].toString();
        String apiName = result['name'].toString();
        String apiAddress = result['address'].toString();
        String ownCoupon = result['own_coupon'];
        String referByCoupon = result['referby_coupon'];

        SharedPreferencesPersonalData().setID(apiId);
        SharedPreferencesPersonalData().setPhoneNo(apiPhoneNo);
        SharedPreferencesPersonalData().setName(apiName);
        SharedPreferencesPersonalData().setAddress(apiAddress);
        SharedPreferencesPersonalData().setOwnCoupon(ownCoupon);
        SharedPreferencesPersonalData().setReferByCoupon(referByCoupon);
        SharedPreferencesPersonalData().setToken(token);
      } else {
        print(response.statusCode);
      }
    } catch (e) {
      print(e);
    }
  }

I am excepting the token work properly but its not working and i notices that when I use postman in access token id all column fill but when I use login in flutter tokenable id is 0 and i hosted in hostinger

enter image description here

futter where i am getting error enter image description here



via Chebli Mohamed

dimanche 20 août 2023

Undefined variable $artikels in foreach laravel

I have a problem with my laravel project where i can't show my data in the table. It keeps giving me undefined veriable $artikels in my writer.blade.php

So this is my controller

namespace App\Http\Controllers;

use App\Models\Artikel;
use Illuminate\Http\Request;

class ArtikelController extends Controller
{
    public function index()
    {
        $artikels = Artikel::all(); // Replace 'Artikel' with your actual model name

        return view('writer', compact('artikels'));
    }
}

this is my view in writer.blade.php

<table class="table table-bordered">
        <tr>
            <th>Image</th>
            <th>Judul</th>
            <th>Kategori</th>
            <th>Tag</th>
        </tr>
        @foreach ($artikels as $item)
        <tr>
            <td><img src="/gambar_artikel/" width="100px"></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        @endforeach
    </table>

and this is my route

Route::view('/', 'dashboard')->name('dashboard');
Route::view('/AboutMe', 'aboutme')->name('AboutMe');
Route::view('/Writer', 'writer')->name('writer');


Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::resource('/artikel', \App\Http\Controllers\ArtikelController::class);

how do i fix this problem? Thank you :)

I tried finding a lot of tutorials but it didn't fix my problem



via Chebli Mohamed

samedi 19 août 2023

The GET method is not supported for route api/login. Supported methods: POST. Laravel + react deplo

I got this problem when I deployed a Laravel app:

The GET method is not supported for route api/login. Supported methods: POST.

But on the route I use the method POST

Route::post('/login',[AuthController::class,'login']);


via Chebli Mohamed

vendredi 18 août 2023

How to convert a mailable object to plain text version?

I have an old Laravel 5.7 application.

I have a mailable Illuminate\Mail\Mailable object.

I want to use a 3rd party to send my emails which is not within Laravel's default driver. For the API, I need to send the plain-text and the html-text of the mail.

I can retrieve html text like this:

$htmlText = $mailable->render();

But how to I retrieve the plain text?

This is my build of the mailable:

public function build()
{
    return $this->view('emails.newsletter.master')
                    ->text('emails.newsletter.master_text')
                    ->subject('Hallo');
} 

I could not find how to retrieve plain text from mailable from Laravel docs. I also did not find a method inside the Mailable class that would generate the text. I only see that the plain text blade path gets assigned to textView variable.



via Chebli Mohamed

jeudi 17 août 2023

after changing theme for one Mailable file in laravel it messup other email which dont have $theme variable set

below is my code to change the theme for particular emails where customize mailable is extended.

class TablerMailable extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Instruct to use tabler theme.
     *
     * @var string
     */
    public $theme = 'tabler';

    /**
     * Build the Markdown view for the message.
     *
     * @return array
     */
    protected function buildMarkdownView()
    {
        $markdown = Container::getInstance()->make(Markdown::class);
        $markdown->loadComponentsFrom([resource_path('views/vendor/tabler-mail')]);

        if (isset($this->theme)) {
            $markdown->theme($this->theme);
        }
        
        $data = $this->buildViewData();

        $views= [
            'html' => $markdown->render($this->markdown, $data),
            'text' => $this->buildMarkdownText($markdown, $data),
        ];
        
        return $views;
    }
}

my problem is other emails that don't extend this class also get affected by this change. I don't know how to unset this theme for those emails where the $theme variable is already not set.



via Chebli Mohamed

vendredi 11 août 2023

Unable to communicate with Sanctum - I am getting a 503 error

I have successfully set up Sanctum on Laravel 8. However, when I try to use Postman with the bearer token obtained from the personal_access_tokens table, I encounter the following error message. Could it be the stateful setting? As I am not communicating to the API form the same domain but from Postman and then my real project will be sending data from another window computer to my website. I have been for 4 days on this, I cannot find an answer to the issue.

"I would like to reassure you that the page is not undergoing maintenance(php artisan up)."

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>

<head>
    <title>503 Service Unavailable</title>
</head>

<body>
    <h1>Service Unavailable</h1>
    <p>The server is temporarily unable to service your
        request due to maintenance downtime or capacity
        problems. Please try again later.</p>
</body>

</html>

api.php:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\V2\Api\KeywordsController;


Route::middleware(['auth:sanctum', 'abilities:db:view,db:update,db:store'])->group(function () {
        Route::get('fetch-keyword', [KeywordsController::class, 'fetchKeyword']);
        Route::put('update-keyword/{keywordName}', [KeywordsController::class, 'updateKeyword']);
});

auth.php:

<?php

return [

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'sanctum',
            'provider' => 'users',
            'hash' => false,
        ],
    ],


    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

    ],

    'verification' => [
        'expire' => 1440,
    ],


    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 180,
            'throttle' => 60,
        ],
    ],

    'password_timeout' => 10800,

];

Sanctum.php:

<?php

use Laravel\Sanctum\Sanctum;

return [

    'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
        '%s%s',
        'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
        Sanctum::currentApplicationUrlWithPort()
    ))),


 'guard' => ['api'],

    'expiration' => null,

    'middleware' => [
        'verify_csrf_token' => App\Http\Middleware\VerifyCsrfToken::class,
        'encrypt_cookies' => App\Http\Middleware\EncryptCookies::class,
    ],

];

Traits in the User Model:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Paddle\Billable;
use Spatie\Permission\Traits\HasRoles;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasApiTokens, HasFactory, Notifiable, Billable, HasRoles;

.....more code not attached

Kernal.php:

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Illuminate\Http\Middleware\HandleCors::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];


    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            \Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
        'abilities' => \Laravel\Sanctum\Http\Middleware\CheckAbilities::class,
        'ability' => \Laravel\Sanctum\Http\Middleware\CheckForAnyAbility::class,
    ];

}

This is how my token is created:

 public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'email' => ['required', Rule::unique('users')->ignore($id)],
            'has_token' => 'nullable|boolean',
            'password' => $request->filled('password') ? 'required|min:8' : '',
            'token_name' => [
                'nullable',
                'string',
                Rule::requiredIf(function () use ($request) {
                    return $request->has_token === 1;
                }),
            ],
        ]);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }

        try {
            $user = User::find($id);

            if ($request->email !== $user->email) {
                return $this->changeEmail($request->email, $request->password, $id);
            }

            // Update fields
            $user->name = $request->name;
            $user->current_credits = $request->current_credits;

            if ($request->filled('password')) {
                $user->password = Hash::make($request->password);
            }

            $user->save();

            $user = User::where('email', $request->email)->first();
            if ($request->has_token === 1 && $request->filled('token_name')) {
                $user->createToken($request->token_name, ['db:view', 'db:update', 'db:store']);
            } elseif ($request->has_token === 0) {
                $user->tokens()->delete(); 
            }

        } catch (Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }

        return response()->json(['success' => 'success'], 200);
    }

I can see the token in the database, image attached: https://i.stack.imgur.com/az8vS.png

Thanks in advance!



via Chebli Mohamed

mercredi 9 août 2023

laravel 5 error: SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT trans2 does not exist

I have a question about Laravel 5.7. I have a multi-tenant project, and the issue is that when generating migrations, the admin is created without any problems. However, when I try to create a client, the client's database is not created, and the following error appears:

SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT trans2 does not exist

The problem seems to be related to the database, in my case, mysql Ver 14.14 Distrib 5.7.43, Laravel 5.7 and PHP 7.4.33. I hope someone has found a solution. Thanks in advance.

Add my MySQL user in the .env file, but it's not creating the client's database.



via Chebli Mohamed

Error "Whoops, looks like something went wrong." in Laravel page

First of all thank you very much for reading me and helping me in this as I am a newbie in laravel.

I have done a deployment of a website given to me by a company and everything works except the search in the post_categories table in the database.

When I enter a link that performs a query to the database to get all the posts of that category I get the following error (In this case for category 13):

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '>'$. "en"' asc' at line 1 (SQL: select * from `post_categories` where `post_categories`.`parent_id` = 13 and `post_categories`.`parent_id` is not null order by `title`->'$. "en"' asc)

However, if I go directly to any post in that category it is perfectly visible, so it is affecting only the search engine.

The error is caused because the title->'$. "es"' part doesn't seem to work, but I don't know why, since the query is generated directly from here as far as I can tell from reading the code.

public function select($query, $bindings = [], $useReadPdo = true)
    {
        return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
            if ($this->pretending()) {
                return [];
            }

            // For select statements, we'll simply execute the query and return an array
            // of the database result set. Each element in the array will be a single
            // row from the database table, and will either be an array or objects.
            $statement = $this->prepared($this->getPdoForSelect($useReadPdo)
                              ->prepare($query));

            $this->bindValues($statement, $this->prepareBindings($bindings));

            $statement->execute();

            return $statement->fetchAll();
        });
    }

I thought that maybe it is failing some php variable that is not being defined well, but I have not touched any of the code since I have only deployed and the web, on the company's server, makes the search correctly.

Would anyone know how to guide me to solve this error?

Thank you all!

all the data and evidence have been provided above



via Chebli Mohamed

vendredi 4 août 2023

Invalid XSL format (or) file name when running php artisan commands on laravel 5.8

I'm getting the "Invalid XSL format (or) file name" error message when running php artisan commands, its only on my laravel 5.8 project, in others it runs perfectly

The command works but take way too long, occurs only in this project, anyone knows why is that?

I tried a feel solutions directly in the windows, but the commands works in others projects, so the problemn is in the project



via Chebli Mohamed

mercredi 2 août 2023

How to implement aws cloudfront in my laravel application?

Here is the code already used in my laravel application. This code directly get image url from my s3 storage. But I need to implement cloudfront and generate signed url for to get and download images.

foreach($fileInfos['files'] as $file) {
       $dlFileName = $file['name'];
       $fileDLUrls[] = FileIO::getUrl(FILE_ORIGINAL_DIRECTORY . '/'.$file['path'], $effectiveTime, ['ResponseContentType' => 
'application/octet-stream','ResponseContentDisposition' => 
'attachment; filename*=UTF-8\'\''. rawurlencode($dlFileName)]);
}
    public static function getUrl($fullPathName, $expires = '+1440 minutes', $headers = [])
    {
        if ($fullPathName == '') {
            return null;
        }

        switch (Storage::getDefaultDriver()) {
            case 's3':
                $disk = Storage::disk('s3');
                $headers = array_merge([
                    'Bucket' => env('AWS_S3_BUCKET'),
                    'Key' => $fullPathName
                ], $headers);
                $command = $disk->getDriver()
                    ->getAdapter()
                    ->getClient()
                    ->getCommand('getObject', $headers);
                $request = $disk->getDriver()
                    ->getAdapter()
                    ->getClient()
                    ->createPresignedRequest($command, $expires);
                return (string) $request->getUri();
            case 'local':
                return URL::to('/' . env('AWS_LOCAL_UPLOADDIR')) .
                         '/' . $fullPathName;
            default:
                return null;
        }
    }

I have changed above code as below,

public static function getUrl($fullPathName, $expires = '+1440 minutes', $headers = [])
    {
        if ($fullPathName == '') {
            return null;
        }


        switch (Storage::getDefaultDriver()) {
            case 's3':
                $resourceKey = env('CLOUDFRONT_URL').$fullPathName;
                $expired = strtotime($expires);
                $privateKey = env('AWS_PRIVATE_KEY');
                $keyPairId = env('AWS_KEY_PAIR_ID');
                
                $cloudFrontClient = new CloudFrontClient([
                    'profile' => 'default',
                    'version' => '2014-11-06',
                    'region' => env('AWS_REGION')
                ]);
                    $result = $cloudFrontClient->getSignedUrl([
                        'url' => $resourceKey,
                        'expires' => $expired,
                        'private_key' => $privateKey,
                        'key_pair_id' => $keyPairId,
                        
                    ]);
                    
                    return $result;
            case 'local':
                return URL::to('/' . env('AWS_LOCAL_UPLOADDIR')) .
                    '/' . $fullPathName;
            default:
                return null;
        }
    }

But I didn't use the headers in the first code. So that am not able to download the images. Can anyone help me to know about the reason or any solutions?



via Chebli Mohamed