elliotk

Experience

10,980

0 Best Reply Awards

  • Member Since 7 Months Ago
  • 106 Lessons Completed
  • 0 Favorites

12th August, 2017

elliotk left a reply on Best Practice For Controllers • 1 month ago

Thank you for your responses, very interesting.

I've decided to go for extra controllers and nest them inside an admin folder.

11th August, 2017

elliotk started a new conversation Best Practice For Controllers • 1 month ago

Hello,

I am looking for some best practice advice as I continue to learn Laravel.

I have a Post model and a PostController with index() and show() methods to display posts to the users on the front end.

I am now building an admin back end panel to manage the blog.

Within the admin panel, I have a table of posts that the administrator can see. They can add a new post and also click edit / delete which call the usual methods: edit(), update(), store(), create() and destroy().

To get the admin view, I've added an adminIndex() method, which returns the posts to the relevant admin view, which is a different view to what the users will see.

My question is, is that right? What do others do? Watching the videos here, I am really trying to stick with the standard controller methods.

To that end, I thought of the following options

  1. Create a new method on the existing controller (that's what I have done with adminIndex()).
  2. Create a second controller AdminPostController and then use the same Post model and then I can stick with the normal method names. Is it ok to have 2 different controllers use the same model?
  3. Create an AdminController for managing Posts, Users etc, but this again would stray away from using the standard methods.

Would really appreciate others thoughts here.

Thanks

Elliot

10th August, 2017

elliotk left a reply on Editing A Pivot Table Relationship • 1 month ago

Thanks @ashbakernz that was really helpful.

elliotk started a new conversation Editing A Pivot Table Relationship • 1 month ago

Hello everyone,

Working my way through tutorials and the forum building myself a basic blog application as I learn the framework.

I have a Post model and a Tag model. I can create Posts and associate them with many Tags.

I am now looking at how to edit existing posts, which is fine, apart from the tags.

In my edit function on my Posts Controller, I grab the post, and then all available tags. I then pass these to my view.

    public function edit(Post $post)
    {
        $tags = Tag::orderBy('name', 'asc')->get();
        return view('admin.posts.edit', compact('post', 'tags'));
    }

Question 1. I'm not sure how to eager load the existing tags on the post when using route model binding. My Post model does have a tags() method. I'd then pass this all to the view.

Putting that aside, when I get to the view, it's getting really ugly and in fact isn't working. I presume that I am now loading the tags by doing the @foreach ($post->tags as $postTag)?

                <select id="tag[]" multiple="multiple" class="form-control" name="tag[]">
                    @foreach ($tags as $tag)
                        <?php $selected = null; ?>
                        @foreach ($post->tags as $postTag)
                            @if ($tag == $postTag)
                                <?php $selected = 'selected="selected"'; ?>
                            @endif
                        @endforeach
                        <option value="{{ $tag->id }}" {{ $selected }}>{{ $tag->name }}</option>
                    @endforeach
                </select>

I go through all the tags, and check if the tag matches one of the tags already associated with the post to set it as selected.

Question 2. There must be a cleaner way of doing this?

Would appreciate any help and pointers.

Thanks

8th August, 2017

elliotk left a reply on How To Save Pivot Table Data • 1 month ago

Thanks @goatshark that was exactly what I needed. My request('tag') was already an array.

I've updated my code as follows and it worked perfectly.

        $post = Post::create ([
            'title' => request('title'),
            'user_id' => auth()->id(),
            'slug' => request('slug'),
            'body' => request('body')
        ]);

        $post->tags()->sync(request('tag'));

elliotk started a new conversation How To Save Pivot Table Data • 1 month ago

Hello,

I'm still learning Laravel, I am working through building a basic blog. I have the concept of tags for blog posts, which is a many to many relationship through a pivot table, I am now adding this to the UI.

I can receive an array of tags in to my store method on my controller, but I am not sure how to save that to the database.

I do some validation, and then call Post::create

My tags are in their own Tag model.

Would someone mind giving me a pointer on the bast way to save an array from request('tag');?

       Post::create ([
            'title' => request('title'),
            'user_id' => auth()->id(),
            'slug' => request('slug'),
            'body' => request('body')
        ]);

Thanks

Elliot

14th May, 2017

elliotk left a reply on API Working In Local, But 405 In Production • 4 months ago

All sorted.

Had to edit: /etc/nginx/webapps.hostname.conf

I've added PATCH and DELETE and it now works as expected.

if ($request_method !~ ^(GET|HEAD|POST|PATCH|DELETE)$ ) {
                return 444;
        }

Thanks for all your help.

elliotk left a reply on API Working In Local, But 405 In Production • 4 months ago

I wasn't really keen on installing the module, I see there are a lot of security concerns around that module. Instead, I have installed nginx.

Just got that up and running, hoping for no errors, but unfortunately, it's a new error now.

app.js:655 PATCH http://fastfrag.co.uk/replies/2 net::ERR_EMPTY_RESPONSE
app.js:717 Uncaught (in promise) Error: Network Error
    at createError (app.js:717)
    at XMLHttpRequest.handleError (app.js:567)

elliotk left a reply on API Working In Local, But 405 In Production • 4 months ago

Thanks Tim, appreciate your help.

So, looking in my routes, i think it looks good. And as I said in my original post, it does work fine using valet on my local machine.

Route::delete('/replies/{reply}', '[email protected]'); Route::patch('/replies/{reply}', '[email protected]');

How would I check what endpoint my Javascript is connected to?

In my Reply.vue file, I am calling destroy as follows

`destroy () { axios.delete('/replies/' + this.attributes.id);

            $(this.$el).fadeOut(300, () => {
                flash('Reply has been updated!');
            });
        }`

and for update

`update () { axios.patch('/replies/' + this.attributes.id, { body: this.body });

            this.editing = false;
            flash('Reply has been updated!');
        },`

13th May, 2017

elliotk left a reply on API Working In Local, But 405 In Production • 4 months ago

It's a good point, I wouldn't have it on normally, I just set debugging on to try and get some more details on what the issue is.

The UI might remove them / edit them, but if you refresh the page, you should see the changes don't apply and it reverts back to what it was before. That's the issue.

In chrome console I see the error throwing, hopefully you can see that too?

I'm not really sure how to debug further though.

elliotk started a new conversation API Working In Local, But 405 In Production • 4 months ago

Hello All,

I have been working through the forum series on here, and I have implemented the Vue delete and update on the forum reply section. In valet, it's working fine, but on my "production server" i get 405.

Please feel free to try it yourself....

http://www.fastfrag.co.uk

I get the following in the console.

app.js:717 Uncaught (in promise) Error: Request failed with status code 405 at createError (app.js:717) at settle (app.js:1270) at XMLHttpRequest.handleLoad (app.js:557)

Anyone come across this?

11th March, 2017

elliotk started a new conversation Using Bootswatch With Laravel 5.4 • 6 months ago

I thought I'd share how simple it is to use Bootswatch in Larvel 5.4 with this simple guide.

Create a fresh Laravel install.

laravel new mynewproject

Then, install the Bootswatch dependancies via npm.

npm install bootswatch

Following this, edit your resources/assets/saas/app.scss


// Fonts
@import url(https://fonts.googleapis.com/css?family=Raleway:300,400,600);

// Variables
// @import "variables"; - Default variables included with laravel no longer required

// Bootswatch Variables for your chosen theme
@import "node_modules/bootswatch/flatly/variables";

// Bootstrap
@import "node_modules/bootstrap-sass/assets/stylesheets/bootstrap";

// Bootswatch theme main CSS files
@import "node_modules/bootswatch/flatly/bootswatch";

Install your npm dependancies

npm install

Finally, compile your saas.

npm run dev

You should then be using your chosen Bootswatch theme, in this case "flatly"

26th February, 2017

Edit Your Profile
Update

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