2017-06-20 43 views
1

使用Laravel 5.2。Laravel - 爲不同用戶重複輸入具有獨特參數

我正在Laravel開發一個電話簿項目,在那裏您將聯繫信息存儲在名爲Contacts的表中。 爲了在這個表中創建新的聯繫人,您已註冊,並且您的信息將被記錄在一個users

我創建的視圖來顯示Contacts表,並在控制器我想出了一個邏輯,只顯示您在表格中創建的聯繫人。所以,這意味着你無法看到表中另一個用戶創建的聯繫人。

這種邏輯是爲了防止用戶失去其聯繫人的蹤跡。例如,作爲一個用戶,我只創建了2個聯繫人,但是如果我的表有超過500個條目,我將不得不在表格中深入查找我的聯繫人。

但是聯繫人,特別是emailphone條目必須是唯一的,所以我們在表中沒有重複的信息。 這就產生了一個問題,因爲我看不到其他用戶在此表中創建的聯繫信息。因此,讓我們說,我和我的兄弟想要存儲我妹妹的聯繫人,只有我們其中一人能夠創建此信息,只有創建它的人才能看到此信息。

這裏是我的問題

如何「授權」相同的接觸,以示對未不失unique參數創建它的用戶?

下面是一些我的代碼(包括一些澄清):

用戶遷移

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class PhonebookUsers extends Migration 
{ 
    protected $primaryKey = 'name'; 
    public $incrementing = false; 

    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->integer('id')->unsigned()->autoIncrement(); 
      $table->string('user_image'); 
      $table->string('name'); 
      $table->string('email', 191)->unique(); 
      $table->string('password', 191); 
      $table->timestamp('updated_at'); 
      $table->timestamp('created_at'); 
      $table->rememberToken(); 
     }); 
    } 

用戶驗證參數

... 

public function rules() 
    { 
     return [ 
      'user_image' => 'file', 
      'name'  => 'required|alpha|min:3|max:255', 
      'email'  => 'required|unique:users,email|email', 
      'password' => 'required|alpha_num|between:6,100', 
     ]; 
    } 

... 

聯繫人遷移

...

public function up() 
    { 
     Schema::create('contacts', function (Blueprint $table) { 
      $table->integer('id')->unsigned()->autoIncrement(); 
      $table->integer('user_id')->unsigned(); 
      $table->foreign('user_id')->references('id')->on('users'); 
      $table->string('image'); 
      $table->string('name'); 
      $table->string('lastname'); 
      $table->string('email', 191); 
      $table->string('phone',191); 
      $table->string('address'); 
      $table->string('description', 255); 
     }); 
    } 

... 

注意遷移包括列,user_id,識別,通過ID,誰創造了一個接觸用戶。

聯繫人驗證參數

... 

public function rules() 
    { 
     return [ 
      'image'  => 'file', 
      'name'  => 'alpha|min:3|max:15|required', 
      'lastname' => 'alpha|min:3|max:15', 
      'email'  => 'required|email|unique:contacts,email', 
      'phone'  => 'required|alpha_num|between:3,25|unique:contacts,phone', 
      'address'  => 'max:255', 
      'description' => 'max:255' 
     ]; 
    } 

... 

ContactsController

<?php 

namespace App\Http\Controllers; 

use App; 
use App\Contact as Contact; 
use App\User as User; 
use DB; 
use Illuminate\Http\Request; 
use App\Http\Controllers\Auth; 

use App\Http\Requests\CreateContactRequest; 
use App\Http\Requests\StoreContactRequest; 

use Illuminate\Support\Facades\Input; 
use Validator; 

class ContactsController extends Controller 
{ 
    // showing the form in the phonebook.blade.php 
    // the form is structured using html and stuff 
    public function index() 
    { 
     return view('phonebook'); 
    } 

    // showing the table where I save my contacts 
    // simple view stuff 
    public function tab() 
    { 
     if(\Auth::check()) 
     { 
      $contacts = App\Contact::where('user_id', \Auth::user()->id)->get(); 
      return view('contacts', compact('contacts')); 
     } 
     else 
     { 
      return view('contacts', compact('contacts')); 
     } 
    } 

public function create(CreateContactRequest $request) 
    { 
     $contact    = new Contact;    
     $contact-> name   = $request->get('name'); 
     $contact-> lastname  = $request->get('lastname'); 
     $contact-> email  = $request->get('email'); 
     $contact-> phone  = $request->get('phone'); 
     $contact-> address  = $request->get('address'); 
     $contact-> description = $request->get('description'); 
     $contact-> user_id  = \Auth::user()->id; 

     // Checking if input has a file 
     if ($request->hasFile('image')) 
     {    
      // It does, so set name to a random string with 10 chars 
      $fileName = str_random(10); 

      // Get the extension of the file (.jpg, .png...) 
      $fileName .= '.' . $request->file('image')->getClientOriginalExtension(); 

      // Move the file to the storage 
      $request->file('image')->move(storage_path('app/public/uploads'), $fileName);  

      // Attribute the contact to the image and then to the name of the file 
      // one thing "pulling" another 
      $contact->image = $fileName;  
     } 
     // new update: echo the same contacts for different users 
     // so, the $contact->save(); line would be an else for 
     // really unique contacts 
     $contact->save(); 

     $contacts = App\Contact::where('user_id', \Auth::user()->id)->get(); 
     return view('contacts', compact('contacts')); 
    } 

