mercredi 18 août 2021

Laravel 5.7: Is it possible to add direct a value in the table?

I was wondering, if it possible to add directly a value in the table. Because I would like that all with the value "1" are registered

Schema::table('games', function (Blueprint $table) {
        $table->integer('rankPosition', '1');
});

Update solution:

Schema::table('games', function (Blueprint $table) {
        table->integer('rankPosition')->default(1);
});


via Chebli Mohamed

How To Hide ID from url ?? Laravel

I want to hide id from URL

http://example.com/photo/1234/image-name

I want it to change like this

http://example.com/photo/image-name

Is that there any way to remove or replace id from url.

CONTROLLER:

public function show($id, $slug = null)
{
    $response = Images::findOrFail($id);

if (Auth::check() && $response->user_id != Auth::user()->id && $response->status == 'pending' && Auth::user()->role != 'admin') {
    abort(404);
} else if (Auth::guest() && $response->status == 'pending') {
    abort(404);
}

$uri = $this->request->path();

if (str_slug( $response->title) == '') {

        $slugUrl  = '';
    } else {
        $slugUrl  = '/'.str_slug( $response->title );
    }

    $url_image = 'photo/'.$response->id.$slugUrl;

Href

<a href=""/>

Route

Route::get('photo/{id}/{slug?}','ImagesController@show');


via Chebli Mohamed

Laravel Cache - Attempt to read from a closed MaxMind DB

I'm using Laravel 5.8 and MaxMind GeoLite2-City.mmdb together with Reader (GeoIp2\Database\Reader).

This is how it look like:

public function checkWithoutCache(Request $request) {
    
    $reader = new Reader(storage_path().'/app/ipToCountry/GeoLite2-City-v2.mmdb');      
    $r      = $reader->city($request->ip());
    $reader->close();
    return $r;
}

Everything working ok. Now because our server deals with tons of requests that for each request we are loading the Reader with the GeoLite2-City-v2.mmdb file (70MB), we wonder if it's smart to leave it like that or adding the Reader object to laravel Cache in order to load it once and then use the cache so we will improve performance.

So we were trying to use Laravel Cache like the next example:

public function checkWithCache(Request $request) {
    
    $reader = \Cache::remember('geo_city_instance', now()->addDays(30), function(){
        return new Reader(storage_path().'/app/ipToCountry/GeoLite2-City-v2.mmdb'); 
    });
    $r      = $reader->city($request->ip());
    $reader->close();
    return $r;
}

But we got the next Exception:

BadMethodCallException Attempt to read from a closed MaxMind DB ( …/vendor/maxmind-db/reader/src/MaxMind/Db/Reader.php158)

Does anyone have a solution on how to add it to cache without exception?

Or we don't need actually need to add it to cache?

Please help, thanks!



via Chebli Mohamed

middleware not redirect to view

i have a problem with middleware

my middleware should redirect to a view if the logged in user has the column 'edad' = Null

User loggin DB: https://i.imgur.com/9UubPls.png

    <?php

namespace App\Http\Middleware;

use Auth;
use Closure;
use Illuminate\Http\Request;

class Personales
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::guest()) {
            return $next($request);
        }
        
        if ($request->route()->getName() == "completar" || $request->route()->getName() == "ucompletar") {
            return $next($request);
        }

        if (Auth::user() && Auth::user()->edad != null && Auth::user()->edad != "") {
            return $next($request);
        } else {
            return redirect('/completar');
        }
    }
}

i can't find the error because it doesn't work middleware

maybe i'm missing something, just create middleware and view



via Chebli Mohamed

mardi 17 août 2021

Unit testing individual pieces of Laravel artisan commands without running the entire command

I have some pretty complicated commands with a lot of moving parts. I want to write tests for individual parts of those commands. This is proving to be a real pain the way I am currently doing it because the input and output interfaces are not instantiated until the run() method is hit during a typical programmatic call.

e.g.

Artisan::call('do:something', ['foo' => 'whatever', '--bar' => true]);

As such, I've written most of my tests like so:

protected function setUp()
{
    parent::setUp();

    $this->command = new DoSomething;
    $this->thing = Something::first();
}

/** @test */
public function it_succeeds_and_fails_at_doing_something()
{
    // Test happy path.
    $success = $this->command->verySuccess($this->thing->id);

    $this->assertInstanceOf(SomethingSuccessful::class, $success);

    // Test unhappy path.
    $failed = $this->command->verySuccess('nope');

    $this->assertNull($failed);
}

The problem with the above is that within the command's code is scattered calls such as if ($this->option('bar')) { ... } which always fails with Call to a member function getOption() on null

Aside from the exception, it also makes it problematic to test these specific methods with the right options and arguments set.

How do I get around this input issue? Feels like I am going about testing this the wrong way.



via Chebli Mohamed

how to use fullcalendar in laravel for mark a attendance

i have no idea how to use Fullcalendar in LARAVEL 5.4 for mark a attendance

   <div id='calendar' ></div>

not need to show events but marks today attendance



via Chebli Mohamed

PDF not generated in laravel

I'm generating pdf using following code in Laravel but the issue is sometimes data is saved in the database and pdf is not generated when i check the pdf folder there is no pdf files with that name, this does not happens every time i generate the pdf but there are some entries in my database for which the pdf is not generated, i want some way so that i can check whether the pdf has been generated and mark status true in the database if pdf is generated later on recreate pdf and send it to user through cron jobs or is there any other way also, Please suggest some solution Thanks

   use PDF;
    ..
    ..
    $DataStore->user_id = $user_id;
    $DataStore->description = $description;
    $DataStore->save();
    if($DataStore) {

    $pdf = PDF::loadView('/admin/user_submission_pdf', ['data' => $DataStore]);
    $content = $pdf->download()->getOriginalContent();
    Storage::put('public/pdfFiles/' . $DataStore->Id . '.pdf', $content);
   


via Chebli Mohamed