laravel collection unique code example

Example 1: collection laravel filter

$collection = collect([1, 2, 3, 4]);

$filtered = $collection->filter(function ($value, $key) {
    return $value > 2;
});

$filtered->all();

// [3, 4]

Example 2: add to collection laravel

$item = collect();
$item->push($product);

Example 3: laravel unique validation

/**
 * Store a new blog post.
 *
 * @param  Request  $request
 * @return Response
 */

public function store(Request $request)
{
    $validatedData = $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    // The blog post is valid...
}

Example 4: laravel rule unique where

'required|email|unique:company_users,email_address,NULL,id,company_id,' . $request->company_id

Example 5: laravel unique column except self

'required|unique:users,email,'.$this->id . ',user_id';

Tags:

Php Example