WallyJ

Experience

3,770

0 Best Reply Awards

  • Member Since 1 Year Ago
  • 12 Lessons Completed
  • 1 Favorite

10th October, 2018

WallyJ left a reply on ID Does Not Pass From View To Controller • 1 week ago

@Cronix , You are correct. I am using a resource controller.

I didn't realize that those controllers could not receive parameters like that.

I guess this is one problem with combining so many learning resources that tutorials get combined into things that sometimes don't work together. I never thought about passing the variable through a hidden field in the form.

Now when I dd($contact), I get the entire record for the correct contact in the DB.

Edited the rest of my code and my form works like a champ now. Thanks!

And thanks to everyone for their feedback. This community is awesome!

WallyJ left a reply on ID Does Not Pass From View To Controller • 1 week ago

@jekinney , thanks for the ideas. The form helpers are part of LaravelCollective who continued developing them when Laravel took them out of the framework.

www.laravelcollective.com

I checked the dd and it shows nothing, however, I'm wondering how my route can be wrong when the form sends the information directly to the controller via this line:

{!! Form::open(['route' => ['contactnotes.store', $contact->id]]) !!}

WallyJ left a reply on ID Does Not Pass From View To Controller • 1 week ago

@D9705996 , The url the form is on includes something like /contact/5, so the ID is being sent to that page. The contact_id is in the controller, and pulls the $contact->id

@jlrdw , The other fields are passed since "contactnotetext" shows up in the results.

WallyJ started a new conversation ID Does Not Pass From View To Controller • 1 week ago

Relationships are set in the model

Form in view:

{!! Form::open(['route' => ['contactnotes.store', $contact->id]]) !!}
                                    {{ csrf_field() }}
                                    <div class="form-group">
                                        {{Form::textarea('contactnotetext', null, array('placeholder' => 'Your note here', 'class' => 'form-control', 'rows' => '3'))}}
                                    </div>
                                    <div class="form-group">
                                        {{Form::submit('Add Note', array('class' => 'btn btn-primary'))}}
                                    </div>
{{Form::close()}}

ContactnotesController:

public function store(Contact $contact, Request $request)
    {
        $contactnote = new Contactnote;
        $contactnote->contact_id = $contact->id;
        $contactnote->contactnotetext = $request->contactnotetext;
        $contactnote->contact()->associate($contact);
        return $contactnote;
    }

I don't understand why the id is not being passed. When I submit the form I get:

{
"contact_id": null,
"contactnotetext": "adsf",
"contact": []
}

8th October, 2018

WallyJ left a reply on Trying To Get Property Of A Non-object - First() • 1 week ago

It was in a foreach loop. I added the optional helper and it worked fine. Thanks!

WallyJ started a new conversation Trying To Get Property Of A Non-object - First() • 1 week ago

When I use:

{{$deal->tasks->first()}}

in a view, I get this when rendered:

{"id":1,"created_at":"2018-10-01 00:00:00","updated_at":"2018-10-01 00:00:00","deal_id":11,"tasktext":"Call Client","taskduedate":"2018-04-20","taskcomplete":0}

When I add the following:

{{$deal->tasks->first()->tasktext}}

I get "Trying to get property of a non-object". I can see the field of the record from the previous code. How am I not calling it correctly?

WallyJ left a reply on Limit Results In Eloquent Results Using Relationship Table • 1 week ago

@snapey - Looks like that works. I'm going to run some more tests to make sure. I did have something in my view like that. Just didn't click with me. Thanks!

WallyJ left a reply on Limit Results In Eloquent Results Using Relationship Table • 1 week ago

@Snapey , does that go in the controller? And if so, where? Thanks!

4th October, 2018

WallyJ left a reply on Limit Results In Eloquent Results Using Relationship Table • 2 weeks ago

@Cronix , that code returns a total of 3 records across all contacts, not 3 for each contact.