    // editing a contact, taking all the info that is in my contacts table according to the id in that row  
    // permeating an edit form similar to the one in the main form using the edit.blade.php 
    public function edit($id) 
    { 
     $contact = Contact::where('user_id', \Auth::user()->id)->findOrFail($id);   

     return view('edit', compact('contact'));  
    } 

    // Create the update request for the table 
    public function update($id) 
    { 
     $contact    = Contact::findOrFail($id); 
     $contact -> name  = Input::get('name'); 
     $contact -> lastname = Input::get('lastname'); 
     $contact -> email  = Input::get('email'); 
     $contact -> phone  = Input::get('phone'); 
     $contact -> address  = Input::get('address'); 
     $contact -> description = Input::get('description'); 

     $contact -> save(); 

     $contacts = App\Contact::where('user_id', \Auth::user()->id)->get(); 
     return view('contacts', compact('contacts')); 
    } 

    // deleting an input in my table, called with a link, going to delete.blade.php. 
    // the function is called when loading the delete.blade.php 
    public function destroy($id) 
    { 
     // finding the id of my contact and deleting 
     // everything under this id which means 
     // my entire contact info 
     $contact = Contact::where('user_id', \Auth::user()->id)->findOrFail($id); 
     $contact->delete(); 

     // this will return the delete view, which is the confirmation that the code works 
     return view('delete'); 
    } 

    // tests about uploading photos. create a button or link in the table to redirect to the upload page  
    // select a pic using an input form, then use the move method to store my pic in a directory 
    // but let's begin with simplicity let's just go to the form that'll request the photo 
    public function upload($id) 
    { 
     // simply return the view upload.blade.php that has the form to input my pic 
     $contact = Contact::where('user_id', \Auth::user()->id)->findOrFail($id); 
     return view('upload', compact('contact')); 
    } 

    // moving the file to a dir 
    public function move(Request $request, $id) 
    { 
     // check if the input has a file 
     if ($request->hasFile('image')) { 

      // if it does create a name for the file, a random string with 10 characters (default 40)    
      $fileName = str_random(10); 

      // take this name we just created add the file's extension using the original extension    
      //adiciona extensão da imagem no nome do arquivo 
      $fileName .= '.' . $request->file('image')->getClientOriginalExtension(); 

      // save image to the right diretory 
      // Salva a imagem no diretorio selecionado 
      $request->file('image')->move(storage_path('app/public/uploads'), $fileName);  

      // Attribute the contact, using its id, to the image uploaded and then to the name of the file 
      // like in the create method, one thing pulls the other 
      $contact  = Contact::findOrFail($id); 
      $contact->image = $fileName; 

      // then save the contact with everything attributed 
      $contact->save();   
     } 

     $contacts = App\Contact::where('user_id', \Auth::user()->id)->get(); 
     // finally return the view 
     return view('contacts', compact('contacts')); 
    } 
} 

在此先感謝。

+0

如果你有這樣的要求,那麼你必須做手工的方法來檢查這些規則 –

回答

3

您可以添加額外的where子句獨特的驗證如下

'email' => Rule::unique('users')->where(function ($query) { 
    $query->where('user_id', Auth::user()->id); 
}) 

不要忘記提及 使用照亮\驗證\規則;

,或者嘗試

雖然插入

'email' => 'unique:users,email,NULL,id,user_id,'.Auth::user()->id, 

雖然更新

'email' => 'unique:users,email,{id of object to edit},id,user_id,'.Auth::user()->id, 

在上面的規則,只能用)驗證的USER_ID :: ID(行會被納入獨特的檢查。

+0

它不工作。即使當我在config/app中聲明類時,也要創建別名和全部。我讀過Laravel棄用它。 –

+0

你正在使用哪個laravel版本? –

+0

5.2.45。我認爲這個功能是在5.3中引入的,並且不像我之前所說的那樣被棄用。 –

相關問題