estoy intentando hacer un dropdown list con un foreach en laravel pero me da el error undefined variable: Customers.
Les muestro como lo estoy haciendo en mi vista add-customers.blade.php:
<div class="form-group col-md-6">
<label for="account">Tipo de cuenta</label>
<select name="account" id="" class="form-control">
<option value="">Seleccione el tipo de cuenta</option>
@foreach ($customers as $customer)
<option value="{{$customer->id}}">{{$customer->account}}</option>
@endforeach
</select>
</div>
Este es el codigo de mi controlador con las funciones:
CustomersController
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Product;
use App\Model\Supplier;
use App\Model\Unit;
use App\Model\Category;
use App\Model\Purchase;
use App\Model\Invoice;
use App\Model\InvoiceDetail;
use App\Model\Payment;
use App\Model\PaymentDetail;
use App\Model\Customer;
use Auth;
use Session;
use DB;
use PDF;
class CustomerController extends Controller
{
public function view(){
$allData = Customer::all();
return view('backend.customer.view-customer', compact('allData'));
}
public function add(){
return view('backend.customer.add-customer');
}
public function store(Request $request){
$customer = new Customer();
$customer->holding = $request->holding;
/*$customer->name = $request->name;*/
$customer->company = $request->company;
$customer->legal_entity = $request->legal_entity;
/*$customer->mobile_no = $request->mobile_no;
$customer->mobile_two = $request->mobile_two;
$customer->mobile_three = $request->mobile_three;*/
$customer->email = $request->email;
/* $customer->position = $request->position;*/
$customer->country = $request->country;
$customer->state = $request->state;
$customer->city = $request->city;
$customer->address = $request->address;
$customer->postal = $request->postal;
$customer->cuit = $request->cuit;
$customer->website = $request->website;
$customer->id_parentCompany = $request->id_parentCompany;
$customer->created_by = Auth::user()->id;
$customer->save();
Session::flash('success');
return redirect()->route('customers.view');
}
public function edit($id){
$editData = Customer::find($id);
return view('backend.customer.edit-customer', compact('editData'));
}
public function detail($id){
$detailData = Customer::find($id);
return view('backend.customer.detail', compact('detailData'));
}
public function update(Request $request, $id){
$customer = Customer::find($id);
$customer->name = $request->name;
$customer->company = $request->company;
$customer->mobile_no = $request->mobile_no;
$customer->mobile_two = $request->mobile_two;
$customer->mobile_three = $request->mobile_three;
$customer->email = $request->email;
$customer->position = $request->position;
$customer->country = $request->country;
$customer->state = $request->state;
$customer->city = $request->city;
$customer->address = $request->address;
$customer->postal = $request->postal;
$customer->cuit = $request->cuit;
$customer->website = $request->website;
$customer->updated_by = Auth::user()->id;
$customer->save();
Session::flash('success');
return redirect()->route('customers.view');
}
public function delete($id){
$customer = Customer::find($id);
$customer->delete();
return redirect()->route('customers.view');
}
No entiendo porque me dice que la variable no esta definida, estoy continuando el proyecto de el programador anterior a mi de la empresa, asi que estoy siguiendo sus metodos y en el caso de las categorias o productos el dropdown funciona pero en la parte de clientes por algun motivo no.
En el caso de las categorias que si funciona asi es el controlador:
CategoriesController:
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Category;
use Auth;
use Session;
class CategoryController extends Controller
{
public function view(){
$allData = Category::all();
return view('backend.category.view-category', compact('allData'));
}
public function add(){
return view('backend.category.add-category');
}
public function store(Request $request){
$category = new Category();
$category->type = $request->type;
$category->name = $request->name;
$category->created_by = Auth::user()->id;
$category->save();
Session::flash('success');
return redirect()->route('categories.view');
}
public function edit($id){
$editData = Category::find($id);
return view('backend.category.edit-category', compact('editData'));
}
public function update(Request $request, $id){
$category = Category::find($id);
$category->type = $request->type;
$category->name = $request->name;
$category->updated_by = Auth::user()->id;
$category->save();
Session::flash('success');
return redirect()->route('categories.view');
}
public function delete($id){
$category = Category::find($id);
$category->delete();
return redirect()->route('categories.view');
}
}
Esta es su respectiva vista:
<div class="form-group col-md-3">
<label for="category_id">Categoria</label>
<select name="category_id" class="form-control">
<option value="">Seleccionar</option>
@foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->type }} - {{ $category->name }}</option>
@endforeach
</select>
</div>