使用Laravel 5.2。
我正在Laravel开发一个电话簿项目,您可以在其中将联系信息存储在名为Contacts
的表中。
要在此表格中创建新联系人,您必须进行注册,并且您的信息将记录在users
表中
我创建了一个显示Contacts
表的视图,在Controller中我想出了一个逻辑,只显示您在表中创建的联系人。因此,这意味着您无法看到其他用户在表格中创建的联系人。
此逻辑用于防止用户失去对其联系人的跟踪。例如,作为一个用户,我只创建了2个联系人,但如果我的表有超过500个条目,我将不得不深入挖掘表格以找到我的联系人。
但是联系人,特别是email
和phone
条目必须是唯一的,因此我们在表格中没有重复的信息。
这会产生一个问题,因为我看不到其他用户在此表中创建的联系信息。所以,让我说我和我的兄弟希望存储我姐姐的联系,只有我们中的一个人能够创建这个信息,只有创建它的人才能看到这个信息。
这是我的问题
如何在不丢失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);
})
不要忘记提及
使用Illuminate \ Validation \ Rule;
或尝试
插入
'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,
在上面的规则中,只有具有Auth :: id()的user_id的行才会包含在唯一检查中。