如何解决Laravel 我不能只显示通过 user_email 检查的用户创建的对象,就像每个用户都不同的购物车
我正在尝试创建一个特定的在线地址簿,每个用户都可以在其中注册并创建他的联系人。
创建联系人时,创建它的用户的“user_email”和“session_id”保存在“students”选项卡中。
This is the structure of the users table
This is the structure of the students table
这是“StudetsController”中存在的函数“addStudent”(不要注意“ICF Dropdown”)
<?PHP
namespace App\Http\Controllers;
use App\Student;
use App\Product;
use Session;
use User;
use Auth;
use DB;
use Illuminate\Http\Request;
use function GuzzleHttp\Promise\all;
class StudentsController extends Controller
{
public function addStudent(Request $request){
if($request->isMethod('post')){
$data = $request->all();
$student = new Student();
$student->student_sex = $data['student_sex'];
$student->student_name = $data['student_name'];
$student->student_surname = $data['student_surname'];
$student->student_birth = $data['student_birth'];
$student->codice_icf = $data['codice_icf'];
if(!empty($data['description'])){
$student->description = $data['description'];
}else{
$student->description = '';
}
$student->user_email = $data['user_email']=Auth::user()->email;
if(empty($session_id)) {
$student->session_id = $data['session_id']= str_random(40);
}else{
$student->session_id = Student::get('session_id');
}
$student->save();
return redirect('/')->with('flash_message_success','Lo studente è stato creato con successo!');
}
//ICF drop down start
$products = Product::get();
$products_dropdown = "<option selected disabled>Codice ICF</option>";
foreach($products as $pro){
$products_dropdown .= "<option value='".$pro->product_name."'>".$pro->product_name."</option>";
}
//Categories drop down ends
return view('students.add_student')->with(compact('products_dropdown'));
}
As you can see the students are created and they save the email of who created them
首先,我在 index.blade.PHP 中为学生创建了一个 foreach 循环
@foreach($students as $student)
<!-- Item -->
<div class="item">
<!-- Images -->
@if($student->student_sex == "M")
<img class="img-holder" src="{{asset('images/frontend_images/studente-M.jpg')}}" alt="" >
@else
<img class="img-holder" src="{{asset('images/frontend_images/studente-F.jpg')}}" alt="" >
@endif
<!-- Overlay -->
<!-- Item Name -->
<div class="item-name"> <a href="#.">{{$student->student_name}} {{$student->student_surname}}</a>
<p>{{$student->codice_icf}}</p>
</div>
</div>
@endforeach
“$students”在“IndexController”中的定义是这样的
<?PHP
namespace App\Http\Controllers;
use App\Category;
use App\Product;
use App\Student;
use Session;
use Illuminate\Http\Request;
class IndexController extends Controller
{
public function index(){
$students = Student::get();
return view('index')->with(compact('students'));
}
}
“StudentsController”中的 $student 函数是这样的:
<?PHP
namespace App\Http\Controllers;
use App\Student;
use App\Product;
use Session;
use User;
use Auth;
use DB;
use Illuminate\Http\Request;
use function GuzzleHttp\Promise\all;
class StudentsController extends Controller
{
public function addStudent(Request $request){...}
public function student(){
if(Auth::check()){
$user_email = Auth::user()->email;
$students = DB::table('students')->where(['user_email'=>$user_email])->get();
}
return redirect('/')->with(compact('students'));
}
<?PHP
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
use App\Student;
use Session;
use DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
class UsersController extends Controller
{
public function userRegister(){
return view('users.register');
}
public function userLogin(){
return view('users.login');
}
public function login(Request $request){
if($request->isMethod('post')){
$data = $request->all();
if(Auth::attempt(['email'=>$data['email'],'password'=>$data['password']])){
$userStatus = User::where('email',$data['email'])->first();
if($userStatus->status == 0){
return redirect()->back()->with('flash_message_error','Il tuo account non è attivo! Conferma la tua email per attivarlo.');
}
Session::put('frontSession',$data['email']);
return redirect('/');
}else{
return redirect()->back()->with('flash_message_error','Email o Password errate!');
}
}
}
public function register(Request $request){
if ($request->isMethod('post')) {
$data = $request->all();
//Check if User Already exist
$usersCount = User::where('email',$data['email'])->count();
if ($usersCount > 0) {
return redirect()->back()->with('flash_message_error','Email già registrata!');
} else {
$user = new User;
$user->name = $data['name'];
$user->email = $data['email'];
$user->password = bcrypt($data['password']);
$user->save();
// Send Confirmation Email
$email = $data['email'];
$messageData = ['email' => $data['email'],'name'=>$data['name'],'code'=>base64_encode($data['email'])];
Mail::send('emails.confirmation',$messageData,function ($message) use($email){
$message->to($email)->subject('Conferma Account');
});
return redirect('/login')->with('flash_message_success','Perfavore conferma la tua email per attivare il tuo account!');
}
}
}
public function logout(){
Auth::logout();
Session::forget('frontSession');
return redirect('/');
}
public function confirmAccount($email){
$email = base64_decode($email);
$userCount = User::where('email',$email)->count();
if ($userCount > 0) {
$userDetails = User::where('email',$email)->first();
if ($userDetails->status == 1) {
return redirect('/login')->with('flash_message_success','Il tuo account è già attivo! Effettua il login.');
} else {
User::where('email',$email)->update(['status' => 1]);
//Welcome Email
$messageData = ['email' => $email,'name'=>$userDetails->name];
Mail::send('emails.welcome',function ($message) use($email){
$message->to($email)->subject('Benvenuto!');
});
return redirect('/login')->with('flash_message_success','Il tuo account è stato attivato! Effettua il login adesso.');
}
}else{
abort(404);
}
}
public function checkEmail(Request $request)
{
$data = $request->all();
$usersCount = User::where('email',$data['email'])->count();
if ($usersCount > 0) {
echo "false";
} else {
echo "true"; die;
}
}
}
我如何才能为每个不同的用户只看到他使用他的电子邮件作为控制字段创建的学生? 提前致谢!
解决方法
看起来您正在重定向到您的 IndexController
return redirect('/')->with(compact('students'));
你不能重定向和传递一些变量
,感谢 Oleg Andreyev 的建议,我通过删除“StudentsController”中的“student”函数解决了这个问题,所以我删除了这一行:
public function student(){
if(Auth::check()){
$user_email = Auth::user()->email;
$students = DB::table('students')->where(['user_email'=>$user_email])->get();
}
return redirect('/')->with(compact('students'));
}
然后我将“index”函数更改为“IndexController”:
public function index(){
$students = Student::get();
return view('index')->with(compact('students'));
}
给这个:
public function index(){
$students = Student::get();
if(Auth::check()){
$user_email = Auth::user()->email;
$students = DB::table('students')->where(['user_email'=>$user_email])->get();
}
return view('index')->with(compact('students'));
}
现在我可以看到与创建他们的用户的电子邮件相关的学生!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。