Snapey

at Novate / Microlise

Hire Me

Experience

929,655

1344 Best Reply Awards

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

21st July, 2018

Snapey left a reply on String Value Showing With [" "] Characters • 4 hours ago

Or use value() (the eloquent function, not your column name)

$restaurant = Globals::where('name' , 'Nombre del restaurante')->value('value');

Snapey left a reply on Can Someone Review My Eloquent ? • 5 hours ago

but we've not finished there because you probably want to show the previous category

change the select

        <select name="category">

            @foreach($categories as $category)

                <option value="{{ $category->id }}"
                        @if($category->id == old('category',$skill->category_id) Selected @endif
                         >{{ $category->category_name }}</option>

             @endforeach 
        </select>

Snapey left a reply on Can Someone Review My Eloquent ? • 5 hours ago

In your Skill controller

// Skill Controller
public function edit($id)
    {
        $skill = Skill::with('category')->get();
        return view('admin.skill.edit',compact('skills','id'));
    }

You pass in $id but you do not use it. I assume you want a single skill with the id mentioned?

Then you are trying to pass $skills to the view but you don't have $skills

And you don't need to pass $id as the skill will have an id

// Skill Controller
public function edit($id)
    {
        $skill = Skill::with('category')->findOrFail($id);

        return view('admin.skill.edit',compact('skill'));
    }

In your relationships, no need to mention the foreign key (especially if you get it wrong)

// Skill.php
public function category()
    {
        return $this->belongsTo('App\Category');
    }
// Category.php
public function skills()
    {
        return $this->hasMany('App\Skill');
    }

You say you want to allow the user to choose other Category then you need to list all categories

Change controller

// Skill Controller
public function edit($id)
    {
        $skill = Skill::with('category')->findOrFail($id);

    $categories = Category::all();

        return view('admin.skill.edit',compact('skill','categories');
    }


// Admin able to choose and edit this category's to another category
<div class="form-group">
    <label>Category</label>
        <select name="category">

            @foreach($categories as $category)

                <option value="{{ $category->id }}">{{ $category->category_name }}</option>
             @endforeach 
        </select>
 </div>
 
<div class="form-group">
    <label>Skill Name</label>
    <input type="text" id="example-nf-email" name="skill_name" 
              value="{{ old('skill_name, $skill->skill_name ?? ''}}" class="form-control" />
</div>

Snapey left a reply on Creating A New Account With A Soft Deleted Email • 6 hours ago

(obviously it will only work once)

Snapey left a reply on Creating A New Account With A Soft Deleted Email • 6 hours ago

give @jlrdw credit for the best answer

Snapey left a reply on Creating A New Account With A Soft Deleted Email • 7 hours ago

@jlrdw's idea makes sense and saves a lot of rework. Before you delete, do something to the original email address.

but we don't want to lose the account the administrator "deleted" for legal reasons

so you probably don't want that account taken over by the new user else they will be attributed with previous activity.

Snapey left a reply on Create/update Sql Records Without Form • 9 hours ago

Just write to the database? Perhaps take some of the free courses available.

Snapey left a reply on Blade Directives - Why? • 9 hours ago

At its simplest your directive needs to include the string name of your var. For instance

imagine an @upper() blade

@upper($surname) needs to return echo($strToUpper($surname));

and not try to insert the value of $surname

Snapey left a reply on How To Run Scheduling Every 2 Sec • 10 hours ago

something like this;

    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {

        $dt = Carbon\Carbon::now();

        $x=60/5;

        do{

            // do your function here that takes between 3 and 4 seconds

            time_sleep_until($dt->addSeconds(5)->timestamp);

        } while(--$x > 0);

        })->everyMinute();
    }


fire this with your cron job every minute. It will run and take a note of the time, then do your function, return and then sleep until the time it started plus 5 seconds, then repeat until it has done it 12 times and then stops.

The cron job then fires another instance which will do the same.

Snapey left a reply on How To Run Scheduling Every 2 Sec • 10 hours ago

