Snapey

at Novate / Microlise

Hire Me

Experience

841,855

1283 Best Reply Awards

  • Member Since 3 Years Ago
  • 874 Lessons Completed
  • 1 Favorite

22nd February, 2018

Snapey left a reply on LARAVEL Post Method Shows Token On Address Bar. • 5 hours ago

Do you have one form nested inside another? or a </form> closing tag mistyped as

You will note that it is also not respecting your action= and is posting back to presumably the same route as the edit route

Snapey left a reply on Inconsistencies In Date Casting? • 5 hours ago

@biishmar

protected $timestamp = false; tells eloquent that your table does not contain created_at and updated_at columns, so don't go trying to update them.

It does not alter the casting of dates. This is the $dates property.

Snapey left a reply on How To Solve This Data Update Error In Laravel • 5 hours ago

@biishmar What's the point in just repeating my answer?

21st February, 2018

Snapey left a reply on Accessing Exceptions Thrown By Vendor And/or Framework • 12 hours ago

if you dd($e) does that reveal the exception name?

Snapey left a reply on Column (Purchase Order No) Should Be Unique - Form Has Multiple Purchase Order Nos • 12 hours ago

heres an article covering this approach

http://biostall.com/check-if-php-array-contains-duplicates/

You could probably create a custom validation rule to do this?

Perhaps also use the unique validation rule to check if the number is already stored

Snapey left a reply on Column (Purchase Order No) Should Be Unique - Form Has Multiple Purchase Order Nos • 13 hours ago

how do you want to handle the situation where a number is repeated?

if you just want to filter them out, you could use a unique function on the list (if you make it a collection)

If you want to return an error to the user, you can use unique and then count the uniques, then also count the original POs if the numbers are different then there was a duplicate.

You probably still need to check at the database insert because one of the numbers used could have been on a previous form

Snapey left a reply on ERROR: Creating Default Object From Empty Value • 13 hours ago

dd the pin before you try and assign new values to it.

Do you have any global scopes?

Does your ActivationPin model know the name of tge table?

Snapey left a reply on Laravel 5.6: Parse Error: Syntax Error, Unexpected '?', Expecting Variable (T_VARIABLE) • 13 hours ago

create a simple test.php page in your public folder

put phpinfo() in it and then hit test.php in your browser

Sometimes the php version being used by the webserver can be a different version to the command line

Snapey left a reply on Stop Form Refreshing When Validation Fails • 13 hours ago

so, I think something like this

