Snapey

Member Since 4 Years Ago

Mansfield

at Novate / Microlise

Experience Points 1,229,755
Experience Level 50

0 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 1058
Lessons
Completed
Best Reply Awards 1749
Best Reply
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

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

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

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

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    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 Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

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

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

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

21 Apr
20 hours ago

Snapey left a reply on Route-Model Binding Doesn't Work

this

public function edit(ChannelStream $stream)

$stream

and this

Route::get('stream/{channelStream}/edit

channelStream

need to match.

Snapey left a reply on Getting "Trying To Get Property Of Non-object" In Blade View.

optional just prevents an error being thrown. I think both of your examples is correct... it depends what you want to do. Personally, i have never found a need for optional()

Snapey left a reply on Getting "Trying To Get Property Of Non-object" In Blade View.

ok, but null coalesce operator is useful for this type of situation.

As is the optional helper

Snapey left a reply on Getting "Trying To Get Property Of Non-object" In Blade View.

one or more of your clubs is missing a president

change your view to catch this

<td class="text-center">
    <span class="badge badge-warning">{{ $club->president->name ?? '----' }}</span>
</td>

Snapey left a reply on My Ajax Keep Returning 419 And 500 Error Status

look in the Laravel log file to see your 500 error

Snapey left a reply on Address Already In Use

dompdf perhaps requires a second thread and blocks for the reason I mentioned earlier

Snapey left a reply on How To Manually Do The OnEachSide() Function In Laravel 5.5 (this Does Exist In 5.7)

Below is a simple solution that shows 2 entries on either side (if there are pages). You will see a reduced number of links as you approach either end

            <ul class="pagination">
                @php($cur = $users->currentPage())
                @php($total = $users->lastPage())

                @if($cur-2 > 0) <li class="page-item"><a class="page-link" href="{{ $users->url($cur-2) }}">{{ $cur-2 }}</a></li>@endif
                @if($cur-1 > 0) <li class="page-item"><a class="page-link" href="{{ $users->url($cur-1) }}">{{ $cur-1 }}</a></li>@endif
                
                <li class="page-item active"><span class="page-link">{{ $cur }}</span></li>

                @if($cur+1 <= $total) <li class="page-item"><a class="page-link" href="{{ $users->url($cur+1) }}">{{ $cur+1 }}</a></li>@endif
                @if($cur+2 <= $total) <li class="page-item"><a class="page-link" href="{{ $users->url($cur+2) }}">{{ $cur+2 }}</a></li>@endif
            </ul>

It uses the standard pagination class to get the counters. Replace $users with your collection being paginated

Snapey left a reply on Address Already In Use

its probably conflicting with itself.

Remember that the php built in server can only run one request at a time and cannot listen for a new request until it has finished the last one. If you have any scheduler tasks, or are calling third party api then new web requests will be blocked until this has finished.

Better to use a proper web server

Snapey left a reply on Object Of Class Illuminate\Database\Eloquent\Collection Could Not Be Converted To Int

one of your str_plural statements is being given something other than a number to work with

Should take you about 30 secondeps to determine which one

Snapey left a reply on Using A Count Both Where Conditions In Blade File.

so user has many referals and user can belong to a referer

add a referrer column to the users table

users model;

public function referrer() { return $this->belongsTo(User::class, 'referrer', 'id'); }

public function referrals() { return $this->hasMany(User::class, 'referrer','id'); }


yes, the model refers back to itself

Then you can count the relations. https://laravel.com/docs/5.8/eloquent-relationships#counting-related-models

Snapey left a reply on Guys Need A Big Suggestion For Login Permission For Different Users Laravel

Why mess around with login? why dont you use the standard auth functionality?

Snapey left a reply on How To Add A Foreign Key To A Table After Migration Done Laravel

create a new migration and add the extra column

php artusan make:migration --table=product

the table= part tells laravel to adjust the existing table , not create a new one

20 Apr
1 day ago

Snapey left a reply on Don't Understand The Lavarel Filesystem

The public folder is where you keep your static assets that dont need to change. They are part of your application and not written to by the webserver. So you can then secure your public folders because nothing should need to change there when your application is running.

But you may also need somewhere to store user data, such as images. The storage folder is used for this so that you can allow your webserver to write to the storage folder. To allow these files to be served to the outside world, the symlink is used to allow parts of the storage folder to appear to the outside world.

If you cannot create the symlink then this might be a restriction of your webhost

To access files in the public folder from your php scripts, you can use the public_path() helper

Snapey left a reply on Global Variable In Controller

you would be best to study and understand the request lifecycle

Snapey left a reply on Using A Count Both Where Conditions In Blade File.

does this page show multiple users?

do you have a relationship referred on the user model?

Snapey left a reply on Laravel Not Behaving Consistently

its an .htaccess issue rather than being Laravel related

Snapey left a reply on Add Image As Background

If you reference an image in css file then the location of the image is either relative to the css file or absolute if you start the path with a forward slash.

If using absolute path then your site needs to be correctly hosted with the public folder as the root folder

You can use your network tools in your browser to check where the browser is looking for the file

These are basic web development skills which should be learned before writing any back-end code.

Snapey left a reply on Update Controller

Delete all related addresses and then add as new records.

To edit the existing records you would need to pass their ids to the form

Snapey left a reply on Array To String Conversion

if your data is coming in as a comma separated list, then use explode to create an array

$user->roles()->sync(explode(',',$role_id));
19 Apr
2 days ago

Snapey left a reply on Base Table Or View Not Found

did you run php artisan migrate?

Snapey left a reply on API Call Not Returning A Response

Sorry, i have been away. glad you got it sorted.

Snapey left a reply on API Call Not Returning A Response

As a guess, you are using PHP webserver (php artisan serve) ?

It only handles one request at a time, so you cannot start another request from inside a request.

Snapey left a reply on WHERE On Multiple Columns

You could create a scope that tidies things up

in your model;

    public function scopeSearch(Builder $query, String $needle, Collection $cols)
    {
        $cols->each(function($col) use($needle,$query){
            $query->orWhere($col, 'like', "%{$needle}%");
        });

        return $query;
    }

(note this is doing a loose 'like' search because this was not possible passing an array into orWhere)

$columns = collect(['title', 'description', 'note']);
$needle = 'carrot';

$articles = App\Article::whereIsPublic(true)
    ->search($needle, $columns)
    ->whereIsFinished(true)
    ->orderBy('title')
    ->get();

Snapey left a reply on WHERE On Multiple Columns

There is only one of us here intellectually challenged.

Snapey left a reply on Fix It

@stereoh

I fix it...

open file

control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, control-X, etc

Snapey left a reply on Please Solve This Error.

No-one can help you with this question.

Format your code

Describe what you are doing

Describe any error that you are seeing and include relevant messages.

Snapey left a reply on Fix It

wtf

Snapey left a reply on WHERE On Multiple Columns

Why? My example provides where on multiple columns (your title)

It does EXACTLY the same as your previous comment, but with less clutter as it does not need a closure.

Snapey left a reply on Guys Need A Big Suggestion For Login Permission For Different Users Laravel

Yes. You don't need the id column on this table but it can be useful.

Snapey left a reply on WHERE On Multiple Columns

I mentioned passing an array to where. Actually it also works with orWhere

$articles = Article::orWhere(['title'=>$needle, 'description'=>$needle, 'note'=>$needle])->get();

Snapey left a reply on Using Laravel Policy To Filter Eloquent Query

When defining the global scope, you can refer to the policy to know if the scope should be applied or if the scope varies according to the policy.

Snapey left a reply on Laravel Controller Is Not Returning To View?

Did you get to this function direct from a route or by some other method?

is your 'Going to view Folder' shown?

Snapey left a reply on Unable To Write To Directory While Populating

you need to give your user write permission to storage folder (not www-data) since you are running this as part of cli artisan command

Check if you switch command line to storage folder that you can create both folders and files

Snapey left a reply on How To Join Table Using Eloquent?

Also, in your Role model you should have users() function and not user() since there can be many users for a role

Snapey left a reply on How To Join Table Using Eloquent?

just check it in tinker. Its the easiest way to check if your relationships are working

eg

>>> User::with('role')->find(1)

should return user 1 with their role. You will instantly see if role is found or if role_id is valid

Snapey left a reply on Restrict Only Single Occurrence Of A Column Using Blade

so you need to group by category?

Snapey left a reply on Foreign Key Constraints

if this is 5.8 the id column on the users table has changed to big int. Your column in posts (user_id) has to match the same format

Snapey left a reply on Eager Loading Null Nested Relationship Causes Memory Error When Null

just checking that you dont have any with() statements in your models?

Snapey left a reply on How To Save Upload Image To Directory In Laravel 5.8

you always have to use multipart/form-data if you want image content to be sent to the server.

You are saving the image in a new location but not remembering where you put it

Snapey left a reply on Guys Need A Big Suggestion For Login Permission For Different Users Laravel

no. This hotels_users table is a pivot associating users with hotels. You still authenticate User model as normal

18 Apr
3 days ago

Snapey left a reply on How To Save Upload Image To Directory In Laravel 5.8

why do you want to save as json?

Snapey left a reply on Trouble With Defining Relationships

you seem to be overthinking it

Its just a straight belongsToMany relationship

post.php

public function tags()
{
    return $this->belongsToMany(Tag::class);
}

Tag.php

public function posts()
{
    return $this->belongsToMany(Post::class);
}

Snapey left a reply on How To Format Nested Set Table Data To Work In JsTree?

Baum appears to use the same table layout, and has commands like;

$tree = Category::where('name', '=', 'Books')->first()->getDescendantsAndSelf()->toHierarchy();

to dump out the entire hierachy

Snapey left a reply on How To Format Nested Set Table Data To Work In JsTree?

I tested this with a model called 'Material'

There were a couple of typos in the last post.

class Material extends Model
{
    public function children()
    {
        return $this->hasMany(Material::class, 'parent_id')
            ->select(['id', 'name', 'parent_id']);
    }

}

then query the table

$model = Material::with('children.children.children')->whereNull('parent_id')->get(['id','name']);

then cast to json

$model->toJson();

produces this json;

[
  {
    "id": 12,
    "name": "Materials",
    "children": [
      {
        "id": 1,
        "name": "Roofing",
        "parent_id": 12,
        "children": [
          {
            "id": 2,
            "name": "Asphalt",
            "parent_id": 1,
            "children": [
              {
                "id": 3,
                "name": "Architectural",
                "parent_id": 2
              },
              {
                "id": 4,
                "name": "Laminated",
                "parent_id": 2
              }
            ]
          },
          {
            "id": 5,
            "name": "Cedar",
            "parent_id": 1,
            "children": []
          },
          {
            "id": 6,
            "name": "Rubber",
            "parent_id": 1,
            "children": []
          }
        ]
      },
      {
        "id": 7,
        "name": "Hardware",
        "parent_id": 12,
        "children": [
          {
            "id": 8,
            "name": "Fasteners",
            "parent_id": 7,
            "children": [
              {
                "id": 9,
                "name": "Bolts",
                "parent_id": 8
              },
              {
                "id": 10,
                "name": "Screws",
                "parent_id": 8
              },
              {
                "id": 11,
                "name": "Anchors",
                "parent_id": 8
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "id": 13,
    "name": "",
    "children": []
  }
]

Snapey left a reply on How To Format Nested Set Table Data To Work In JsTree?

I must admit, when I have done this before, I just used regular relationships and could rely on eager loading relations.

eg

$model = Model::with('children.children.children')->whereNull('parent_id')->get('id','name');

This will only go to 4 levels.

Model has a children() relationship

        public function children()
        {
            return $this->hasMany(Model::class)->select('id','name','parent_id');
        }

Its a good few years ago though so I might be mistaken.

Then, if you have this structure, you can map over it, just pulling out the values you need if the structure does not suit.

Snapey left a reply on Input Hidden Cant Achieve In Modal Javascript

you should not have html element ids inside a loop. They are meant to be unique within the page

Put the club id in a data attribute of the button so that you can grab it when the button is clicked

Snapey left a reply on WHERE On Multiple Columns

i think where() accepts an array, but these are treated as AND conditions

Snapey left a reply on Using Laravel Policy To Filter Eloquent Query

Maybe you need global scopes? Automatically apply restrictions to your eloquent queries.

https://laravel.com/docs/5.8/eloquent#query-scopes