mercredi 8 septembre 2021

What is the proper syntax to exclude rows that have a specific ID, but lack a row in a related table with a value in a column on that row?

Currently working on getting my head around the ordering of things, not even with laravel at first, but sql. Though the end goal is to use eloquent.

There are 2 conditions for the rows that need to be excluded. If I'm fetching the rows from TableA..

1) TableA with the column status_id (or TableC.id) has to have lets say a value of 1.

2) TableB has to have an instance for that TableA.id where status_id (or TableC.id) is lets say 2.

Been looking at whereNotIn, whereExists and when for a while now, but can't wrap my head around what is correct for the situation and how it should look like.

Still learning about sql queries and I am very grateful for any tips on this dilemma.



via Chebli Mohamed

How to convert stream to download laravel

            $stream = fopen('data://text/plain;base64,' . base64_encode($response),'r');
            $stat = fstat($stream);
            $size = $stat['size'];

            $type = "video/mp4";
            $start = 0;
            $length = $size;
            $status = 200;

            $headers = ['Content-Type' => $type, 'Content-Length' => $size, 'Accept-Ranges' => 'bytes'];

            return response()->stream(
                function() use ($stream, $start, $length) {
                    fseek($stream, $start, SEEK_SET);
                    echo fread($stream, $length);
                    fclose($stream);
                }, $status, $headers
            );

the above is working to stream a video but I need to download the video directly instead... any suggestions, please



via Chebli Mohamed

assign and return specific data based on user role

I like to understand how I can assign each user who has the role we say that each agent with its own interface, indeed I am applying a solution which set up a system in which an administrator creates a new user with the agent role, an agent can create users with the user role, so we suppose that an admin creates 2 agents: Agent A and agent B each agent accesses the same interface but each one has its own users, I don't like to return all the users of the database in the interface of all the agents, so if agent A is authenticated the interface retrieves only the users who are added by agent A and the same thing for agent B is there a solution? im in laravel 5 here is mu User.php model

namespace App\Models;

use App\Models\Formatters\Formatter;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable 
{
    
   

   
    const ROLE_USER  = 'USER';
    const ROLE_ADMIN = 'ADMIN';

 

   
    /**
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'last_login_from', 'role', 'status', 'last_login_at', 'referrer_id'
    ];

    /**
     * 
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token', 'status', 'role', 'last_login_from', 'referrer_id', 'referrer', 'totp_secret'
    ];


   
  
    /**
     * User account
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function account()
    {
        return $this->hasOne(Account::class);
    }

    /**
   
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function referrer()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Users, referred by current user (referees)
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function referees()
    {
        return $this->hasMany(User::class, 'referrer_id');
    }

  

    /**
     * User games
     *
     * @return $this
     */
    public function games()
    {
        return $this->hasManyThrough(Game::class, Account::class)->where('status', Game::STATUS_COMPLETED);
    }

    /**
     *
     * @param $role
     * @return bool
     */
    public function hasRole($role)
    {
        return isset($this->role) && $this->role == $role;
    }

    public static function roles()
    {
        return [ self::ROLE_USER, self::ROLE_ADMIN];
    }


    /**
     *
     */
    public function admin()
    {
        return $this->hasRole(self::ROLE_ADMIN);
    }

   

}


via Chebli Mohamed

delete code in string with loop when read PDF with php

I´m traying to read PDF file with this library, \Smalot\PdfParser\Parser(); in laravel 5.6

i´m reading all my pdf ok, i´m getting all my content ok. i´m deleting headers from my PDF and i´m inserting by line in array.

and this it´s data:

    [0] =>  MARTIN CARRILLO MARIA ESMERALDA ALHAMBRA 10 958 54 38 93
    [1] => 1745591 ESPIGARES DIAS JOSE ANTONIO ALHAMBRA 11 958 54 33 32
    [2] => 1770062 GUTIERREZ TITOS JOSE MANUEL ALHAMBRA 12 958 54 04 10
    [3] => 1793228 MARTIN COBOS ANTONIO ALHAMBRA 18 958 54 33 28
    [4] => 1768807 GOMEZ CARRILLO JOSE ALHAMBRA 20 958 54 32 72
    [5] => 1830072 RODRIGUEZ RUANO BUENAVENTURA ALHAMBRA 21 958 54 35 86
    [6] => 1759534 GARCIA ARIAS MARIA ISABEL ALHAMBRA 22 958 54 07 87
    [7] => 1831938 RODRIGUEZ JIMENEZ MIGUEL ALHAMBRA 27 958 54 08 77
    [8] => 1722022 GUTIERREZ FERNANDEZ AMANDA HILDA ALHAMBRA 3 958 49 98 62
    [9] => 1746872 DIAZ FLORIAN DOLORES ALHAMBRA 30 958 54 33 99
    [10] => 1817587 PEREZ LASTRA SAUL ADAN ALHAMBRA 32 958 54 31 46
    [11] => 1724006 AMIGO MOLINA MARIA MATILDE ALHAMBRA 35 958 54 31 91
    [12] => 1745604 ESPIGARES GOMEZ JORGE ALHAMBRA 37 958 54 02 22
    [13] => 1745595 ESPIGARES ESPIGARES JOSE ALHAMBRA 40 958 54 31 83