@Snapey , Thanks for the link. I've seen it during my debugging research. There isn't really a clear workaround in that post. Also, it is disheartening to see a bug report like that from 2014 that still doesn't have a clear fix from Laravel or @TaylorOtwell . For a new programmer like me Laravel is supposed to make "plain language" programming easier, and this is a very simple concept. But this is discouraging.

2nd October, 2018

WallyJ left a reply on Limit Results In Eloquent Results Using Relationship Table • 2 weeks ago

The code I placed returns the correct records as written. It returns ALL contacts for the logged in user. It also shows EVERY contact note under every contact. I want it to show ALL contacts, but I don't want it to show ALL the contact notes.

I want the query to limit the number of contact notes for each contact to the latest 3 (not all). So if a logged in user has 10 contacts, each of those contacts has over 100 notes each, that dashboard gets long and unreadable really fast. But if each contact record in the collection only returned the most recent 3 notes, that would be manageable.

Hopefully that makes better sense.

WallyJ started a new conversation Limit Results In Eloquent Results Using Relationship Table • 2 weeks ago

The following code returns "Contacts" with their "Contactnotes", that belong to a given logged in user, in descending order correctly. I am trying to limit the notes per Contact to 3. I tried to add "take(3)" to the $query statement, but it limited the entire result to only 3 contact notes across all contacts. I believe I need to add a @foreach statement to my query, but I'm not sure how.

public function index()
    {
        //Look up contacts associated with the logged in user
        $user_id = auth()->user()->id; 
        $user = User::find($user_id);;
        $contacts = Contact::with(['contactnotes' => function($query) {
            $query->orderBy('updated_at', 'desc');
        }])
        ->with('deals')
        ->orderBy('contact1lastname', 'asc')
        ->where('user_id', '=', $user_id)->get();
        return $contacts;
        return view('dashboard')
        ->with('contacts', $contacts);
    }

27th September, 2018

WallyJ left a reply on Access Relations Field In View - Property [contactnotetext] Does Not Exist On This Collection Instance • 3 weeks ago

@Snapey , I received the error "Method Illuminate\Database\Eloquent\Collection::with does not exist." on the simplified code you gave me.

WallyJ left a reply on Access Relations Field In View - Property [contactnotetext] Does Not Exist On This Collection Instance • 3 weeks ago

I'm looking to order the contactnotes (not contacts) by the date field (updated_at), and in some views to limit the list to the latest 3 comments.

WallyJ left a reply on Access Relations Field In View - Property [contactnotetext] Does Not Exist On This Collection Instance • 3 weeks ago

Great suggestion. Can I also do an "orderBy" on the contactnotes?

WallyJ started a new conversation Access Relations Field In View - Property [contactnotetext] Does Not Exist On This Collection Instance • 3 weeks ago

I have the following as my controller and view (models have relationships set correctly), but when I try to pull an item from the relations part of a collection, I receive the error "Property [contactnotetext] does not exist on this collection instance"

Model

public function index()
    {
        //Look up contacts associated with the logged in user
        $user_id = auth()->user()->id; 
        $user = User::find($user_id);;

        //Return contacts with contactnotes
        $contacts = Contact::with('contactnotes')->orderBy('contact1lastname', 'asc')->where('user_id', '=', $user_id)->get();
        return view('dashboard')
        ->with('contacts', $contacts);
    }

View

<div>
Notes:
         @foreach($contacts as $contact)
               <div>
                    <p>ContactNote: {{$contact->contactnotes->contactnotetext}}</p>
               </div>
         @endforeach
</div>

If I put this in my controller:

dd($contacts);

I get the see the items under the relations arrow for contactnotes, so I know the data is being sent.

I think I'm just not referencing it in the view correctly.

24th September, 2018

WallyJ started a new conversation 'node' Is Not Recognized As An Internal Or External Command, Operable Program Or Batch File • 3 weeks ago

I am running a Laravel project on Laragon and am trying out the "new-ish" Puppeteer bridge "PuPHPeteer"...

When I run my code, I receive the following error:

Exit Code: 1(General error) Working directory: C:\laragon\www\Project\public Output: ================ Error Output: ================ 'node' is not recognized as an internal or external command, operable program or batch file.

I double checked and node is installed in Laragon, and I'm using 8.9.1.

Not sure what else I should do. I was running my Puppeteer straight from Javascript and the terminal within Laragon and it was working fine, but now that I'm using PHP, it can't seem to find Node.

16th August, 2018

WallyJ left a reply on Javascript Within A Blade View, Passing Variables Between PHP And Javascript • 2 months ago

Cool. To modify your example a bit, my app would have an existing list of products without prices in the DB (id, productname, price1, price2, price3).

The app queries the DB for a list of products, runs Puppeteer (Javascript) to get prices from 3 different websites (added to Javascript array via push method as part of a loop for each product/3 websites), then adds the prices to the DB with the appropriate products.

WallyJ left a reply on Javascript Within A Blade View, Passing Variables Between PHP And Javascript • 2 months ago

@Cronix , Always great and explanatory responses. This is the heart of learning! Thanks for that explanation. I will work on converting the form examples to pull the data from my variables rather than form fields, if I'm understanding you correctly. :)

WallyJ left a reply on Javascript Within A Blade View, Passing Variables Between PHP And Javascript • 2 months ago

I appreciate the suggestion. However, most of what I read in tutorials and other posts about PHP, Javascript, and AJAX, is based on form submissions. I don't need to submit a form. I need to run a DB lookup, pass the info to Puppeteer Javascript, which will run, return results, put those in the DB in the correct records, and then notify me that it is finished, (possibly showing me the new records recently added via the datamining process). Maybe I need to look more into examples that don't require form submissions when using AJAX

WallyJ started a new conversation Javascript Within A Blade View, Passing Variables Between PHP And Javascript • 2 months ago

So, PHP, server side, Javascript, client side... But when using something like Puppeteer (Javascript) for data mining, I need to pull data from the DB first, send variables from PHP to Javascript (Puppeteer, to mine the correct data), then the results from the mining (Javascript variables) need to be added/appended to the database to particular records, not just edited for screen view (client side).

I want to do this correctly with Laravel using models, routes, views, etc. But the passing back and forth may take multiple views, but maybe not. I also want to keep the Puppeteer Javascript in a separate file and call it when needed in the process.

Just looking for some logical suggestions of file structures and where to place each functional part of my code.

24th May, 2018

WallyJ left a reply on Passport Guzzle Response Directs To Login Page • 4 months ago

Sorry to tag along, but I have the same issue. If I go to a URL (For a site I am logged into) directly in Chrome, the page shows fine.

But if I use Guzzle and the command:

$client = new \GuzzleHttp\Client();
        $res = $client->request('GET', $url);
        echo $res->getbody();

I receive the login page, as if I'm not logged in, and also in my case, there are no images on the page.

I'm thinking there is a simple fix for this, but I am new to using Guzzle and DOM functions.

20th May, 2018

WallyJ left a reply on Count Of Records From DB::select • 5 months ago

Thanks Cronix. As you know, sometimes my brain gets stuck wanting to do things one way. This time, I tried the PHP count method before, directly in the SQL query, but couldn't get it to work. Then I realized that I need to be doing things the Laravel way as much as possible (which I am often doing), but couldn't get away from the DB:select to simply declare a variable with a simple count.

This was my solution:

$countofopps = count($homeopps);

and at the end of my return statement, I added:

->with('countofopps', $countofopps);

And simply put this in my view:

