Bvanhaastrecht

Bvanhaastrecht

Member Since 3 Months Ago

Experience Points 560
Experience Level 1

4,440 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed 0
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

13 Aug
6 days ago

Bvanhaastrecht left a reply on Unable To Retrieve Request Body With POST Method

Got it, using trailing slash I get no body, with no trailing slash it works.

Works:

POST https://url/api

No body:

POST https://url/api/

Is this as expected?

Bvanhaastrecht left a reply on Unable To Retrieve Request Body With POST Method

api.php

Route::post('/', '[email protected]');

BaseController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BaseController extends Controller
{
    public function apiRoot(Request $request)
    {
       var_dump($request->getContent());
       exit;
    }
}

As you can see I'm trying to get POST from api root directory. Just tested with an nested directory, and this does contain POST body. So the issue is only when trying to get body from root of /api/.

Because of migration from a non-laravel to Laravel app I need to have POST with body in API root.

Bvanhaastrecht left a reply on Unable To Retrieve Request Body With POST Method

Thanks, but the string of getContent() is empty, and so is all().

Bvanhaastrecht started a new conversation Unable To Retrieve Request Body With POST Method

Hello,

Just banging my head on something very simple. I'm doing POST request with application/json RAW body data in Postman. I'm unable to get this JSON body data in Laravel.

Using Laravel 5.8 I should be seeing data in $request->getContent(), but it's empty.

Disabled CSRF, disabled every Middleware without success.

Any ideas?

Thanks, Bastiaan

Bvanhaastrecht left a reply on Search For Null In Collection Stange Result

I see, thank you very much for the help and explanation!

Bvanhaastrecht left a reply on Search For Null In Collection Stange Result

Yes indeed, $invoices is a Collection.

The values() method indeed solves this. Thanks!

So getting a subset of a collection where the result doesn't contain the first row, keys are added to the rows. Is this how it works?

Bvanhaastrecht started a new conversation Search For Null In Collection Stange Result

Hello,

When I request rows where some column is null, the return format is different then from searching that same column for a value.

With value:

$invoices->where('contract_id', '00000185');

object(Illuminate\Database\Eloquent\Collection)#785 (1) {
["items":protected]=>
array(19) {
[0]=>
object(App\Models\CustomerInvoices)#817 (26) {

In JSON looks like (correctly):

[{
    "column1" : "data",
    "column2" : "data",
},
    "column1" : "data",
    "column2" : "data",
}]

Search for null:

$invoices->where('contract_id', null);

object(Illuminate\Database\Eloquent\Collection)#785 (1) {
["items":protected]=>
array(5) {
[19]=>
object(App\Models\CustomerInvoices)#836 (26) { ...

In JSON looks like (incorrect, different from with value search):

{
    "19": {
        "column1" : "data",
    "column2" : "data",
    },
    "20": {
        "column1" : "data",
    "column2" : "data",
    }
}

So you see with search for null each row gets a key. Because I'm outputing directory to json, I would like both outputs to look the same. Any idea why this happens?

Regards, Bastiaan

16 Jul
1 month ago

Bvanhaastrecht left a reply on Mass Assign With Input Filter

Adding the columns to $filleable would work, but as described in my initial post, we have a lot of tables and large ones to. It would require a lot of work to fill the $fillable's, but it isn't smart so to speak.

I'm looking for a way we don't need to fill all the $fillable's. Using $guarded will work for input names we know and want to filter, but that doesn't work for unknown input names.

I'm seeking something like:

$guarded = ['send-email', 'any other not known table column'];

Bvanhaastrecht left a reply on Mass Assign With Input Filter

This will not protect the model from filtering unwanted/unknown input's.

We use hidden inputs to give additional info into the request. But these should not be used in the mass assignment.

<input type="hidden" name="send-email" value="true">
<input type="text" name="name">
<input type="text" name="asset">

In this example, the 'send-email' should not be used in mass assignment. Secondly, for security reasons I would like to filter the request inputs to be only what is accepted by the model. These are unpredictable input names, so therefore dynamically filtering by column name would work, but how :-)

Bvanhaastrecht started a new conversation Mass Assign With Input Filter