i need delete firt numbers, it´s code from this client. i´m doing this for delete code, but always it´s doing in firts element.

This is my code to tray delete my code from all string...

// loop to get data line
for($i=0; $i<count($dataByLine); $i++){
   // delete code
   $numberCode = stripos($dataByLine[$i], " ");
   $codeClear = substr($text, 0, $numberCode);
   $dataByLine = str_replace($codeClear, "", $dataByLine);

   print_r($dataByLine);
}

this print_r() it´s my first content array. As you can see in my array, first element it´s without code.. but the rest, have code.

how i can to delete all code from this string¿?

thanks for help and read. Sorry for my english



via Chebli Mohamed

Adding some If/Else logic inside of rendered

I'm fairly new to VueJS. I'm using PusherBeams to send Notifications to a user, but locally and on my staging site, they're not setup as https or a localhost instance.

So what I'm thinking of doing is using my .env files to determine whether or not to load the Beamsclient

As when running locally, beams is throwing an error :

Error: Pusher Beams relies on Service Workers, which only work in secure contexts. Check that your page is being served from localhost/over HTTPS

I am wondering if I can write an IF to use the .env around the beamsClient constant that I'm setting.

Current code is :

<script>
import AppButton from '../components/AppButton';
import AppHeader from '../components/AppHeader';
import AppBackground from "../components/AppBackground";
import ColumnContainer from "../layouts/ColumnContainer";
import * as PusherPushNotifications from "@pusher/push-notifications-web";

const beamsClient = new PusherPushNotifications.Client({
    instanceId: 'sasasa',
});

beamsClient
    .start()
    .then(() => beamsClient.addDeviceInterest('club'))
    .then((beamsClient) => beamsClient.getDeviceId())
    .then((deviceId) =>
        console.log("Successfully registered with Beams. Device ID:", deviceId)
    )
    .catch(console.error);

export default
{
    components: {ColumnContainer, AppButton, AppHeader, AppBackground},
    props: ['user']
};
</script>


via Chebli Mohamed

mardi 7 septembre 2021

Call laravel controller from vue file

I created a component in vue file and I want to fetch data from laravel controller function.

currently, I have used axios to fetch data. But can I directly call laravel controller function?

Thanks in advance.



via Chebli Mohamed

Laravel undefined uPlot library

I'm trying to use the library uPlot into my view. But I'm getting an error of Uncaught ReferenceError: uPlot is not defined it seems that it can't find the uPlot library but this library is already loaded in the bootstrap as shown below

// resources/js/bootstrap.js

window._ = require('lodash');
try {
    window.Popper = require('popper.js').default;
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
    require('admin-lte');

    require('../../node_modules/uplot/dist/uPlot.iife.min');

} catch (e) {}

Now the chart.js is loaded in the resource/js/app.js as shown below

require('./bootstrap');
require('./chart');

Now in my chart.js I have this

$(function () {

    function getSize(elementId) {
        return {
          width: document.getElementById(elementId).offsetWidth,
          height: document.getElementById(elementId).offsetHeight,
        }
      }

  let data = [
    [0, 1, 2, 3, 4, 5, 6],
    [28, 48, 40, 19, 86, 27, 90],
    [65, 59, 80, 81, 56, 55, 40]
  ];

  const optsLineChart = {
    ... getSize('lineChart'),
    scales: {
      x: {
        time: false,
      },
      y: {
        range: [0, 100],
      },
    },
    series: [
      {},
      {
        fill: 'transparent',
        width: 5,
        stroke: 'rgba(60,141,188,1)',
      },
      {
        stroke: '#c1c7d1',
        width: 5,
        fill: 'transparent',
      },
    ],
  };

  let lineChart = new uPlot(optsLineChart, data, document.getElementById('lineChart'));
  window.addEventListener("resize", e => {
    lineChart.setSize(getSize('lineChart'));
  });
});

Now the element lineChart is already defined but it's throwing an error when the script reaches the line new uPlot. I checked the pub/js/app.js and the uPlot was successfully imported. As well as the chart.js is included in the app.js. But one this I noticed is the script of chart.js is added at the very top of the app.js file while the uPlot is added at the bottom part.

How can I fix this? It doesn't seem like the uPlot library is being read or loaded in the chart.js file



via Chebli Mohamed