{{$countofopps}

Thanks again.

WallyJ left a reply on Count Of Records From DB::select • 5 months ago

I added this line to my controller just before the "return view" line and I received this error:

"Call to a member function count() on array"

WallyJ started a new conversation Count Of Records From DB::select • 5 months ago

I have the following query in a controller:

$opps = DB::select('SELECT mainfield, field2, etc. FROM table LEFT JOIN ( SELECT otherfield FROM othertable Group By fieldname');
return view ('home')
->with ('opps', $opps)

The query works fine. I just want to get a count of the records returned so that I can put this count at the top of the view.

In the view I currently return the results in a:

@foreach($opps as $opp)

But I want to put the count above the foreach.

I tried:

{{$opp->mainfield->count()}}

But of course it errors out saying it doesn't recognize that variable because it hasn't been called yet.

Just want to put a count of records returned at the top of the view. Thanks.

19th May, 2018

WallyJ left a reply on 1366 Incorrect String Value, On CSV Import To MySQL DB • 5 months ago

Update 3 - Solution:

I used the mb_convert_encoding function to specify the collation for the fields that were causing errors. (Ended up being 2 fields)

In the controller, I added a line above the field that was problematic, to force UTF8:

$remarks=mb_convert_encoding($remarks, "UTF-8");
$newdata->remarks=$remarks;

WallyJ left a reply on 1366 Incorrect String Value, On CSV Import To MySQL DB • 5 months ago

Update 2: So, it turns out that the CSV file I have is not encoded UTF-8.

I opened the file in Notepad, and saved as UTF-8 and it imported like a champ.

I may look at those PHP functions to force that field to be encoded as UTF so I don't have to convert the CSV file every time.

WallyJ left a reply on 1366 Incorrect String Value, On CSV Import To MySQL DB • 5 months ago

Update: I took the text directly from the CSV file and inserted it directly into the DB using PHPMyAdmin.

No error, so the DB is able to hold the accented "e".

But I can't figure out what is causing the error.

Any other suggestions? Thanks!

WallyJ left a reply on 1366 Incorrect String Value, On CSV Import To MySQL DB • 5 months ago

@rsvb - Thanks for the suggestion, but I believe this is a collation setting issue, not something that needs an additional PHP function to do something to the string.

I understand if I could use the function to test the string possibly. I was just hoping that someone would have experienced this particular error before and knows where the issue is. Still hoping for a solution. Thanks.

18th May, 2018

WallyJ left a reply on 1366 Incorrect String Value, On CSV Import To MySQL DB • 5 months ago

The error specifically denotes the word with the accented "e", "Incorrect string value: '\xE9cor a...' for column 'Remarks'" and then shows the data with the word showing the accented "e" replaced with that black diamond with the question mark - d�cor

WallyJ left a reply on 1366 Incorrect String Value, On CSV Import To MySQL DB • 5 months ago

varchar(800) utf8_unicode_ci

The query returns this field type and collation

WallyJ left a reply on 1366 Incorrect String Value, On CSV Import To MySQL DB • 5 months ago

When I upload the CSV, which calls the controller, that's where I receive the error. The value never gets into the column in the DB.

I have tried changing the collation, but it didn't keep the error from happening.

What else can I do?

17th May, 2018

WallyJ started a new conversation 1366 Incorrect String Value, On CSV Import To MySQL DB • 5 months ago

I receive this error on a field that contains a word with an accented "e" character. I have Googled to find that the issue seems to be in the collation of the data and the tables. I have changed the collation in the db to utf8_unicode_ci as well as utf8_general_ci, and changed my config/database.php to reflect utf8_general_ci, to no effect. I've also read that using "SET NAMES" to UTF8 as part of your query can help, but I'm using a simple $newdata->save() at the end of my field data, so I'm not sure how to do that. Ideas? Thanks.

21st April, 2018

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

@Sergiu17 , Follow up. Though this line works beautifully and cleanly, I just noticed that I lost my "orderBy('created_at', 'desc'), and can't figure out how to put it back in to order the contactnotes.

$contacts = Contact::with(['contactnotes'->orderBy('created_at', 'desc'), 'deals.tasks'])->find($id);

WallyJ left a reply on How To Properly Add A Package To My Project • 6 months ago

I added the links to the js and the css from the cdnjs and it immediately started working. Thanks.

Unfortunately now I can't get the parameters to work correctly. Will work on that next.

WallyJ left a reply on How To Properly Add A Package To My Project • 6 months ago

I appreciate your explanation, and will look into using mix and npm.

So should the developer give a better explanation in the instructions, or is it my job as the developer to understand the ecosystem enough to translate the instructions into what I really ought to do? #trulytryingtolearn

WallyJ left a reply on How To Properly Add A Package To My Project • 6 months ago

Appreciate the insight, but in an effort to understand, why would this package be available via composer if composer doesn't really "install" it in a way that it will actually work after installation?

20th April, 2018

WallyJ started a new conversation How To Properly Add A Package To My Project • 6 months ago

I found a cool date picker, and according to the instructions on its page at Packagist, I ran the following command in my terminal:

composer require eternicode/bootstrap-datepicker

I feel like I'm supposed to add something else somewhere, but I don't see it in the instructions on this page, or at the docs linked from that page.

https://packagist.org/packages/eternicode/bootstrap-datepicker

Maybe I'm supposed to know.

19th April, 2018

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

Wow... mystery solved...

I had a lingering old field in my contacts table called "contactnotes" when I originally created the table, before I created an entirely different table for "contactnotes". I changed the name of the field, and boom, everything worked as expected.

DD in the controller, then DD in the view, then the view itself.

Thanks guys! #newbmistakes #greatcommunity #thankful

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

Again, the contact notes were working before I tried to add the tasks. It was at that point I wasn't writing the code correctly to also request the tasks. That's when I was encouraged to use this:

$contacts = Contact::with(['contactnotes', 'deals.tasks')->find($id);

Which was your cleaned up version. :)

So I know the relationships were working and the contactnotes were being found.

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

Thanks for the encouragement.

Still quotation marks.

It's something simple. Just can't figure it.

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

Interesting, and depressing...

I get the quotation marks again. Even when I change the case of the "Contactnotes"

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

It's the strangest thing... If I perform: `` dd($contacts);


I get back:

Contact {#585 ▼ #connection: "mysql" #table: null #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:26 [▶] #original: array:26 [▶] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: array:2 [▼ "contactnotes" => Collection {#640 ▶} "deals" => Collection {#744 ▶} ] #touches: [] +timestamps: true #hidden: [] #visible: [] #fillable: [] #guarded: array:1 [▶] }


And I can click through the relations and see the contactnotes collection and the deals collection.

If I perform:

dd($contacts->deals)

I get back:

Collection {#744 ▼ #items: array:2 [▼ 0 => Deal {#682 ▶} 1 => Deal {#683 ▶} ] }


With only the deals collection, as expected.

However, if I perform:

dd($contacts->contactnotes);


I get back this:

""


That's it... a set of quotation marks... I am baffled.

18th April, 2018

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

Returns the Contact Info for the contact.

Also includes a "relations" array with 3 contactnotes and their values.

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

@Sergiu17, You gave me an idea, so I went back to my controller and did a:

dd($contacts->contactnotes);

and it returned the quotation marks. So the view is not the problem.

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

Thanks @JoaoPedroAS51 . That looks cleaner. And I changed the Model as you suggested. No change.

The dump of the $contactnotes still gives ""

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

I think there's something wrong with this line:

$contacts = Contact::with('contactnotes')->with('deals.tasks')->find($id);

Because it works for deals, but not for contactnotes.

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

Contact Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    public function User()
    {
        return $this->belongsTo(User::class);
    }
    
    public function Contactnotes()
    {
        return $this->hasMany(Contactnote::class);
    }
    
    public function Deals()
    {
        return $this->hasMany(Deal::class);
    }

    public function addContactnote($contactnotetext)
    {
            $this->contactnotes()->create(compact('contactnotetext'));
    }

}

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

There are definitely notes associated with that contact. That part was working before I added the tasks.

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

Yes. Same array.

WallyJ left a reply on Referencing A Variable Within A Controller Is Not Recognized • 6 months ago

Yes. It returns an array of 2 items (deals), and under item 0, the #relations has an array of 1 task, and under its attributes it shows the fields. So the data is connected.

There's something in the naming between the controller and the view that is off. I just can't figure out what.

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.