Php 如果条件为唯一标题名称,则在laravel中创建新名称

Php 如果条件为唯一标题名称,则在laravel中创建新名称,php,laravel,laravel-5,Php,Laravel,Laravel 5,我正在使用laravel,如果控制器中存在条件,我想使用它 如果我的表中已经存在产品标题,那么获取产品id 否则创建新标题并获取产品id 这是我的桌子 $table->increments('id'); $table->timestamps(); $table->string('image'); $table->string('stock'); $table->string('title');

我正在使用laravel,如果控制器中存在条件,我想使用它
如果我的表中已经存在产品标题,那么获取产品id 否则创建新标题并获取产品id 这是我的桌子

 $table->increments('id');
        $table->timestamps();
        $table->string('image');
        $table->string('stock');
        $table->string('title');
        $table->string('slug')->unique();
        $table->string('gender');
        $table->text('description');
        $table->integer('price');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')
                    ->onDelete('restrict')
                    ->onUpdate('restrict');
        $table->integer('designer_id')->unsigned();
        $table->foreign('designer_id')->references('id')->on('designers')
                    ->onDelete('restrict')
                    ->onUpdate('restrict');
        $table->dateTime('published_at');

我相信这个问题已经得到了回答


提供一些方法,例如
first或create
first或new

此答案是否解决了您的问题,请标记为解决方案。这可以帮助其他人。这看起来是正确的答案,但我从来没有这样做过,所以让我先做,如果它正确,我将标记为sollution,但如何保存或需要保存()类以进行保存this@SidHeart,请再问一个问题
$validator = Validator::make(['title'=>$title], ['title' => 'string|unique:my_tbl']);
if ($validator->fails()) {
    return Response::json($validator->errors());
} else {
    $title = 'new Title';
    //Your Code
}