lundi 24 février 2020

Laravel 5.8 custom primary keys that allows hasMany belongsTo relationships

I am trying create two database tables (boxes and items) that will eventually be coded into 2 models with a hasMany belongsTo relationship in Laravel 5.8

These are the migrations I hope to make (below).

create_boxes_table.php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBoxesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('boxes', function (Blueprint $table) {
            //$table->bigIncrements('id');
            $table->string('box_barcode');      //**want this to be my id that can increment**
            $table->string('sort_description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('boxes');
    }
}

create_items_table.php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateItemsTable extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            //$table->bigIncrements('id');
            $table->string('item_barcode');  //**want this to be my id that can increment**
            $table->string('its_box_barcode');
            $table->string('item_quality');
            $table->timestamps();


        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('items');
    }
}

How do I make Laravel recognize these as ids that I would want to make relationships on in my CRUD? For example:

  • show only the item_barcodes that belong to box_barcode TRB0001 when someone clicks on its box link
  • show only the item_barcodes that belong to box_barcode TRB0002 when someone clicks on its box link
  • etc etc

Unless there is a better way to structure these data tables for relationships

See example below:

enter image description here



via Chebli Mohamed

Laravel API returns file and client must show download dialog

I am developing an Laravel 5.3 API Brige to download some files to various systems. What I want to achieve:

  1. User clicks download button
  2. PHP web does a cURL post request to API
  3. Api response may be a file or a 404 HTTP Code
  4. Client browser shows file download dialog

APi method:

    $reportService = new ReportService($request->get('vRefGMS'));
    $reportData = $reportService->handle();
    if ($reportData) {
        $serverService = new NetServerService($reportData);
        $csvFile = $serverService->handle();
        if ($csvFile != null) {
            return response()->file($csvFile);
        } else {
            return abort(404);
        }
    } else {
        return abort(404);
    }

Now i will show you the code I had try.

PHP code in the web for the download:

    $uri = $this->getEndpointShow($this->reportCode, SELF::ENDPOINT_REPORT_DOWNLOAD);
    $file = $this->apiConnection->downloadReport($uri, $this->reportCode);
    if ($file) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        flush();
        readfile($file);
    } else {
        echo "alert('Can´t find a report file')";
    }

And the downloadReport method:

public function downloadReport($uri, $reportCode)
{
    if (!$reportCode) {
        throw new InvalidArgumentException("No report Code");
    }
    $cURLConnection = curl_init();
    curl_setopt($cURLConnection, CURLOPT_URL, self::BASE_API_URL . $uri);
    curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($cURLConnection, CURLOPT_POST, TRUE);
    curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, "vRefGMS=$reportCode");
    curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . self::API_ACCESS_TOKEN
    ]);

    $response = curl_exec($cURLConnection);

    if ($response === false) {
        throw new Exception(curl_error($cURLConnection), curl_errno($cURLConnection));
    }

    curl_close($cURLConnection);

    return $response;
}

As you can see in the PHP code for the download I have a $file var which always comes blank $file = ''. I did also try on the api with return response()->download($csvFile) with the same result.

Is possible that I am missunderstanding concepts, but I cannot achieve the file download. How can get this file?



via Chebli Mohamed

Why my Post api run in postman but doesn't run in react-native app

enter image description here I'm try to update 1 item to my server. It work in Post man

enter image description here But in react-native app, it do not work!



via Chebli Mohamed

dimanche 23 février 2020

Laravel: having trouble installing laravel-ffmpeg package

I'm a JS developer doing some Laravel and I'm having trouble using a package. I am trying to use a ffmpeg wrapper ( https://github.com/pascalbaljetmedia/laravel-ffmpeg ) and I went through the instructions to install it via composer and added it to config/app.php in the aliases and providers section as details.

At the top of my Users file, I have use Pbmedia\LaravelFFMpeg\FFMpegFacade; below the others.

I am then using it my code as follows in a function:

public function createThumb($url, $product) 
{
        $media = FFMpeg::open($url);
        $thumb = $media->getFrameFromString('00:00:05.00');
        $originalWidth = $thumb->width();
        $originalHeight =$thumb->height();
        $filename = "test";
}

I keep getting an error Error: Class 'App\UserProfile\FFMpeg not found referencing the line where I call it. I'm not sure what I am missing to use this.



via Chebli Mohamed

Have Laravel 5.8 count data in other fields not just unsignedInteger

Hi I am new to Laravel,

I have two database tables (boxes and items) aka a hasMany() relationship. I am trying to get laravel to display 4 results of the boxbarcode column not 5 of the box_id as you see in the screenshot. Basically, all items that are of box TRTB0001

enter image description here

enter image description here

The problem is it is looking at the box_id (1,1,1,1,1) not the boxbarcode (TRTB0001). How can I adjust my Model, Controller, View to display this? See code below.

Box.php (Model)

namespace App;

use Illuminate\Database\Eloquent\Model;

class Box extends Model
{

    protected $guarded = [];


    public function items(){

        return $this->hasMany(Item::class);
    }

}

Item.php (Model)

namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{

    public function company(){

        return $this->belongsTo(Company::class);
    }

}

boxesController.php (Controller)


namespace App\Http\Controllers;

use App\Box;
use Illuminate\Http\Request;

class boxesController extends Controller
{

    /**
     * Display the specified resource.
     *
     * @param  \App\Box  $box
     * @return \Illuminate\Http\Response
     */
    public function show(Box $box)
    {        
        return view('boxes.show', compact('box'));
    }


}

show.blade.php

@extends('layout')


@section('title', 'Show Box')  


@section('content')

<h1 class="title"></h1>

<p> <a href="/projects//edit">Edit Box</a></p>

<h3 class="content">Status: </h3>

<hr>

<h5 class="content">List of Box Items:</h5>

<!-- ONLY SHOW TASK <DIV> IF A TASK EXISTS -->
@if ($box->items->count())
    <div>
        @foreach ($box->items as $item)

            <div>

                <form method="POST" action="/items/">
                    @method('PATCH')
                    @csrf

                    <label class="checkbox " for="in" >

                    <input type="checkbox" name="in" onChange="this.form.submit()" >
                            

                    </label>

                </form>         

            </div>

        @endforeach

    </div>
@endif

@endsection



via Chebli Mohamed

i move my laravel application but it always redirected

i have laravel apps owned by my friend in my server and point to hxxps://mydomain.com/ then i download it to local but it alwasy redirected.

My friend said that it only redirect the apps with .htaccess, and then i remove that files.

every time i open the app it always pointed to hxxps://localhost/ (localhost with http) and i have no idea what happenned,

every config in .env config/app have been checked and no item that describe about that domain, all database item have been checked also.

when debugging the line code i got

C:\wamp64\www\bikinmall\project\vendor\symfony\http-foundation\ResponseHeaderBag.php
$headers = $this->allPreserveCase();

and the header one of value is ['location'] = https://localhost/

how come this variable come? where to find it?



via Chebli Mohamed

Larvel's Passport error with frontend.....Everything works great till I have to run the command npm run dev and then I get the following error

dev /var/www/html/MyProject

npm run development

@ development /var/www/html/MyProject cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js

98% after emitting SizeLimitsPlugin

ERROR Failed to compile with 6 errors 2:10:21 AM

error in ./resources/js/components/passport/Clients.vue

Module Error (from ./node_modules/vue-loader/lib/index.js): [vue-loader] vue-template-compiler must be installed as a peer dependency, or a compatible compiler implementation must be passed via options.

@ ./resources/js/app.js 16:34-78 @ multi ./resources/js/app.js ./resources/sass/app.scss

error in ./resources/js/components/passport/AuthorizedClients.vue

Module Error (from ./node_modules/vue-loader/lib/index.js): [vue-loader] vue-template-compiler must be installed as a peer dependency, or a compatible compiler implementation must be passed via options.

@ ./resources/js/app.js 17:45-99 @ multi ./resources/js/app.js ./resources/sass/app.scss

error in ./resources/js/components/passport/PersonalAccessTokens.vue

Module Error (from ./node_modules/vue-loader/lib/index.js): [vue-loader] vue-template-compiler must be installed as a peer dependency, or a compatible compiler implementation must be passed via options.

@ ./resources/js/app.js 18:49-106 @ multi ./resources/js/app.js ./resources/sass/app.scss

error in ./resources/js/components/passport/Clients.vue

Module build failed (from ./node_modules/vue-loader/lib/index.js): TypeError: Cannot read property 'parseComponent' of undefined at parse (/var/www/html/MyProject/node_modules/@vue/component-compiler-utils/dist/parse.js:14:23) at Object.module.exports (/var/www/html/MyProject/node_modules/vue-loader/lib/index.js:67:22)

@ ./resources/js/app.js 16:34-78 @ multi ./resources/js/app.js ./resources/sass/app.scss

error in ./resources/js/components/passport/AuthorizedClients.vue

Module build failed (from ./node_modules/vue-loader/lib/index.js): TypeError: Cannot read property 'parseComponent' of undefined at parse (/var/www/html/MyProject/node_modules/@vue/component-compiler-utils/dist/parse.js:14:23) at Object.module.exports (/var/www/html/MyProject/node_modules/vue-loader/lib/index.js:67:22)

@ ./resources/js/app.js 17:45-99 @ multi ./resources/js/app.js ./resources/sass/app.scss

error in ./resources/js/components/passport/PersonalAccessTokens.vue

Module build failed (from ./node_modules/vue-loader/lib/index.js): TypeError: Cannot read property 'parseComponent' of undefined at parse (/var/www/html/MyProject/node_modules/@vue/component-compiler-utils/dist/parse.js:14:23) at Object.module.exports (/var/www/html/MyProject/node_modules/vue-loader/lib/index.js:67:22)

@ ./resources/js/app.js 18:49-106 @ multi ./resources/js/app.js ./resources/sass/app.scss

   Asset      Size   Chunks             Chunk Names

/css/app.css 177 KiB /js/app [emitted] /js/app /js/app.js 2.11 MiB /js/app [emitted] /js/app

ERROR in ./resources/js/components/passport/Clients.vue Module Error (from ./node_modules/vue-loader/lib/index.js): [vue-loader] vue-template-compiler must be installed as a peer dependency, or a compatible compiler implementation must be passed via options. @ ./resources/js/app.js 16:34-78 @ multi ./resources/js/app.js ./resources/sass/app.scss

ERROR in ./resources/js/components/passport/AuthorizedClients.vue Module Error (from ./node_modules/vue-loader/lib/index.js): [vue-loader] vue-template-compiler must be installed as a peer dependency, or a compatible compiler implementation must be passed via options. @ ./resources/js/app.js 17:45-99 @ multi ./resources/js/app.js ./resources/sass/app.scss

ERROR in ./resources/js/components/passport/PersonalAccessTokens.vue Module Error (from ./node_modules/vue-loader/lib/index.js): [vue-loader] vue-template-compiler must be installed as a peer dependency, or a compatible compiler implementation must be passed via options. @ ./resources/js/app.js 18:49-106 @ multi ./resources/js/app.js ./resources/sass/app.scss

ERROR in ./resources/js/components/passport/Clients.vue Module build failed (from ./node_modules/vue-loader/lib/index.js): TypeError: Cannot read property 'parseComponent' of undefined at parse (/var/www/html/MyProject/node_modules/@vue/component-compiler-utils/dist/parse.js:14:23) at Object.module.exports (/var/www/html/MyProject/node_modules/vue-loader/lib/index.js:67:22) @ ./resources/js/app.js 16:34-78 @ multi ./resources/js/app.js ./resources/sass/app.scss

ERROR in ./resources/js/components/passport/AuthorizedClients.vue Module build failed (from ./node_modules/vue-loader/lib/index.js): TypeError: Cannot read property 'parseComponent' of undefined at parse (/var/www/html/MyProject/node_modules/@vue/component-compiler-utils/dist/parse.js:14:23) at Object.module.exports (/var/www/html/MyProject/node_modules/vue-loader/lib/index.js:67:22) @ ./resources/js/app.js 17:45-99 @ multi ./resources/js/app.js ./resources/sass/app.scss

ERROR in ./resources/js/components/passport/PersonalAccessTokens.vue Module build failed (from ./node_modules/vue-loader/lib/index.js): TypeError: Cannot read property 'parseComponent' of undefined at parse (/var/www/html/MyProject/node_modules/@vue/component-compiler-utils/dist/parse.js:14:23) at Object.module.exports (/var/www/html/MyProject/node_modules/vue-loader/lib/index.js:67:22) @ ./resources/js/app.js 18:49-106 @ multi ./resources/js/app.js ./resources/sass/app.scss npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! @ development: cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the @ development script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR! /home/zeshan/.npm/_logs/2020-02-23T21_10_21_973Z-debug.log npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! @ dev: npm run development npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the @ dev script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR! /home/zeshan/.npm/_logs/2020-02-23T21_10_22_036Z-debug.log



via Chebli Mohamed