Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel 4中雄辩ORM的错误_Php_Laravel_Laravel 4_Eloquent - Fatal编程技术网

Php laravel 4中雄辩ORM的错误

Php laravel 4中雄辩ORM的错误,php,laravel,laravel-4,eloquent,Php,Laravel,Laravel 4,Eloquent,我目前正在学习laravel,并尝试遵循本教程。但是我使用的是Laravel4,教程使用的是Laravel3,因此我应该修复很多东西。 我在控制器上有这个方法 public function InsertTestData() { $logged_in_user = Auth::user(); $photos = array( array( 'user_id' => $logged_in_user->id,

我目前正在学习laravel,并尝试遵循本教程。但是我使用的是Laravel4,教程使用的是Laravel3,因此我应该修复很多东西。 我在控制器上有这个方法

public function InsertTestData()
{   
    $logged_in_user = Auth::user(); 

    $photos = array(
        array(
            'user_id' => $logged_in_user->id,
            'location' => 'http://farm6.staticflickr.com/5044/5319042359_68fb1f91b4.jpg',
            'description' => 'Dusty Memories, The Girl in the Black Beret (http://www.flickr.com/photos/cloudy-day/)'
        ),
        array(
            'user_id' => $logged_in_user->id,
            'location' => 'http://farm3.staticflickr.com/2354/2180198946_a7889e3d5c.jpg',
            'description' => 'Rascals, Tannenberg (http://www.flickr.com/photos/tannenberg/)'
        ),
        array(
            'user_id' => $logged_in_user->id,
            'location' => 'http://farm7.staticflickr.com/6139/5922361568_85628771cd.jpg',
            'description' => 'Sunset, Funset, Nikko Bautista (http://www.flickr.com/photos/nikkobautista/)'
        )
    );
    $logged_in_user->photos()->save($photos);
}
这就是我使用的模型

class User extends Eloquent implements UserInterface, RemindableInterface {
    public function photos()
    {
    return $this->hasMany('Photo');
    }
}
它像这样重复错误

Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, array given, called in /Library/WebServer/Documents/webpraktek/app/controllers/DashboardController.php on line 44 and defined
第44行是这一行吗

$logged_in_user->photos()->save($photos);
这是班级照的模型

class Photo extends Eloquent
{
    public function user()
    {
        return $this->belongsTo('User');
    }

    public function photocomments()
    {
        return $this->hasMany('PhotoComment');
    }
}
我不知道如何解决这个问题,因为我还在学习。为什么会这样?laravel版本中的不同是否会导致此类错误? 谢谢

编辑:我现在用这个修复了它

foreach($photos as $photo) {
    $photos = new Photo();
    $photos->user_id = $photo['user_id'];
    $photos->location = $photo['location'];
    $photos->description = $photo['description'];
    $photos->save();
}

据我所知,您的示例不起作用,因为您没有创建新的照片模型

尝试替换此:

$logged_in_user->photos()->save($photos);
与:


您不需要使用
$logged\u in\u user
进行关联,因为您已经在数组中设置了ID,所以只需使用
create

Photo::create($photos);

你有一张班级照片吗(这张照片很有说服力)。所以我可以确切地看出你的错误,我可以帮你。这是一系列教程,这是第二部分,如果你有时间,请看:)我应该如何修复我的照片模型?与此同时,我就这样把它修好了你能说得更具体些吗?你需要修理什么?
Photo::create($photos);