tk90

tk90

Member Since 2 Years Ago

Experience Points 1,410
Experience Level 1

3,590 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 1
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

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

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

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

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    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 Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

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

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

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

10 May
4 months ago

tk90 started a new conversation Sort With JQuery UI

I would like to sort my pages with sortable. it works Controller:

$pages = Page::all();
        $id = $request->get('id');
        $sorting = $request->get('sorting');
        foreach ($pages as $item) {
            return Page::where('id', $id)->update(array('position' => $sorting));
        }

JavaScript:

$(".sortable").sortable({
        stop: function () {
            $.map($(this).find('div'), function (el) {
                var id = $(el).data('page-id');
                var sorting = $(el).index();
                $.ajax({
                    url: 'page/sorting',
                    type: 'GET',
                    data: {
                        id: id,
                        sorting: sorting
                    },
                });
            });
        }
    });

Blade:

@foreach($pages as $page)
        <div class="row" data-page-id="{{ $page->id }}">
            <div class="col-lg-5"><a class="text-dark d-block" href="{{ route('dashboard.page.show', $page->id)}}">{{$page->title}}</a></div>
            <div class="col-lg-2 text-lg-right">
                <div class="btn-group">
                    <a class="btn btn-light btn-sm" href="{{ route('dashboard.page.show', $page->id)}}"><span data-feather="info"></span> Info</a>
                    <a class="btn btn-light btn-sm" href="{{ route('dashboard.page.edit', $page->id)}}"><span data-feather="edit-2"></span> Bearbeiten</a>
                    <form action="{{ route('dashboard.page.destroy', $page->id)}}" method="post" class="form-delete">
                        @csrf
                        @method('DELETE')
                        <button class="btn btn-danger btn-sm" type="submit">L&ouml;schen</button>
                    </form>
                </div>
            </div>
        </div>
        @foreach($page->sub as $subPage)
        <div class="row subPage" data-page-id="{{ $page->id }}">
            <div class="col-lg-5 pl-5"><a class="text-dark d-block" href="{{ route('dashboard.page.show', $subPage->id)}}">{{$subPage->title}}</a></div>
            <div class="col-lg-2 text-lg-right">
                <div class="btn-group">
                    <a class="btn btn-light btn-sm" href="{{ route('dashboard.page.show', $subPage->id)}}"><span data-feather="info"></span> Info</a>
                    <a class="btn btn-light btn-sm" href="{{ route('dashboard.page.edit', $subPage->id)}}"><span data-feather="edit-2"></span> Bearbeiten</a>
                    <form action="{{ route('dashboard.page.destroy', $subPage->id)}}" method="post" class="form-delete">
                        @csrf
                        @method('DELETE')
                        <button class="btn btn-danger btn-sm" type="submit"></span> L&ouml;schen</button>
                    </form>
                </div>
            </div>
        </div>
        @endforeach
    @endforeach

Model:

public function parent()
    {
        return $this->belongsTo('App\Page', 'parent_id');
    }

    public function sub()
    {
        return $this->hasMany('App\Page', 'parent_id');
    }

How can I achieve that when I move one page to another, it automatically enters the id as parent_id? I would like to create a sub-navigation via drag & drop

22 Mar
1 year ago

tk90 left a reply on Saving Multiple Records To Database With Many To Many Relation

Ah, i see this:

public function store(Request $request)
    {
        request()->validate([
            'title' => 'required'
        ]);

        $movie = Movie::create($request->all());
        $movie->categories()->attach($request->categories);

        Session::flash('success', 'Film gespeichert');
        return Redirect::to('/');
    }
    

works fine.

tk90 left a reply on Saving Multiple Records To Database With Many To Many Relation

Thank you. Is it possible to shorten the code like

 Movie::create($request->all());

and add the attach()?

tk90 left a reply on Saving Multiple Records To Database With Many To Many Relation

hmm i've tried this:

Store

$movie->save();
$movie->categories()->attach($request->categories);