@m7vm7v yes, thats right. Just remind me, how many minutes are there in 5 seconds? :-p

Snapey left a reply on How To Run Scheduling Every 2 Sec • 12 hours ago

you cannot do it with schedule since cron will only call the scheduler once per minute

Snapey left a reply on Eager Loading Doesn't Work • 12 hours ago

anything extra added to your models like scopes?

I wonder if 'type' has any special meaning to eloquent?

You have a Type model and it contains the other relationships?

Snapey left a reply on Bring Sub Collection And It Subcollection... N Level? • 12 hours ago

sorry no. Sounds like a separate question

Snapey left a reply on How To Run Scheduling Every 2 Sec • 12 hours ago

You probably need to run continuously and in each loop usleep() and pass it the number of microseconds until the next multiple of 5 seconds

Snapey left a reply on Bring Sub Collection And It Subcollection... N Level? • 14 hours ago

You have to be careful of recursion, but;

if Document model has a column 'parent_id' and then relationships

public function parent()
{
    return $this->belongsTo(Document::class, 'parent_id');
}

public function children()
{
    return $this->hasMany(Document::class,'parent_id');
}

now any document can have parent document and children documents.

To automatically load, you can have a property in the model like;

public $with = ['children'];

Now, each time you load a document, its child documents are loaded, and for each of those, the child documents are loaded, and for each of those, the child documents are loaded etc etc

The problem with $with is that it can cause a lot of extra queries when all you want to do is work with the single document. This can be such an issue, you might want a separate model for just dealing with a single document.

Snapey left a reply on How To Categorizes Data By Month Of Birth • 14 hours ago

add birthday to $dates array in model

https://laravel.com/docs/5.6/eloquent-mutators#date-mutators

Snapey left a reply on SQL Query • 14 hours ago

The query in your very first post would have worked if column_1 was the upper number and column_2 the lower, but without values or a hint what column_1 and column_2 actually were then we could not get to the right answer straight away.

This is why I always prefer real code than abstracted answers

For instance if the query was

function aliveIn($year)
{
    return People::where('birthdate','>=',$year)->where('died','<=',$year)->get();
}

then the intent of the code would have been clearer

Snapey left a reply on SQL Query • 15 hours ago

In testing, I noticed that

SELECT * FROM orders WHERE 111 BETWEEN column_1 AND column_2;

only works if column_1 is the lower bound and column_2 is the upper bound. It does not work if the values are reversed meaning that it does not actually seem to check if the number is BETWEEN, its actually checking if value is more than the first clause and less than the second.

Snapey left a reply on SQL Query • 15 hours ago

because your comparitors are the wrong way around.

SELECT * 
FROM table1
WHERE column_1 <= 111
AND column_2 >= 111;

but think hard about what you want to happen when either column_1 or column_2 are exactly 111

Snapey left a reply on SQL Query • 15 hours ago

So your row contains something like

column_1 = 150
column_2 = 25

yet the first example does not work. I cannot think why not.

Snapey left a reply on Namespaces In PHP And Classes With Same Names • 15 hours ago

The namespace ensures you avoid collision so if you need to use new Assets in a class then you can use the namespace to determine the correct one.

If you find it confusing when using the code then you can alias it within the class where it is used

use Acme\App\Admin\Asset as AdminAsset

class myFunkyClass
{

