这里我的数据库中有两个表,分别名为user和products,这里是user.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->text('profile_photo_path')->nullable();
$table->timestamps();
});
}使用这个表,我可以通过电子邮件地址注册和登录。登录后,它将我带到一个名为index.blade.php的页面,其中有一个名为add data的按钮,单击它后,它将把我带到我的create.blade.php页面,在那里我可以为products表注册一些数据。在注册数据之后,这些数据将在index.blade.php中显示。这很好,我可以添加数据,它会显示在索引中。没有问题。但问题是,当我从索引登录并使用新的电子邮件地址再次注册,并使用新的电子邮件地址登录时,它会显示我在以前的电子邮件中添加的旧数据。但是我不想在使用新的电子邮件地址登录后看到旧数据。这是我的product.php表
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('detail');
$table->string('color');
$table->string('image');
$table->string('logo');
$table->timestamps();
});
}这是我的ProductController.php
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ProductController extends Controller
{
public function index()
{
if(Auth::check()){
$products = Product::latest()->paginate(1);
return view('products.index',compact('products'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
}
public function create()
{
return view('products.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
'color' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:640dp*480dp',
'logo' => 'required|mimes:jpeg,png,jpg,gif,svg|max:512*512',
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = 'image/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}
if ($logo = $request->file('logo')) {
$destinationPath = 'logo/';
$profileLogo = date('YmdHis') . "." . $logo->getClientOriginalExtension();
$logo->move($destinationPath, $profileLogo);
$input['logo'] = "$profileLogo";
}
Product::create($input);
return redirect()->route('products.index')
->with('success','Product created successfully.');
}
public function show(Product $product)
{
return view('products.show',compact('product'));
}
public function edit(Product $product)
{
return view('products.edit',compact('product'));
}
public function update(Request $request, Product $product)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
'color' => 'required'
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = 'image/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}else{
unset($input['image']);
}
if ($logo = $request->file('logo')) {
$destinationPath = 'logo/';
$profileLogo = date('YmdHis') . "." . $logo->getClientOriginalExtension();
$logo->move($destinationPath, $profileLogo);
$input['logo'] = "$profileLogo";
}else{
unset($input['logo']);
}
$product->update($input);
return redirect()->route('products.index')
->with('success','Product updated successfully');
}
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('products.index')
->with('success','Product deleted successfully');
}
function indextwo(){
//return DB::select("select * from products");
//DB::table('products')->orderBy('id','desc')->first();
return Product::orderBy('id', 'DESC')->first();
}
}下面是Product.php as模型
class Product extends Model
{
use HasFactory;
protected $fillable = [
'name', 'detail', 'image','color','logo'
];
}这是index.blade.php
@extends('products.layout')
@section('content')
<div class="row">
<div class="pull-right">
<!-- Authentication -->
<form method="POST" action="{{ route('logout') }}" style="margin: 20px">
@csrf
<x-jet-dropdown-link href="{{ route('logout') }}"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Log Out') }}
</x-jet-dropdown-link>
</form>
</div>
{{-- --}}
<div></div>
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Click Button</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('products.create') }}"> For New Data</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>App Name</th>
<th>App Logo</th>
<th>Splash Image</th>
<th>Description</th>
<th>Color</th>
<th width="280px">Action</th>
</tr>
@foreach ($products as $product)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $product->name }}</td>
<td><img src="/logo/{{ $product->logo }}" width="100px"></td>
<td><img src="/image/{{ $product->image }}" width="100px"></td>
<td>{{ $product->detail }}</td>
<td>{{ $product->color }}</td>
<td>
<form action="{{ route('products.destroy',$product->id)}}" method="POST">
<a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">Edit</a>
<a class="btn btn-info" href="products_link">Get Json</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
{!! $products->links() !!}
@endsection这是create.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
{{-- <title>Document</title> --}}
<title>Bootstrap Colorpicker Example - nicesnippets.com</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-colorpicker/2.5.1/css/bootstrap-colorpicker.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-colorpicker/2.5.1/js/bootstrap-colorpicker.min.js"></script>
</head>
<body>
@extends('products.layout')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New Data</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a>
</div>
</div>
</div>
@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('products.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-xs-4">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-4">
<div class="form-group">
<strong>Detail:</strong>
<textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></textarea>
</div>
</div>
<div class="container">
<strong>Color picker</strong>
<div id="cp2" class="input-group colorpicker colorpicker-component">
<input type="text" value="#00AABB" class="form-control" name="color" placeholder="Color" />
<span class="input-group-addon"><i></i></span>
</div>
</div>
<div class="col-xs-4">
<div class="form-group">
<strong>Logo:</strong>
<input type="file" name="logo" class="form-control" placeholder="logo">
</div>
</div>
<div class="col-xs-4">
<div class="form-group">
<strong>Image:</strong>
<input type="file" name="image" class="form-control" placeholder="image">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
<script type="text/javascript">
$('.colorpicker').colorpicker({});
</script>
@endsection
</body>
</html>发布于 2021-08-07 05:04:42
我已经通过 Auth::id()做到了这一点。
https://stackoverflow.com/questions/67363963
复制相似问题