Form:

<div class="form-group">
    {{ Form::label('categories', 'Kategorie') }}
    {{ Form::select('categories[]', $categories, null, ['class' => 'form-control', 'multiple' => true]) }}
</div>

This work's.

tk90 started a new conversation Saving Multiple Records To Database With Many To Many Relation

Hi, i would like to save multiple categories to my movies. Tables: movies: id, name etc. categories: id, name category_movie: id_category, id_movie

Category Model:

public function movies()
{
    return $this->belongsToMany(Movie::class);
}

Movie Model:

public function categories()
{
    return $this->belongsToMany(Category::class);
}

Form:

<div class="form-group">
  {{ Form::label('category_id', 'Kategorie') }}
  {{ Form::select('category_id', $categories, null, ['class' => 'form-control', 'multiple' => 'multiple']) }}
</div>

Create Method:

request()->validate([
        'title' => 'required'
]);

$movie = new Movie;
$movie->title       = $request->title;

$movie->save();
$movie->categories()->attach($request->category_id);

//Movie::create($request->all());
//return redirect()->route('/')->with('success', 'Film gespeichert');

Session::flash('success', 'Film gespeichert');
return Redirect::to('/');

It only stores one category to the database.

18 Sep
2 years ago

tk90 left a reply on Old In Select

Done :)

Changed the Code to this:

{{ Form::select('role', $role, array_pluck($user->roles, 'id'), ['class' => 'form-control'])  }}

works!

tk90 left a reply on Old In Select

I've tried it before with

{{ Form::select('role[]', $role, null, ['class' => 'form-control'])  }}

But it always shows the first entry (admin)

I think it is because of my table users [id, name, ...]; roles [id, name]; role_user [id, role_id, user_id];

Controller:

public function edit($id)
    {
        $user = User::find($id);
        $role = Role::pluck('name', 'id');
        $team = Team::pluck('name', 'name');
        return View::make('users.edit', compact('user', 'role', 'team'));
    }
13 Sep
2 years ago

tk90 left a reply on Old In Select

With this code i see all possible options. I would like to have a select field with the chosen group shown as selected.

12 Sep
2 years ago

tk90 left a reply on Old In Select

<div class="form-group">
    {{ Form::label('role', 'Gruppe') }} 
    {{ Form::select('role', $role[], null, ['class' => 'form-control']) }}
</div>

result is this error

Cannot use [] for reading (View: 
11 Sep
2 years ago

tk90 left a reply on Old In Select

Thats after the request. In the database are these tables:

  • roles = admin, user, trainee
  • role_user = id / role_id / user_id
  • team = Web etc...
{{ Form::select('role_id', $role, old('role'), ['class' => 'form-control']) }}

didn't work.

tk90 left a reply on Old In Select

{{ dd($role) }}

Collection {#261 ▼
  #items: array:3 [▼
    1 => "admin"
    2 => "user"
    3 => "trainee"
  ]
}

{{ dd(old('role')) }}

null

tk90 left a reply on Old In Select

How should i check the difference?

<div class="form-group">
    {{ Form::label('team', 'Team') }} 
    {{ Form::select('team', $team, null, ['class' => 'form-control']) }}
</div>

