简体   繁体   中英

view data only for specified registered id in laravel-8

Here I have two tables in my database named user and products here is 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();
        });
    }

using this table I can register and login by email address. after login it takes me to a page called index.blade.php where I have a button called add data after clicking it, it takes me to my create.blade.php page where I can register some data for the products table. after registered data, those data show me in index.blade.php. That's nice again I can add data and it shows in the index. there is no problem. But the problem is when I login from the index and registered again with a new email address and login with the new email address it's showing me old data I have added with my previous email. but I don't want to see old data after login with new email address. here is my product.php table

    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();
        });
    }

here is my 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();
    }

}

here is Product.php as model

class Product extends Model
{
    use HasFactory;

    protected $fillable = [
        'name', 'detail', 'image','color','logo'
    ];
}

this is 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

and this is 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>

I have done this by ```` Auth::id()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM