Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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
在laravel中将对象保存到数据库时,如何检查唯一密钥?_Laravel_Laravel 4_Eloquent - Fatal编程技术网

在laravel中将对象保存到数据库时,如何检查唯一密钥?

在laravel中将对象保存到数据库时,如何检查唯一密钥?,laravel,laravel-4,eloquent,Laravel,Laravel 4,Eloquent,我正在尝试保存到数据库,如果日志已经在数据库中,我希望忽略它。这就是我目前保存它的方式。posted必须是唯一的 公共函数保存$type、$postid、$url、$author、$content { $post=新员额; $post->type=$type; $post->postid=$postid; $post->url=$url; $post->author=$author; $post->content=$content; echo$post; $post->save; } 您可以使用

我正在尝试保存到数据库,如果日志已经在数据库中,我希望忽略它。这就是我目前保存它的方式。posted必须是唯一的

公共函数保存$type、$postid、$url、$author、$content { $post=新员额; $post->type=$type; $post->postid=$postid; $post->url=$url; $post->author=$author; $post->content=$content; echo$post; $post->save; } 您可以使用Post::findOrFailUNIQUE\u ID来尝试使用它。。。接住如果一篇文章已经有了您想要设置的唯一ID,则返回结构。

您可以使用post::findOrFailUNIQUE\u ID尝试使用它。。。接住如果一篇文章已经有了您想要设置的唯一ID,则返回结构。

在尝试保存之前,在您想要唯一的任何字段上使用Validator类的唯一验证规则

$input = array('postid' => $postid);
$rules = array(
    'postid' => 'unique:posts,postid'
);

$validate = Validator::make($input,$rules);

if($validator->passes())
{
    $this->save($type, $postid, $url, $author, $content)
} 
else 
{
    $messages = $validator->messages();
    echo $messages->first('postid');
}
在尝试保存之前,对任何希望唯一的字段使用Validator类的唯一验证规则

$input = array('postid' => $postid);
$rules = array(
    'postid' => 'unique:posts,postid'
);

$validate = Validator::make($input,$rules);

if($validator->passes())
{
    $this->save($type, $postid, $url, $author, $content)
} 
else 
{
    $messages = $validator->messages();
    echo $messages->first('postid');
}

您可以在验证中检查唯一性。我不确定您是否正在检查唯一的主postid。我建议您保持postid作为主键&自动递增。您可以匹配文章标题进行复制

$rules = array(
    'postid' => 'required|unique:posts'
);

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

if($validator->fails()){
    return Redirect::back()->withInput()->with('errors', $validator->messages());
}else{
    $data = array(
        'type' => Input::get('type'),
        'postid' => Input::get('postid'),
        'url' => Input::get('url'),
        'author' => Input::get('author'),
        'content' => Input::get('content')
    );

    if(Post::create($data)){
        Session::flash('messages', 'Post created.');
        return Redirect::route('routename');
    }
}

您可以在验证中检查唯一性。我不确定您是否正在检查唯一的主postid。我建议您保持postid作为主键&自动递增。您可以匹配文章标题进行复制

$rules = array(
    'postid' => 'required|unique:posts'
);

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

if($validator->fails()){
    return Redirect::back()->withInput()->with('errors', $validator->messages());
}else{
    $data = array(
        'type' => Input::get('type'),
        'postid' => Input::get('postid'),
        'url' => Input::get('url'),
        'author' => Input::get('author'),
        'content' => Input::get('content')
    );

    if(Post::create($data)){
        Session::flash('messages', 'Post created.');
        return Redirect::route('routename');
    }
}

我确实尝试{Post::findOrFail$postid;}捕获。。。它似乎每次都会触发捕获,即使$postid是唯一的。。。它似乎每次都会触发捕获,即使$postid是唯一的。我试图编辑答案,但有一个可笑的6个字符的配额。注意$validate应该是$validator。我试图编辑答案,但有一个可笑的6个字符的配额。