<div class="form-group">
    {{ Form::label('role', 'Gruppe') }} 
    {{ Form::select('role', $role, null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
    <label for="team">Team</label> 
    <select class="form-control" id="team" name="team">
        <option value="Web" selected="selected">Web</option>
        <option value="Tools">Tools</option><option value="SQL">SQL</option>
        <option value="Hotline">Hotline</option>
        <option value="Mediengestalter">Mediengestalter</option>
    </select>
</div>

<div class="form-group">
    <label for="role">Gruppe</label> 
    <select class="form-control" id="role" name="role">
        <option value="1">admin</option>
        <option value="2">user</option>
        <option value="3">trainee</option>
    </select>
</div>

Team works. Role not. I think its because role is another database table

tk90 left a reply on Old In Select

Still noone with an idea?

01 Sep
2 years ago

tk90 left a reply on Export Data To .csv

Okay ill try this: http://csv.thephpleague.com/

I have this code

        $encoder = (new CharsetConverter())->inputEncoding('utf-8')->outputEncoding('iso-8859-15');
        $exportVars = Variable::select('name', 'description', 'sql', 'win')->get();
        $csv = Writer::createFromFileObject(new \SplTempFileObject());
        $csv->addFormatter($encoder);
        foreach ($exportVars as $var) {
            $csv->insertOne($var->toArray());
        }
        $csv->output('variablelist.csv');

I would like to seperate with ; inseat of , Then i would like to wrap each column in ""

tk90 left a reply on Export Data To .csv

Yes, i would like to use pure php. When i use a package, i have to learn how it works :)

tk90 started a new conversation Export Data To .csv

Is there a simple way (without any third party extension) to export data from my table to csv? I would like to format the lines and use iso instead of utf8

tk90 left a reply on Old In Select

Does nobody have an idea?

31 Aug
2 years ago

tk90 left a reply on Old In Select

        <div class="form-group">
            {{ Form::label('team', 'Team') }}
            {{ Form::select('team', $team, null, ['class' => 'form-control']) }}
        </div>
        <div class="form-group">
            {{ Form::label('role', 'Gruppe') }}
            {{ Form::select('role', $role, null, ['class' => 'form-control']) }}
        </div>

With "Team" it works with "null". But with role not. Maybe because it is a pivot table?

tk90 started a new conversation Old In Select

I have this Code

{{ Form::select('role_id', $role, Input::old('role'), ['class' => 'form-control']) }}

Thes generates a select field like this

<select class="form-control" name="role_id">
    <option value="1">admin</option>
    <option value="2">user</option>
    <option value="3">trainee</option>
</select>

How can i use the "old" helper?

tk90 left a reply on Edit User And Change Role

This works thank you. Why increase it the id`s?

tk90 started a new conversation Edit User And Change Role

when i create a user i wrote this code:

$user = new User;
            $user->short     = $request->short;
            $user->password  =   bcrypt( $request->short);
            $user->save();
            $user->roles()->attach(Role::where('name', 'user')->first());

this gives a new user the the role_id 2 in table role_user.

How can i edit the role from a user? I've tried this:

        <div class="form-group">
            {{ Form::label('role', 'Role') }}
            {{ Form::select('role_id', $role, null, ['class' => 'form-control']) }}
        </div>
} else {
            $user = User::find($id);
            $user->name     = $request->name;
            $user->save();
            $user->roles()->attach($request->role);
        

It changes nothing in the role_user table.

I used this: https://medium.com/@ezp127/laravel-5-4-native-user-authentication-role-authorization-3dbae4049c8a

17 Aug
2 years ago

tk90 started a new conversation Is This Possible With Wordpress?

Hi, i have a question. I have a complete web application with a file with the database connection for the app as an array. Now i would like to install wordpress and create an "api". I would like to create a new admin menu in wordpress where i can set the path to the other web application. If the path is set wordpress should use the database connection from the app. Then i would like to display different results from the applikation in a widget.

Is this possible? And how should i start? It should be OOP.

PHP is no problem.

Thanks

16 Aug
2 years ago

tk90 started a new conversation Bootstrap Dropdown Navigation

I would like to build a navigation with bootstrap elements and laravel.

Pages Model:

class Page extends Model
{
    protected $table = 'pages';

    public function posts()
    {
        return $this->hasMany('App\Post')->orderBy('sorting');
    }

    public function parent()
    {
        return $this->belongsTo('App\Page', 'parent_id');
    }

    public function children()
    {
        return $this->hasMany('App\Page', 'parent_id');
    }
}

Pages Controller

public function index()
    {
        $page = Page::with('posts')->where('is_home', 1)->first();
        $navPages = Page::where('active', 1)->orderBy('sorting', 'asc')->get();
        return view('pages.index', compact('page', 'navPages'));
    }

How can i build a navigation like this:

<ul class="navbar-nav mr-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
            <div class="dropdown-menu" aria-labelledby="dropdown01">
              <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another action</a>
              <a class="dropdown-item" href="#">Something else here</a>
            </div>
          </li>
        </ul>

tk90 left a reply on Trying To Get Property Of Non-object

I changed the view to

@isset($page)
<h1>{{ $page->title }}</h1>
{!! $page->text !!}
<hr>
@foreach($page->posts as $post)
    <h1 @isset($post->title_class)class="{!! $post->title_class !!}"@endisset>{!! $post->title !!}</h1>
    <p>{!! $post->text !!}</p>
@endforeach
@endisset

@empty($page)
<h1>Es sind noch Keine Seiten vorhanden</h1>
@endempty

This works. In my navigation view i made no changes. Is it because of the first() and get()?

tk90 started a new conversation Trying To Get Property Of Non-object

I have a general question.

My Controller

public function index()
    {
        $page = Page::with('posts')->where('is_home', 1)->first();
        $navPages = Page::where('active', 1)->orderBy('sorting', 'asc')->get();
        return view('pages.index', compact('page', 'navPages'));
    }

View

<h1>{{ $page->title }}</h1>

{!! $page->text !!}

<hr>

@foreach($page->posts as $post)
    <h1 @isset($post->title_class)class="{!! $post->title_class !!}"@endisset>{!! $post->title !!}</h1>
    <p>{!! $post->text !!}</p>
@endforeach

If i have no db entry, i'll get "Trying to get property of non-object". I unterstand why but what can i do to set a default value if the db is empty?

10 Aug
2 years ago

tk90 left a reply on Relation In Single View

Sorry, im stupid -.-. Overwrited my variables...

tk90 left a reply on Relation In Single View

Okay, this works. But i want to list alle Pages in the view. Thats why i have the $pages variable with ::all().

But when i change the code that the posts are correct, the pages wont be shown.

@foreach($pages as $page => $value)
                <div class="row">
                    <div class="col-md-11">{{ $value->title }}</div>
                    <div class="col-md-1">{{ $value->sorting }}</div>
                </div>
                @endforeach

Error

Trying to get property of non-object (View: C:\wamp\www\projects\website\resources\views\admin\page\edit.blade.php)

tk90 started a new conversation Relation In Single View

hi, i woule like to show all posts from a specific page in a edit template. in the index this works:

PagesController

$pages = Page::withCount('posts')->where('active', 1)->paginate(\Config::get('settings.adminPagination'));
        return view('admin.page.index', compact('pages'));

index.blade.php

@foreach($pages as $page => $value)
    <ul class="sortable-posts">
            @foreach($value->posts as $post)
                <li class="" data-post-id="{{ $post->id }}">{!! $post->title !!}</li>
                @endforeach
    </ul>
@endforeach

This works for the posts from the Pages. In my edit Page this doesn't work

    public function edit($id)
    {
        $page = Page::find($id);
        $pages = Page::all();
        return view('admin.page.edit', compact('page', 'pages'));
    }

edit.balde.php

<ul class="sortable-posts list-unstyled">
    @foreach($pages as $page => $value)
        @foreach($value->posts as $post)
        <li class="" data-post-id="{{ $post->id }}">{!! $post->title !!}</li>
        @endforeach
    @endforeach
</ul>

The code lists all posts from the database. I need only the posts from the page wich is beeing edited.

Thanks

09 Aug
2 years ago

tk90 left a reply on Relations Won't Work

Ah okay, if it was a hasOne relation, what would i have to write in the view?

tk90 started a new conversation Relations Won't Work

Post.php

public function users()
    {
        return $this->belongsTo('App\User');
    }

User.php

public function posts()
    {
        return $this->hasMany('App\Post');
    }

PostsController

$posts = Post::with('page', 'users')->paginate(\Config::get('settings.adminPagination'));
        return view('admin.post.index', compact('posts'));

View

@foreach($posts as $post => $value)
        <div class="row post">
            <div class="col-md-3">
                    {{ $value->users->id }}
            </div>
        </div>
@endforeach

Error:

Trying to get property of non-object (View: C:\wamp\www\projects\website\resources\views\admin\post\index.blade.php)

Why can i not include the user here?

tk90 left a reply on JQuery Sortable Doesn't Work With Table?

The column "sorting" in my pages table was unique. That was the problem.

tk90 left a reply on JQuery Sortable Doesn't Work With Table?

I updated the post. Thought it's because of the ID's or the table. Now the template is build with DIV's and there is a data-attribute. But the result is the same error

tk90 started a new conversation JQuery Sortable Doesn't Work With Table?

hi, i have this Code:

index.plade.php

<div class="col-md-4">
    <div class="postOnPage">
            <ul class="sortable-posts list-group">
        @foreach($value->posts as $post)
            <li class="list-group-item" data-post-id="{{ $post->id }}">{!! $post->title !!}</li>
                @endforeach
        </ul>
    </div>
</div>

JavaScript:

$(function() {
    $(".sortable-posts").sortable({
        stop: function() {
            $.map($(this).find('li'), function(el) {
                var id = el.id;
                var sorting = $(el).index();
                $.ajax({
                    url: 'pages/sortPosts',
                    type: 'GET',
                    data: {
                        id: id,
                        sorting: sorting
                    },
                });
            });
        }
    });
});

PagesController

    public function sortPosts()
    {
        $posts = Post::all();
        $id = Input::get('id');
        $sorting = Input::get('sorting');
        foreach ($posts as $item) {
            return Post::where('id', $id)->update(array('sorting' => $sorting));
        }
    }

The sorting and storing in the database works very well. If i try the same thing with the Pages:

@foreach($pages as $page => $value)
            <div class="row" data-page-id="{{ $value->id }}">
...
                        <ul class="sortable-posts list-group">
                            @foreach($value->posts as $post)
                            <li class="list-group-item" data-post-id="{{ $post->id }}">{!! $post->title !!}</li>
                            @endforeach
                        </ul>
...
            @endforeach

$(function() {
    $(".sortable-pages").sortable({
        stop: function() {
            $.map($(this).find('div'), function(el) {
                var id = $(el).data('page-id');
                var sorting = $(el).index();
                $.ajax({
                    url: 'pages/sortPages',
                    type: 'GET',
                    data: {
                        id: id,
                        sorting: sorting
                    },
                });
            });
        }
    });
});

    public function sortPages()
    {
        $pages = Page::all();
        $id = Input::get('id');
        $sorting = Input::get('sorting');
        foreach ($pages as $item) {
            return Page::where('id', $id)->update(array('sorting' => $sorting));
        }
    }

it changes the value in the database but only once and don't know why.

I'll get the Message

jquery-3.1.1.min.js:4 GET http://website.dev/admin/pages/sortPages?id=2&sorting=0 500 (Internal Server Error)
jquery-3.1.1.min.js:4 GET http://website.dev/admin/pages/sortPages?id=1&sorting=1 500 (Internal Server Error)

tk90 left a reply on Sortable List With Change In Database

For others with the same problem, here is my solution:

Template

<ul class="sortable-posts list-unstyled">
  @foreach($value->posts as $post)
  <li id="{{ $post->id }}">{!! $post->title !!}</li>
  @endforeach
</ul>

JavaScript

$(function() {
    $(".sortable-posts").sortable({
        stop: function() {
            $.map($(this).find('li'), function(el) {
                var id = el.id;
                var sorting = $(el).index();
                $.ajax({
                    url: 'pages/sortPosts',
                    type: 'GET',
                    data: {
                        id: id,
                        sorting: sorting
                    },
                });
            });
        }
    });
});

web.php

Route::get('/admin/pages/sortPosts', 'Admin\[email protected]');

PagesController

public function sortPosts()
    {
        $posts = Post::orderBy('sorting', 'ASC')->get();
        $id = Input::get('id');
        $sorting = Input::get('sorting');
        foreach ($posts as $item) {
            return Post::where('id', '=', $id)->update(array('sorting' => $sorting));
        }
    }

Page Model (for order by)

return $this->hasMany('App\Post')->orderBy('sorting');
07 Aug
2 years ago

tk90 left a reply on Sortable List With Change In Database

Now i have this:

functions.js

$(document).ready(function () {
    $('.sortable').sortable({
        cursor: 'move',
        axis: 'y',
        update: function (event, ui) {
            var data = $(this).sortable('toArray');
            $.ajax({
                data: data,
                type: 'POST',
                url: 'pages/sortPosts'
            });
        }
    });
});

web.php

Route::post('/admin/pages/sortPosts', 'Admin\[email protected]');

Admin\PagesController

    public function sortPosts()
    {
        $input = Input::get('sorting');
        $i = 1;
        foreach ($input as $value) {
            $posts = Post::find($value);
            $posts->sorting = $i;
            $posts->save();
            $i++;
        }
    }

But i don't know what i have to write in this line:

$input = Input::get('sorting');

I'll get this Error:

500 (Internal Server Error)

tk90 left a reply on Sortable List With Change In Database

Thanks for the anwser. The "save" button wouldn't look very well in the page :) That#s why i would like to use ajax.

I tought something like this:

functions.js

$('.sortable').sortable({
    update: function (event, ui) {
        var data = $(this).sortable('serialize');
        $.ajax({
            data: data,
            type: 'POST',
            url: 'pages/store'
        });
    }
});

web.php

Route::post('/admin/pages/sort', 'Admin\[email protected]');

Admin\PagesController

public function sortPosts(Request $request)
{
        foreach ($request->post as $order => $post_id) {
            Post::find($post_id)->update(['sorting' => $order]);
        }
}

index.blade.php

<ul class="sortable list-unstyled">
    @foreach($value->posts as $post)
    <li id="post-{{ $post->id }}">{!! $post->title !!}</li>
    @endforeach
</ul>

But i get the Message:

POST http://localhost/projects/website/public/admin/pages/store 405 (Method Not Allowed)

tk90 left a reply on Sortable List With Change In Database

now i have this method in my controller

    public function sort(Request $request)
    {
        foreach ($request->post as $order => $post_id) {
            echo 'Post id '.$post_id.' has the new order of '.$order.'<br>';
            Post::find($post_id)->update(['sorting' => $order]);
        }
    }

the JavaScript (https://jqueryui.com/sortable/):

$(function() {
    $(".sortable").sortable();
    $(".sortable").disableSelection();
});

in the view:

<ul class="sortable list-unstyled">
   @foreach($value->posts as $post)
       <li id="post-{{ $post->id }}">{!! $post->title !!}</li>
    @endforeach
</ul>

sorting works but i don't know how to realise this in ajax. i think i have to give the value to a route that listen to the sort method?

04 Aug
2 years ago

tk90 started a new conversation Sortable List With Change In Database

Hi, would like to sort the posts to a page with jquery and save the sorting to the database. this ist what i have:

index.blade.php

@foreach($pages as $page => $value)
<tr>
    <td>{{ $value->id }}</td>
    <td>
        {{ $value->title }}
        <div class="postOnPage">
            <strong>Posts</strong>
            <ul id="sortable" class="list-unstyled">
                @foreach($value->posts as $post)
                <li>{!! $post->title !!}</li>
                @endforeach
            </ul>
        </div>
    </td>
    <td>{{ $value->slug }}</td>
    <td>{{ $value->active }}</td>
    <td>{{ $value->sorting }}</td>
    <td>
        <a class="btn btn-sm btn-info pull-left" href="{{ URL::to('admin/pages/'. $value->id .'/edit/') }}"><i class="fa fa-pencil" aria-hidden="true"></i></a> {{ Form::open(array('url' => 'admin/pages/' . $value->id, 'class' => '')) }} {{ Form::hidden('_method',
        'DELETE') }}
        <button type="submit" class="btn btn-sm btn btn-danger"><i class="fa fa-trash-o" aria-hidden="true"></i></button> {{ Form::close() }}
    </td>
</tr>
@endforeach

In the List of the pages i can see the posts from this page

functions.js

$(function() {
    $("#sortable").sortable();
    $("#sortable").disableSelection();
});

I can sort the posts. How can i change the sorting of the posts? In the database i have the column "sorting" in the posts table.

i don't know how to describe it better, sorry for that

thanks

27 Jul
2 years ago

tk90 left a reply on Relation Again

Okay, now i want to output the user.

User Model:

public function posts()
    {
        return $this->hasMany('App\Post');
    }

Posts Model:

public function user()
    {
        return $this->belongsTo('App\User');
    }

is this possible?

Controller:

$posts = Post::with('page', 'user')->paginate(\Config::get('settings.adminPagination'))

View:

<td>{{ $post->user->name }}</td>

tk90 left a reply on Relation Again

That works. Is ist because a post can only be in one page?

posts != post?

tk90 left a reply on Relation Again

Correct, a Page have multiple posts but a post only one page.

I chaged the Post Model:

class Post extends Model
{
    protected $table = 'posts';

    public function pages()
    {
        return $this->belongsTo('App\Page');
    }
}

i would like to list all the posts. that works fine and in the table column "id" i would like to show the page title from table pages not the page_id from table posts

tk90 started a new conversation Relation Again

Hi, i would like to get the name from the page with the id stored in the database column page_uid in table posts:

Post Model

    protected $table = 'posts';

    public function page()
    {
        return $this->belongsTo('App\Page');
    }

Page Model

    rotected $table = 'pages';

    public function posts()
    {
        return $this->hasMany('App\Post');
    }

PostsController

    public function index()
    {
        $posts = Post::paginate(\Config::get('settings.adminPagination'));
        $pages = Post::with('pages')->whereId('page_id')->first();
        return view('admin.posts.index', compact('posts', 'pages'));
    }

Index.blade.php

            @foreach($posts as $post)
                <tr>
                    <td>{{ $post->id }}</td>
                    <td>{{ $post->page_id }}</td>
                    <td>{{ $post->author_id }}</td>
                    <td>{{ $post->title }}</td>
                    <td>{{ $post->slug }}</td>
                    <td>{{ str_limit($post->text, $limit = 50, $end = '...') }}</td>
                    <td></td>
                </tr>
            @endforeach


            @foreach($pages->pages as $page)
                <p>
                    {{ $page->title }}
                </p>
            @endforeach

Error:

Trying to get property of non-object (View: C:\wamp\www\projects\t3\resources\views\admin\posts\index.blade.php)

What im doing wrong?

Thank you!

tk90 left a reply on (1/1) MethodNotAllowedHttpException

This worked! But why? Until 1 or 2 hours ago the code worked now suddenly not.

tk90 started a new conversation (1/1) MethodNotAllowedHttpException

hi, i don't know why this error shows up.

web.php

Route::get('/', '[email protected]');
Route::get('/admin', 'Admin\AdminControll[email protected]');
Route::get('/{slug}', '[email protected]');
Route::resource('/admin/pages', 'Admin\PagesController');
Route::resource('/admin/posts', 'Admin\PostsController');

Controllers\Admin\PagesController

    public function create()
    {
        return view('admin.page.create');
    }


    public function store()
    {
        $rules = array(
            'title' => 'required'
        );

        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) {
            return Redirect::to('admin/pages/create')
                ->withErrors($validator)
                ->withInput(Input::all());
        } else {
            $page = new Page;
            $page->title    = Input::get('title');
            $page->slug     = str_slug(Input::get('title'));
            $page->sorting  = Input::get('sorting');
            $page->text     = Input::get('text');
            $page->active   = Input::get('active');
            $page->save();

            Session::flash('message', \Config::get('settings.createPageSuccess'));
            return Redirect::to(\Config::get('settings.redirectToPages'));
        }
    }

admin\page\create.php

@if (Session::has('message'))
    <div class="alert alert-info">{{ Session::get('message') }}</div>
@endif

{{ Html::ul($errors->all()) }}

{{ Form::open() }}

    <div class="form-group">
        {{ Form::label('title', 'Seitenname') }}
        {{ Form::text('title', Input::old('title'), array('class' => 'form-control')) }}
    </div>

    <div class="form-group">
        {{ Form::label('text', 'Seiteninhalt') }}
        {{ Form::textarea('text', Input::old('text'), array('class' => 'form-control ckeditor')) }}
    </div>

    <div class="form-group">
        {{ Form::label('sorting', 'Sortierung') }}
        {{ Form::number('sorting', Input::old('sorting'), array('class' => 'form-control')) }}
    </div>

    <div class="form-group">
        {{ Form::label('active', 'Aktiv') }}
        {{ Form::checkbox('active', '1', Input::old('active')) }}
    </div>

    {{ Form::submit('Seite erstellen', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

Can someone explain?

Thanks

26 Jul
2 years ago

tk90 left a reply on Form For Update

Change

{{ Form::model($page, array('route' => array('admin.pages.update', $page->slug), 'method' => 'PUT')) }}

to

{{ Form::model($page, array('route' => array('pages.update', $page->slug), 'method' => 'PUT')) }}

works.

tk90 started a new conversation Form For Update

web.php

Route::resource('/admin/pages', 'Admin\PagesController');

Admin/PagesController.php

public function edit($slug)
{
    $page = Page::first();
    return view('admin.page.edit', compact('page'));
}

edit.blade.php

{{ Form::model($page, array('route' => array('admin.pages.update', $page->slug), 'method' => 'PUT')) }}

Error:

Route [admin.pages.destroy] not defined.

What is the right Way to define? I tought that "Route::resource" deliver the right Methods?

Thanks!

tk90 left a reply on Resource Routing And "Method [show] Does Not Exist."

I'll get it. In my AdminController was no show Method. -.-

tk90 started a new conversation Resource Routing And "Method [show] Does Not Exist."

In my web.php is this Code:

Route::resource('/', 'PagesController');
Route::resource('admin', 'Admin\AdminController');
Route::resource('admin.pages', 'Admin\PagesController');
Route::resource('admin.posts', 'Admin\PostsController');

i have the Folder Controllers/Admin with the files

AdminController.php
PagesController.php
PostsController.php

if i call "/admin/pages" i get the Error: "Method [show] does not exist."

in my Controller "Admin/PagesController" is:

public function show($slug){ }

What im doing wrong?

Thanks

25 Jul
2 years ago

tk90 left a reply on Check If Active

Great, thanks this works fine

tk90 started a new conversation Check If Active

Hi, i have thes situation:

app.blade.php

...
<nav id="navigation">
  @include('pages.navigation')
</nav>
...

navigation.blade.php

...
<ul class="navbar-nav mr-auto">
  @foreach($navPages as $pages)
    <li class="nav-item">
      <a class="nav-link" href="{{ url($pages->slug) }}">{{ $pages->title }}</a>
    </li>
   @endforeach
</ul>
...

PageController.php

...
public function index()
{
  $page = Page::with('posts')->whereSlug('home')->first();
  $navPages = Page::where('active', 1)->orderBy('sorting', 'asc')->get();
  return view('pages.index', compact('page', 'navPages'));
}
...

This works but how can i check if the current menu is active?

I think something like "if GET xy = SLUG add class="active""