在Laravel中显示具有多对多关系的数据的问题

时间:2019-04-09 15:23:02

标签: php laravel

我与用户和产品表之间有许多关系,并且我用product_id,user_id列设置了数据透视表product_user。当我尝试上传一些文件时,它成功上传到数据库,并且在页面上显示数据,但没有显示“产品所有者”列,该列是该关系所在的列。同样,当我手动更新数据库表product_user并设置正确的ID和刷新页面时,“产品所有者”列也会显示正确的信息。当我动态上传文件而无需手动更新数据库时,我想显示该信息。我尝试使用attach方法,但由于某种原因它无法正常工作。感谢您的帮助,这是我的代码:

HomeController:

<?php

namespace App\Http\Controllers;

use App\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller
{
    public function store(Request $request)
    {
        if ($file = $request->file('files')) {
            $name = $file->getClientOriginalName();
            if ($file->move('products', $name)) {
                $product = new Product();
                $product->files = $name;
                $product->save();
                $product->users()->attach($request->user_id);

                return redirect()->route('welcome');
            }
            return redirect()->back();
        }
    }
}

welcome.blade.php:

@if($products)
<table class="table">
    <thead>
        <th>#</th>
        <th>Product Name</th>
        <th>Owner Of The Product</th>
        <th>Created At</th>
    </thead>
    <tbody>
    @foreach ($products as $product)
         <tr>
             <td>{{ $product->id }}</td>
             <td>{{ $product->files }}</td>
             <td>
                 @foreach ($product->users as $user)
                     {{ $user->name }}
                 @endforeach
             </td>
             <td>{{ date('M j, Y', strtotime($product->created_at)) }</td>
         </tr>
    @endforeach
    </tbody>
 </table>
 @endif

home.blade.php:

<div class="col-md-12">
    <form action="/store" method="POST" enctype="multipart/form-data">
        {{ csrf_field() }}
        <div class="form-group">
            <input type="file" name="files" class="form-control">
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-success" value="Submit">
        </div>
     </form> 
 </div>

User.php:

<?php

namespace App;

use App\Product;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
    'name', 'email', 'password',
    ];

    protected $hidden = [
    'password', 'remember_token',
    ];

    protected $casts = [
    'email_verified_at' => 'datetime',
    ];

    public function products()
    {
        return $this->belongsToMany(Product::class, 'product_user', 'user_id', 'product_id');
    }
}

Product.php:

<?php
namespace App;

use App\User;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = ['files'];

    public function users()
    {
        return $this->belongsToMany(User::class, 'product_user', 'product_id', 'user_id');
    }
}

1 个答案:

答案 0 :(得分:1)

如果您不提交具有该名称的字段,则请求没有user_id属性。

您有两个选择来获取当前用户的ID。我的选择是Auth::id()auth()->id,因为它们不会从数据库中加载用户模型。

获取用户ID的更多方法:how to get current user id in laravel 5.4