我正在尝试使用laravel中两个数据库中的数据。编辑:我已将产品控制器添加到此帖的底部
这是我测试它们是否被链接的方式:
<p>TEST :{{ $products->ticket->created_at}}</p>
错误消息:
未定义变量:产品(查看:/Applications/MAMP/htdocs/lsapp/resources/views/products/create.blade.php)
Product.php(App)
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
//Table NAME
protected $table = 'products';
//PRIMARY KEY
public $primaryKey = 'id';
//Timestamps
public $timestamps =true;
public function user(){
return $this->belongsTo('App\User');
}
public function products()
{
return $this->hasMany(App\product::class);
}
}
Ticket.php(app)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class product extends Model
{
public function Product(){
return $this->belongsTo(App\Product::class);
}
}
产品总监:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\product;
use App\Ticket;
class productController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth',['except' => ['index','show']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = product::orderBy('created_at','desc')->paginate(10);
//$products = product::where('type','major')->get();
return view('products.index')->with('products',$products);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('products.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'title' => 'required',
]);
//create product
$product = new product;
$product->title = $request->input('title');
$product->venue = $request->input('venue');
$product->city = $request->input('city');
$product->country = $request->input('country');
$product->description = $request->input('description');
$product->date = $request->input('date');
$product->user_id = auth()->user()->id;
$product->save();
return redirect('/products')->with('success','product Created');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$product = product::find($id);
return view('products.show')->with('product',$product);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$product = product::find($id);
if(auth()->user()->id !==$product->user_id){
return redirect('/products')->with('error','unauthorised page');
}
return view('products.edit')->with('product',$product);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'title' => 'required'
]);
$product = product::find($id);
$product->title = $request->input('title');
$product->venue = $request->input('venue');
$product->city = $request->input('city');
$product->country = $request->input('country');
$product->description = $request->input('description');
$product->date = $request->input('date');
$product->user_id = auth()->user()->id;
$product->save();
return redirect('/products')->with('success','product updated');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$product = product::find($id);
if(auth()->user()->id !==$product->user_id){
return redirect('/products')->with('error','unauthorised page');
}
$product->delete();
return redirect('/products')->with('success','product deleted');
}
}
答案 0 :(得分:0)
您的代码存在一些问题。
<p>TEST :{{ $products->ticket->created_at}}</p>
永远不会奏效。
假设您正在索引页面中进行测试,则需要遍历每个产品以查看它与故障单的关系。
您的所有产品都有一张票吗?如果是这样,那么您可以在配置变量中定义单个票证。在数据库中定义多对一关系是没有意义的。
在Product
模型中,您尚未定义与Ticket
模型的关系。您只在Ticket
模型中为Product
模型定义了关系。这意味着您可以从product
访问ticket
,但不能相反。您需要明确定义从Product
到Ticket
的一对一关系才能使用它。
您已在产品型号中定义了此功能。
public function products() {
return $this->hasMany(App\product::class);
}
为什么呢?产品本身有很多产品吗?我想你可以从学习雄辩的关系中受益。 Laracasts是从头开始学习Laravel的好资源。 另请参阅Laravel official Eloquent documentation