Hello,

I would like to filter out input fields who not exist as a column in the table. I was thinking of something like:

$asset = new \App\Models\CmdbAssets();
$asset->create($request->only($asset->getFillable()));

I have my model setup as:

class CmdbAssets extends Model
{
    protected $table = 'cmdb_assets';
    protected $guarded = ['customer'];
}

I would expect the getFillable() would return my fillable attributes who are not guarded, so all of them. But the array is empty. I could fill the $fillable in the model, but we have a lot of large tables and wouldnt make sense. The $guarded way is much more clean.

Is there another way to accomplish input filtering in mass assignment?

Regards, Bastiaan

07 Jul
1 month ago

Bvanhaastrecht left a reply on Trying To Get Property Of Non-object With Relation

Interesting,

echo $rag->customer;

Returns 146;

As in the id field, seems like the relation is not there.

Bvanhaastrecht started a new conversation Trying To Get Property Of Non-object With Relation

Hello,

I'm pulling my hair over here. I'm trying to access an relation, but it's telling me there isn't one. While when I convert it to json, it's defenetely there.

{
    protected $table = 'customer_rag';

    public function customer()
    {
        return $this->belongsTo('App\Models\Customers', 'id', 'customer');
    }
}
$rags = \App\Models\CustomerRag::with(['customer'])
  ->where('level', 1)
  ->orWhere('level', 2)
  ->get();

foreach ($rags as $rag) {
  echo $rag->customer->customer;   //should give the customer property, string name
}

Throws: Trying to get property of non-object

While: $rag->toJson();

Gives:

{
    "id":5,
    "customer":{
        "id":146,
        "customer":"Test customer name",
        "email":"[email protected]",
        "created":"2017-09-14",
        "deleted":0
    },
    "level":1,
    "reason":"Test reason",
    "changed":"2018-12-31 08:17:41"
}

I know the name field in the Customers model isn't well chosen, 'customer'. But I got to deal with it as at this moment we cant change it.

Any help is very welcome!

Regards, Bastiaan

04 Jul
1 month ago

Bvanhaastrecht left a reply on Use Websockets In Non Vue App?

Alright, now I understand. Despite I'm not using Node I need to do npm run build so it compiles whats in /resources with Mix to /public.

Exactly what I was looking for, thanks!!

Bvanhaastrecht left a reply on Use Websockets In Non Vue App?

Thanks, but /resources/js/bootstrap.js includes NodeJS functions like 'require()', not native Javascript API. So including /resources/js/bootstrap.js in my plain HTML simply wont work. But that's where I perhaps don't understand the whole architecture.

Bvanhaastrecht started a new conversation Use Websockets In Non Vue App?

Hello,

We use plain HTML with views, no Vue or so.

I'm implementing Laravel Websockets. The documentation describes you should add code to the /resources/js/bootstrap.js file. It looks like these files are only loaded when using Vue or so, but not with just static view pages.

import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: window.pusherKey,
    wsHost: window.pusherHost,
    wsPort: window.pusherPort,
    disableStats: false,
    encrypted: true
});



window.Echo.channel('test')
    .listen('SendMessageToUserEvent', (e) => {
        console.log(e);
    });

How should I implement this piece of code in normal Javascipt?

Thanks in advance

24 May
2 months ago

Bvanhaastrecht left a reply on SelectRaw Returns JSON In Stead Of Object

@jlrdw & @snapey , these AES encrypted passwords are not used for user authentication but for passwords used by the framework to authenticate to remote API's. But thanks for the concerns.

@hpolthof, indeed a controller fires this code from a remote class. It's a colection. So got it working by doing:

$password = \App\Models\Passwords::selectRaw("AES_DECRYPT(password, '$secret') AS password")
 ->whereRaw("id = $id")
 ->get();

return $password->first()->password;

ps, somehow ->where(id, $id) doesn't work, generates ? in the where clause.

Thanks!

23 May
2 months ago

Bvanhaastrecht started a new conversation SelectRaw Returns JSON In Stead Of Object

Hello,

Using Laravel 5.8, when I do:

return \App\Models\Passwords::selectRaw("AES_DECRYPT(password, '$secret') AS password")
 ->where('id', $id)
 ->get();

