我已将localhost
设置为新PC。将数据库导入到phpmyadmin
后,我运行laravel
并打开页面。但是,我收到此错误:
试图获取非对象的属性“ id”(视图:C:\ xampp \ htdocs \ inventoryApp \ resources \ views \ Item \ edit.blade.php)
在另一页上,出现此错误:
试图获取非对象的属性“名称”(视图:C:\ xampp \ htdocs \ inventoryApp \ resources \ views \ Item \ show.blade.php)
我的view
代码如下:
<table id="sale-table" class="table table-bordered table-striped table-hover datatable">
<thead>
<tr>
<th>No</th>
<th>Seller</th>
<th>Item ID</th>
<th>Item Name</th>
<th>Quantity</th>
<th>Total Sale</th>
<th>Detail</th>
<th>Action</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($sales as $sale)
<tr>
<td>{{ $sale->id }}</td>
<td>{{ $sale->user->name }}</td>
<td>{{ $sale->item_id }}</td>
<td>{{ $sale->item->name }}</td>
<td>{{ $sale->quantity }}</td>
<td>RM {{ $sale->price }}</td>
<td>{{ $sale->created_at }}</td>
@if(Auth::user()->type=='admin'||Auth::user()->type=='super_admin')
<td class="center">
<a href="{{ route('Sale.edit', ['id'=>$sale->id ]) }}" class="btn btn-warning btn-sm custom"><i class="glyphicon glyphicon-edit"></i> EDIT</a>
</td>
<td class="center">
<form action="{{ route('Sale.destroy', ['id'=>$sale->id ]) }}" method="post" onsubmit="return confirm('Delete sale {{ $sale->name }} permanently?')" >
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button class="btn btn-danger btn-sm" type="submit" ><i class="glyphicon glyphicon-trash"></i> DELETE</button>
</form>
</td>
@else
<td class="center">
<b><p>Only for admin</p></b>
</td>
<td class="center">
<b><p>Only for admin</p></b>
</td>
@endif
</tr>
@endforeach
</tbody>
</table>
我的控制者是:
class SalesController extends Controller
{
public function index()
{
$sales=Sale::all();
return view('Sale.index')->with('sales',$sales);
}
public function create()
{
$items=Item::all();
return view('Sale.create')->with('items',$items);;
}
public function store(Request $request)
{
$this->validate($request, [
'item_id'=>'required|integer',
//'price'=>'required|regex:/^\d+(\.\d{1,2})?$/',
'quantity'=>'required|integer',
]);
$item=Item::FindOrFail($request->item_id);
$item->quantity -= ($request->quantity);
$sale=new Sale;
$sale->seller_id=$request->get('seller_id');
$sale->item_id=$item->id;
$sale->price=$item->selling_price * $request->input('quantity');
$sale->quantity=$request->input('quantity');
$sale->save();
$item->save();
return redirect('/Sale')->with('success','Sale added');
}
public function edit($id)
{
$sales=Sale::find($id);
return view('Sale.edit')->with('sales',$sales);
}
public function update(Request $request, $id)
{
$this->validate($request, [
//'price'=>'required|regex:/^\d+(\.\d{1,2})?$/',
'quantity'=>'required|integer',
]);
$sale=Sale::find($id);
$item=Item::FindOrFail($sale->item_id);
$item->quantity = $item->quantity + $sale->quantity - $request->quantity;
$sale->quantity=$request->input('quantity');
$sale->price=$item->selling_price * $request->input('quantity');
$item->save();
$sale->save();
return redirect('/Sale')->with('success','Sale updated');
}
public function destroy($id)
{
$sale = Sale::find($id);
$sale->delete();
return redirect('/Sale')->with('success','Sale Removed');
}
}
当我尝试添加新项目时,它工作良好。但是我不能使用导入的。
答案 0 :(得分:1)
确保在销售表中有ID列。
,并且已经在迁移文件和销售模型中与用户模型和项目模型建立了关系。
public function user()
{
return $this->belongsTo(User::class);
}
public function item()
{
return $this->belongsTo(Item::class); // assuming you have named the model "Item" for your item table.
}