Code Monkey home page Code Monkey logo

Comments (8)

mohsin avatar mohsin commented on July 25, 2024

+1 same issue. Is there a proper solution for this other than adding the ID to the validation rules

from uniquewith-validator.

felixkiss avatar felixkiss commented on July 25, 2024

How is your validation called? Do you use a model validation package or roll your own code?

from uniquewith-validator.

mohsin avatar mohsin commented on July 25, 2024

I’m using model validation

    public $rules = [
        'user' => 'required',
        'establishment'  => 'required',
        'user_id' => 'unique_with:mohsin_rating_reviews, establishment_id'
    ];

In my app, this was added to the Review model and the logic here is that an establishment can have only one review from a particular user. Both user and establishment are belongsTo Eloquent relationships to the Review model so review model has foreign keys with their IDs.

from uniquewith-validator.

felixkiss avatar felixkiss commented on July 25, 2024

Right. But what package are you using to get the model validation functionality? This is important to know how to tackle your problem.

from uniquewith-validator.

mohsin avatar mohsin commented on July 25, 2024

I’m not entirely sure what specifically you are asking for. I use the one that comes with Laravel 5

from uniquewith-validator.

felixkiss avatar felixkiss commented on July 25, 2024

I am not aware of a model validation feature provided by Laravel out-of-the-box. How does your Model look? Does it extend Illuminate\Database\Eloquent\Model directly?

from uniquewith-validator.

mohsin avatar mohsin commented on July 25, 2024

My model looks like this...

<?php namespace Mohsin\Rating\Models;

use Model;

/**
 * Review Model
 */
class Review extends Model
{
    use \October\Rain\Database\Traits\Validation;

    /**
     * @var string The database table used by the model.
     */
    public $table = 'mohsin_rating_reviews';

    /**
     * @var array Guarded fields
     */
    protected $guarded = [];

    /**
     * @var array Jsonable fields
     */
    protected $jsonable = ['rating'];

    /**
     * @var array Relations
     */
    public $belongsTo = [
      'user' => ['RainLab\User\Models\User'],
      'establishment' => ['Mohsin\Establishments\Models\Establishment']
    ];

    public $rules = [
        'user' => 'required',
        'establishment'  => 'required',
        'user_id' => 'unique_with:mohsin_rating_reviews, establishment_id'
    ];

}

Yes, it extends Illuminate\Database\Eloquent\Model but not directly. I’m using this as a part of a custom plugin for OctoberCMS so that extends Laravel 5’s model class and my Review model basically extends that. The validation code above is based on this documentation

from uniquewith-validator.

felixkiss avatar felixkiss commented on July 25, 2024

Closing because of inactivity

from uniquewith-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.