My return is in JSON format, I would expect it to be an object. I could convert it to object, but would like to know if there's something I could change to do it the right way.

Thanks, Bastiaan

10 May
3 months ago

Bvanhaastrecht left a reply on Multiple Levels Down

Ah, I see. You need to start with the most far out table.

It works, thank you very much!

Bvanhaastrecht left a reply on Multiple Levels Down

Got it working, but it's not fully Eloquent:

$this->hasMany('App\Models\CustomerProducts', 'customer')
            ->join('products', 'products.id', 'customer_products.product');

The closest I get with belongsToMany is this:

$this->belongsToMany('App\Models\CustomerProducts', 'billing_products', 'product', 'id');

Which produces:

"select * from `customer_products` inner join `products` on `customer_products`.`id` = `products`.`id`"

Which should be:

"select * from `customer_products` inner join `products` on `customer_products`.`product` = `products`.`id`"

So I do not see belongsToMany working with this.

08 May
3 months ago

Bvanhaastrecht left a reply on Multiple Levels Down

Looking at belongsToMany, this still seems to be a one level down method. I'm trying to go 2 levels down:

customer (Customer)
  products (CustomerProducts)
    product (Products)

Bvanhaastrecht started a new conversation Multiple Levels Down

Hello,

I'm bumping my head over the following:

DB structure:

customer (the customers table)
  id

customer_products (customer can have multiple entries to determine which products are enabled)
  id
  product
  customer
  enabled

products (the products table)
  id
  name
class Customers extends Model
{
    public function products()
    {
        return $this->hasMany('App\Models\CustomerProducts', 'customer');
    }
}

class CustomerProducts extends Model
{
    public function product()
    {
        return $this->belongsTo('App\Models\Products', 'product', 'id');
    }
}

$customer = \App\Models\Customers::with(['products'])
            ->where('id', 1)
            ->first();

I can get the products, but the product itself doens't cary the nested product table info like 'name'.

I can successfully do:

$CustomerProducts = \App\Models\CustomerProducts::with('product')
            ->where('customer', 1)
            ->get();

Tried:

$customer = \App\Models\Customers::with(['products', 'product'])
            ->where('id', 1)
            ->first();

But this fails because 'product' cant be found.

How can I get the nested product details thru the 'customer'?

Kind regards, Bastiaan

30 Apr
3 months ago

Bvanhaastrecht left a reply on Help Needed With First Time Design

Hi, thank you for your reply, much appreciated!

I'm not there yet. When a user logs in the current status should be requested from the remote api's, this will be done with Guzzle, will this be done async? When Guzzle fulls the DTO's, how does the current user who's on a particular view get that data? There needs to be some form of async in place I guess? Ajax or websocket perhaps?

Bvanhaastrecht started a new conversation Help Needed With First Time Design

Hello,

I'm an php developer and last few days I started to explore Laravel. I'm trying to breakdown my current application and fitting it into Laravel's modules. I could really use some help with finding the right approach for each action within my current application.

My current application is an administrative portal for all kinds of remote applications. My application accesses the API's of the remote's for active view, issue creates and changes. The current state of the remote API's is not cached, it's real-time view. We store customer information in a MySQL DB. When a user or customer logs into the app, the API's are accessed and it's data is stored in the php session. We use this session to display it in the php pages. The changes done against the API's are done instantly, no queue or so. The application has some sub features like a message center, monthly invoicing of current usage, and statistics. The portal is flat in terms of back-front end. It's mixed html with php with jquery for dynamic data.

My shortterm goal is to rebuild the application with laravel, make a robust layered design which can hold for years to come. Make use of blade with html.

My longterm goal is to make it front-backended with Vue.

The thing I'm currently stuck on is the way to handle the remote API's and how to store it's data for the user who's accessing it during it's session. I have looked into Queue/Jobs. I've managed to execute API requests in a job but then my challenge, how to store the data for the user/views to use it. And how to tell the user the data is there for it to use. And it must be fast as the user is waiting for the data in it's session.

Hope someone can give me push in the right direction here.

Thanks, regards, Bastiaan