    public function doStuff()
    {

        $asset = new AdminAsset();

Snapey left a reply on (1/1) MethodNotAllowedHttpException • 15 hours ago

Sooner or later you WILL need to post some code here

Snapey left a reply on Policy Always Returning: This Action Is Unauthorized. • 15 hours ago

I must admit I don't use policies very often, but your approach seems non-standard?

Have you checked the docs for the version you are running? You might be following an old tutorial?

Snapey left a reply on SQL Query • 16 hours ago

@tray whereBetween only checks one column between two values, not the other way around.

Snapey left a reply on (1/1) MethodNotAllowedHttpException • 16 hours ago

Actually my all forms are working fine on localhost machine,but as I hosted my website on server,then firstly error of Tokenmismatch error found,to solve this problem I have changed code in Exception/handler.php code so I found MethodNotAllowedHttpException error on all post method forms.

There's your problem, solving the wrong problem.

Step by step

  1. use network tools in your browser to check the URL your form is posted to

  2. examine the posted data and check that it includes;

_method: POST
_token: cGhudzyDEEG1YMuqC4m8l6daLAL1tAE3G03teMyC        // your token not mine
  1. php artisan route:list and check you have a route matching the route you saw in 1

AND, if you see the word 'public' in your URLs then fix that first

Snapey left a reply on SQL Query • 16 hours ago

Show some actual data. What I showed should work just fine if your understanding of the data is correct

Snapey left a reply on Problem In Conversion To Pdf In Laravel • 16 hours ago

also needs to be wrapped in
      <thead>
        <tr>
            <th>ID</th>
              <th>Date</th>
              <th>Time</th>
              {{-- <th>Customer Id</th> --}}
              <th>Customer Type</th>
              <th>Customer Name</th>
              {{-- {{-- <th>Customer Location</th> --}}
              <th>Customer Area</th>
              <th>Customer Booth</th>
              <th>Employee</th>
              <th>Rate/Litre</th>
              <th>No of Litres (Lt)</th>
              <th>Total (Rs)</th>
        </tr>
      </thead>

Snapey left a reply on Problem In Conversion To Pdf In Laravel • 16 hours ago

The first thing I spot is an in balance of comment tags

here

              {{-- {{-- <th>Customer Location</th> --}}

and here {{-- {{-- {{ $sal->customer_location }} --}}

Snapey left a reply on Problem In Date Format In Laravel? • 16 hours ago

Use the docs!

model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Pay extends Model
{
    public $table= 'pay';

    protected $dates = [
        'created_at',
        'updated_at',
        'date'
    ];

}

When you want a formatted date, chain it onto the date which is now an object;

  <td>{{ $pay->date->format('d-m-Y') }}</td>

Other Carbon functions here

https://carbon.nesbot.com/docs/

and the date formats in php here

http://php.net/manual/en/function.date.php

Snapey left a reply on Why My PHP Array Is Null • 16 hours ago

You can pass $dirtyElements in by reference by prefixing with &

however, you would be better using collections to reduce to just those items that are dirty and then call getdirty on those

something like

$dirtyElements = $items->filter(function ($item) {
    return $item->isDirty();
    })
    ->each($function($item) {
        return $item->getDirty();
    });

Snapey left a reply on SQL Query • 16 hours ago

chain two where statements

$record = table1::where('column1','>=',$number)
                        ->where('column2','<',$number)
                        ->firstOrFail();

chained, they are AND ed together

Snapey left a reply on Undefined Variable: P_accounts Occurs • 17 hours ago

If using the magic with method you need to capitalise the first letter of the variable name

return view('Overall.personal_accounts')->withP_accounts($p_accounts);

Snapey left a reply on How Can Replace Page In Laravel • 17 hours ago

Sorry there is not enough here to give any advice

Snapey left a reply on Problem In Date Format In Laravel? • 17 hours ago

add date to your $dates array in the model. The date will then be automatically converted to a carbon object when you retrieve it

You can then use all of Carbon's format options in the view

https://laravel.com/docs/5.6/eloquent-mutators#date-mutators

To use this, your date field must be in one of the datetime formats in the database, not just a string

Snapey left a reply on How To Check If The ID Belong To Authenticated User Before I Delete? • 17 hours ago

you have to use authorization tools that are in Laravel, or do your own simple check.

If you dont need to setup authorization then a simple check like below would work fine

Assume the task has a user_id column

public function destroy($id){

    $task = Task::where('user_id',Auth::id())
                      ->findOrFail($id);

    $task->delete();

this adds a where statement to the query so that only records of the user are considered

if you have relationships setup, another approach is

$task = Auth::user()->tasks()->findOrFail($id);

Some prefer this because your controller should not need to know how the relationship is created

20th July, 2018

Snapey left a reply on Blade Directives - Why? • 1 day ago

You need to remember that your blade directive needs to return the php code you want in the view and NOT the variable.

The actual output will be inserted when the view is finally rendered

Snapey left a reply on Laravel And Leaflet Polyline Storage • 1 day ago

or lat, long and sequence (multiple table rows per polyline)

Snapey left a reply on Show Image In View Stored In Storage • 1 day ago

@omankopyor please take note of how old the post is please. I doubt they are waiting for your answer.

19th July, 2018

Snapey left a reply on Multiple Relationships Between Two Models • 2 days ago

create columns of origin_id and destination_id

then create relationships on the model of origin and destination

Then in the relationship, you need to tell eloquent the name of the column that holds the foreign key and not just let it try and use convention

public function origin()
{
    return belongsTo(City::class, 'origin_id'j;
}

public function destination()
{
    return belongsTo(City::class, 'destination_id'j;
}

then use like

$route=Route::with(['origin','destination'])-find(1);

Snapey left a reply on Custom Accessor Not Working Correctly • 2 days ago

@zsoltgyure is correct. When you use pluck its like using select on the table.

When you use an accessor, the attribute is only evaluated when you try to access it.

Using pluck on the collection you are accessing the attributes of the model not the table columns.

Snapey left a reply on Custom Accessor Not Working Correctly • 2 days ago

Seems strange that you would have a guest attribute in a model called BasePrice

seems like there is more wrong here than this.

does the guest attribute have a value in the database?

How does this accessor make it into the form?

oh, and stop using that Form crutch please

18th July, 2018

Snapey left a reply on Auth • 3 days ago

If you have a database column isAdmin you can just test it direct, no need for a function

    public function handle($request, Closure $next)
    {

        abort_unless(Auth::check() && Auth::user()->isAdmin(), 404);

        return $next($request);
    }

Snapey left a reply on Creating Database Tables? • 3 days ago

You need to create Models that represent the tables.

I suggest some reading or watching Laracasts excellent tutorial videos

Snapey left a reply on Get Route Parameter And Query Parameters • 3 days ago

$request->all() WILL contain codigoconfig

Snapey left a reply on Can't Migrate A Foreign Key • 3 days ago

Please format your code by putting 3 backticks ``` on a line before and after each code block

You still have 'foreign_key' in your relationships, but you don't have a column with that name

Snapey left a reply on Can I Give Likes And Dislikes To Post Without Login In • 3 days ago

What are your thoughts on how they are doing it?

Its not obvious that they are doing it, but if they are, you would probably need to see the server code also.

17th July, 2018

Snapey left a reply on Soft Delete Pivot Records • 4 days ago

@mmickelson

i don't understand the relation to licences so I will use Company and Product

the relationship links the two, you want to 'soft' detatch the relationship

What I would do is create a model instead of the pivot. If you had a CompanyProduct model which belongsTo Company and belongsTo Product, then you can have deleted_at columns and treat it as a full model, with softdelete capabilities

Does it help to think about it that way?

Snapey left a reply on Parse Error: Syntax Error, Unexpected ';' • 4 days ago

what's this meant to do

$url .= ;

.= concatenates strings, but you didn't provide the second argument

Snapey left a reply on Can I Give Likes And Dislikes To Post Without Login In • 4 days ago

I'm saying you can hit the face and a record is sent to the server each time. Since the votes are percentages its impossible to say if the vote is registered because you don't know how many other votes there have been. For instance, if there were 10,000 votes then you would need to press 100 times for the percentage to increase.

However, thats beside the point. If you want to avoid a guest voting twice then you have to resort to fingerprinting with a library such as https://clientjs.org/. Its not foolproof though and it would be trivial for someone to post votes using guzzle.

Interestingly, the site's back-end is built with Laravel.

Snapey left a reply on Can I Give Likes And Dislikes To Post Without Login In • 4 days ago

Thats not what I said. I said HOW do you know they ARE blocking it?

Edit Your Profile
Update

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