Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Osamu Fujimoto
Osamu Fujimoto
45,636 Points

I am stuck in the "Accessing our Relationships" of the course Laravel Basics

I am following the course with Laravel 5.

I got a error "BadMethodCallException in Builder.php line 2024: Call to undefined method Illuminate\Database\Query\Builder::listItems()"

I created the Model "TodoItem" as the course goes. I placed the "use App\TodoItem;" in TodoListController.php.

I can't solve this error. please help me figure it out.

Konnichiwa,

Can you please post some code? :)

Osamu Fujimoto
Osamu Fujimoto
45,636 Points

Hello.

It's TodoItem.php in app folder

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TodoItem extends Model { public function todoList() { return $this->belongTo("App\TodoList"); } }

It's TodoListController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests; use App\TodoList; use App\TodoItem; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Validator;

class TodoListController extends Controller { /** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { $list = TodoList::findOrFail($id); $items = $list->listItems()->get(); return $items; return view("todos.show")->withList($list); }

It's my codes. Could you find the problems in my codes?

4 Answers

Hello,

Can you post the contents of your TodoList model? Call to undefined method usually means you are trying to use a function that you have not yet created.

It looks like your TodoList model should have a function as such:

public function listItems()
{
    return $this->hasMany('App\ToDoItem');
}
Osamu Fujimoto
Osamu Fujimoto
45,636 Points

I just didn't typed as it should be!!

Osamu Fujimoto
Osamu Fujimoto
45,636 Points

Now I am moving to Nested CRUD with Laravel.

If you can, can you help me solve a problem?

I got a error "InvalidArgumentException in Response.php line 470: The HTTP status code "1" is not valid." in the "Nested Updates". When I update item in " TodoItems".

even though I got a error, todo_items was updated well. I don't know why I got a error. it's my code "TodoItemController.php"

public function update(Request $request, $list_id, $item_id)
{
    $rules = array(
        "content" => array("required")
        );

    $validator = Validator::make($request->all(), $rules);

    if($validator->fails()) {
        return  redirect("todos/items/edit", [$list_id, $item_id])->withErrors($validator)->withInput();
    }

    $item = TodoItem::findOrFail($item_id);
    $item->content = $request->input("content");
    $item->Update();
    return redirect("todos.show", [$list_id])
        ->withMessage("item was updateed");
}

Sure I would love to. If you could please make a new question (that way someone else can potentially find the answer in the future if they need it) with your controller method and the route entry that points to this controller.

I have an idea about what the issue is but I would like to see the route first.

Osamu Fujimoto
Osamu Fujimoto
45,636 Points

Hello. Sorry for my late response!!!

I could solve this iusse just by finding my spelling mistakes. And I could finish the laravel course by your help!! Tnank you very much!!

You're very welcome and congratulations!