@if(isset(old('modal'))

    <script>
        $(window).load(function(){
            $(#{{ old('modal') }}).modal(\'show\');
        });
    </script>

  @endif 

Sorry, I changed my approach over the last couple of years

Snapey left a reply on Laravel 5.6: Parse Error: Syntax Error, Unexpected '?', Expecting Variable (T_VARIABLE) • 14 hours ago

yes, 5.6 now, but what was it before? Your error is typical of an incompatible php version

Snapey left a reply on Laravel 5.6: Parse Error: Syntax Error, Unexpected '?', Expecting Variable (T_VARIABLE) • 14 hours ago

did the 'upgrade' also significantly change the Laravel version?

Snapey left a reply on Stop Form Refreshing When Validation Fails • 14 hours ago

what is the id of the modal?

Snapey left a reply on Add Paginators To Related Models • 14 hours ago

If you only have the one parent model, I would pass it separately to the view, and then each child model separately also.

My post might help with paginating multiple collections at some point

http://novate.co.uk/using-multiple-pagination-links-on-one-page/

Snapey left a reply on No Line Breaks With <p>{{$user->description}}</p> • 14 hours ago

do you convert line breaks to ''?

Snapey left a reply on Stop Form Refreshing When Validation Fails • 14 hours ago

i have another trick. In the view it also checks for old('modal')

in the modal form, include a hidden field called modal and give it the value of the name of the modal. When validation fails, old('modal') will still be set and can be used to open the correct modal

Snapey left a reply on Accessing Exceptions Thrown By Vendor And/or Framework • 14 hours ago

You should try and catch the specific exception, or if this is difficult, then catch \Exception because you are inside a namespace

catch (\Exception $e) {

This should catch all exceptions.

The code is letting the exceptions pass by at the moment because the exception does not match what you are catching

You can then handle the exception, or throw a new one of your own

Snapey left a reply on Stop Form Refreshing When Validation Fails • 17 hours ago

You can use javascript to determine if the modal should be open when the page is loaded.

This site of mine uses this http://rotarota.net

Try to log in with some jibberish. You will see the page is Posted, but the modal reappears after failing validation

This on the master layout checks if the modal should be displayed.

  <?php 
    if(isset($modal)) {
      echo('$(window).load(function(){$("#' . $modal . '").modal(\'show\');});');
    }          
  ?>

I pass the name of the modal to the view if it should be rendered.

You will notice that the login and register modals are present on every page (they are an @include)

Snapey left a reply on How To Apply Workpress Theme In Laravel 5.4 • 23 hours ago

Thats a funny coincidence - I just built a car chassis from different components. Can you tell me how to fit a BMW series 3 bodyshell to it?

Wordpress themes are specific to wordpress. You will have more success with a HTML or Bootstrap theme from themeforest or wrapbootstrap but be prepared to do a lot of cutting and pasting to get it into the right shape.

Theme-downloader https://github.com/LaravelDaily/theme-downloader is a package by @PovilasKorop to provide plug and play theme installation like Wordpress. There are only compatible 2 themes at this time though.

Snapey left a reply on Scheduler Not Being Executed • 23 hours ago

The provided its running every minute (and not throwing an error) you should be good to go.

Possible errors are, incorrect path to the right version of php

Incorrect path to the root of your project

Snapey left a reply on Custom Order Of Collection • 23 hours ago

@austenc - Using merge doesn't leave any items out.

provided you cast each Eloquent collection to a support collection first

Snapey left a reply on Laravel - 301 Moved Permanently (from Disk Cache) • 23 hours ago

It will probably be your .htaccess equivalent for IIS

Snapey left a reply on Merging A Collection Of Two Models? • 23 hours ago

There are two more suggestions above that do not cause an issue with the ID's

Snapey left a reply on Merging A Collection Of Two Models? • 1 day ago

@austenc, yes, concat seems to do the trick, but random(4) does not do the same as shuffle()

If the collection (after concat) is made up M1,M1,M1,M1,M2,M2,M2,M2

then random seems to just select randomly in the same order. Like take 1,3,5,7 or 2,3,4,8

So running the code about 10 times, an instance of M2 is always the last item

Combining then advice from everyone;

$model1s = Business::inRandomOrder()->take(4)->get();
$model2s = User::inRandomOrder()->take(4)->get();

$collection = $model1->concat($model2s)->shuffle()->take(4);

Snapey left a reply on Blada Dont Show Http • 1 day ago

HTML doesn't need 'optimising' thats ridiculous.

Most people would blow any savings made just with a single wrong sized png or jpg.

Snapey left a reply on Merging A Collection Of Two Models? • 1 day ago

@foreach($collection as $model) 
@if($model instanceof `App\Business`)

<div class="col-md-3 col-sm-6 col-xs-12">
<div>
    <h4>{{$model->genre}} at {{$model->company_name}}</h4></a>
    <p >{{$model->city}}, <span>{{$model->street}}</span></p></a>
</div>
</div> 

@else 
     
<div class="col-md-3 col-sm-6 col-xs-12">
<div>
    <h4>{{$model->genre}} at {{$model->company_name}}</h4></a>
    <p >{{$model->city}}, <span>{{$model->street}}</span></p></a>
</div>
</div> 
@endif 
@endforeach

but hey - if your User model contains **all the same attributes** (?) as Business model then you don't need to split processing at all.


Snapey left a reply on Merging A Collection Of Two Models? • 1 day ago

@tykus

Yes, there is a clash with just merging the eloquent collections. You were right.

This seems to work and produce the same output, but always returns the same number of models;

$col1 = Member::inRandomOrder()->where('id','<',60)->take(4)->get();
$col2 = Club::inRandomOrder()->take(4)->get();

$collection=collect(array_merge($col1->all(),$col2->all()));

or in the OP case

$model1s = Business::inRandomOrder()->take(4)->get();
$model2s = User::inRandomOrder()->take(4)->get();

$collection = collect(array_merge($model1s->all(), $model2s->all())->shuffle()->take(4);

20th February, 2018

Snapey left a reply on Merging A Collection Of Two Models? • 1 day ago

i tried it selecting 4 from 2000 of one model and 4 from 60 of another model.

On that basis it would be difficult to observe a clash

I suppose on my mac I could run the select and merge multiple times and see if there were always 8 models in the result. If there is a clash there would be 7 or 6 sometimes?

Snapey left a reply on Merging A Collection Of Two Models? • 1 day ago

lm on my ipad now so cant check but its something like

@foreach($collection as $model) // choose better names
    @if($model instanceof App\Model1::class)

          // deal with model 1

     @else 
     
            // deal with model2

     @endif 
@endforeach

Snapey left a reply on Merging A Collection Of Two Models? • 1 day ago

the eloquent collection is an array of objects. The objects should not interfere with each other because they are not keyed by the id?

Each object is self contained.

Snapey left a reply on Merging A Collection Of Two Models? • 1 day ago

$model1s = Model1::inRandomOrder()->take(4)->get();

$model2s = Model2::inRandomOrder()->take(4)->get();

$collection = $model1s->merge($model2s)->shuffle()->take(4);


This works. No need to convert anything.

Snapey left a reply on Custom Order Of Collection • 1 day ago

You mean stick them in some random meaningless order?

Snapey left a reply on Merging A Collection Of Two Models? • 1 day ago

Not sure what you want here. 4 models in total, sometimes all in model1 block, sometimes 3 in model 2 and one in model 1, sometimes 2+2 ?

Do the two models have identical fields?

Snapey left a reply on Trying To Prevent Cross-Site Scripting • 1 day ago

You will miss anything that uses Vue. So the way around this is to either expressly strip {{ or }} (or both) from all user input (also their unicode equivalents), or to put v-pre inside any tag that contains user input.

For example

<h4 v-pre>{{$post->title}}</h4>

Snapey left a reply on Trying To Prevent Cross-Site Scripting • 1 day ago

Your data is inserted after blade has processed.

This;

{{ $user->name }}

is parsed by Blade, and becomes

<?php e($user->name) ?>

if $user->name contains {{badFunction()}} then the final output to the browser is

<?php e({{badFunction()}}) ?>

Vue sees this as something it should deal with. The e() function that makes html safe is not doing anything because there are no html tags to filter.

If you still don't believe me, change your code;

At the top of your layout, change <div id="app"> to something else like <div id="no-app">

Snapey left a reply on Trying To Prevent Cross-Site Scripting • 1 day ago

A bit more research. One option is to use v-pre on any element that can contain user input that is inside the vue area (ie, the div with 'app' by default)

Snapey left a reply on Trying To Prevent Cross-Site Scripting • 1 day ago

This issue is related to Vue. Since the default laravel boilerplate includes vue.js and because it is bound to a div with id of app which is basically the whole body of the page, then any double curly braces in the view are of interest to Vue (too many views here!)

See https://vuejs.org/v2/guide/syntax.html#Text

Basically, Vue treats anything inside double curly braces as javascript.

You can try this with a clean Laravel project. Install a new project. Run make:auth. Do NOTHING else except register a user with the name of {{ alert('yup') }} and you will see it appear on the page.

Snapey left a reply on Update A Database With Form Data • 1 day ago

Don't use route::any - there is no point. Its quite a special use case to get the best from it.

You need a GET route to display the page. This goes to the controller. The controller marshalls the required data and sends it to the view

You need a POST route to store the changes. This goes to the controller, the controller deals with the required actions or delegates them to something else and then returns a redirection to a suitable GET route.

Try the following as a good grounding. If necessary, quickly build a new project to get started in;

https://www.5balloons.info/tutorial-simple-crud-operations-in-laravel-5-5/

Snapey left a reply on Making Signup Usernames Unique And Url Friendly • 1 day ago

You also need to stop them creating names that conflict with your routes.

For instance, can someone call themselves 'login'

Snapey left a reply on Laravel Stores Too Much Sessions • 1 day ago

Routes in the api.php file are already prefixed api/

Add your route then do php artisan route:list and you will see what I mean

Snapey left a reply on Update A Database With Form Data • 1 day ago

My Controller has only this method:

So how do you show the view, and prepare the data?

From what you have said, I suggest you need to pass the $member to the view and also all the $members (to choose between)

Snapey left a reply on Laravel 5 Validation Request, How To Handle Validation On Update? • 1 day ago

@royduin bear in mind that this is only a solution where it is the user themselves that is updating their own record.

Its no use at all if you are trying to update another user (say, as an admin) in which case its better to fall back to using the id from the route, assuming restful routes.

Note that if using route binding, the parameter in the url might be other than id, for instance

Route::get('/members/{member}/update', '[email protected]');

then, in the form request, $this->member is what you need for the unique rule ignore attribute

Snapey left a reply on Scheduler Not Being Executed • 1 day ago

You have to run php artisan schedule:run every minute with a CRON job on your server. When it reaches EXACTLY 10:30 your function should run. Not at any other minute of the day.

This is why you have to run the command every minute and not just randomly when testing.

If you are not aware of a cron job running then it sounds like you are not doing this?

Snapey left a reply on Update A Database With Form Data • 1 day ago

What is this, three for the price of one? I take it we have moved on to yet another issue. Does this mean that the question is answered twice already?

Why are you mixing up doing things in javascript AND blade?

I suggest a new question tagged javascript so that those with better JS skills can help you.

ps, I don't know what you mean by "pull the id"

Snapey left a reply on How To Solve This Data Update Error In Laravel • 1 day ago

OK. The real problem stems from the fact that you are not hosting your site properly.

because if you are on the page http://localhost/FinalProject/public/edit/2 and you use a URL starting with / for instance POST to /edit/2 then you are saying, from the root of this server, go to the edit folder and send to the 2 page

this results in a URL http://localhost/edit/2 which gives page not found because it is not in your project.

The best solution is to change your web server to serve your public folder as the document root

If this is not possible then the next best thing is to use the route() helper to dynamically work out that you are in a sub folder and work out what the correct URL would be.

In this case give your route a name

Route::put('/adminedit/{id}', '[email protected]')->name('adminedit.patch');

and then reference this named route in the form

<form action="{{ route('adminedit.patch',$edd }}" method="post" enctype="multipart/form-data">

As I say, a lot of your problems are down to not hosting the site properly. This gives rise to a lot of confusing "try this", "try that" because your routes are not behaving how people expect them to behave.

Snapey left a reply on Laravel - 301 Moved Permanently (from Disk Cache) • 1 day ago

restart your webserver then

Snapey left a reply on Update A Database With Form Data • 1 day ago

one way

<form method="POST" action="/members/{{$member->id}}">

Since you are using html for the action, you need to use blade tags to add on the member id

or more robust, name the route

Route::post('/members/{member}', '[email protected]')->name('members.store);

then in the form, use the route helper and give it the name of the route and the parameter. If you pass it the whole member object it will know which value it needs.

<form method="POST" action="{{ route('members.store, $member) }}">

Snapey left a reply on How To Solve This Data Update Error In Laravel • 1 day ago

What is the full URL of your edit page when it is displayed?

Snapey left a reply on Laravel - 301 Moved Permanently (from Disk Cache) • 1 day ago

I think the 301 is telling you that your browser has remembered a previous 301 response and until that clears its going to redirect locally

https://stackoverflow.com/questions/9130422/how-long-do-browsers-cache-http-301s

Edit Your Profile
Update

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