I'm getting a undefined variable error in my view if I include this route with a parameter:
<div class="table-toolbar">
<div class="row">
<div class="col-md-6">
<div class="btn-group">
<i class="fa fa-plus"></i> Add new
</div>
</div>
</div>
</div>
Here's my full controller for that given view:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Model\CampusMajor;
use App\Model\CampusMajorsFacilities;
class CampusMajorsFacilitiesController extends Controller
{
//
public function index($campusMajorId)
{
$campusMajorsFacilities = CampusMajorsFacilities::ofCampusMajor($campusMajorId)->get();
$data = [
'campusMajorsFacilities' => $campusMajorsFacilities,
'campusMajor' => $campusMajorId
];
return view('major-facilities.index', $data);
}
public function add($campusMajorId)
{
$campusMajorsFacilities = new CampusMajorsFacilities;
$data = [
'campusMajorsFacilities' => $campusMajorsFacilities,
'campusMajorId' => $campusMajorId,
'formIs' => 'add'
];
return view('major-facilities.form', $data);
}
public function save(Request $request,$campusMajorId)
{
$this->validation($request);
$post = $request->all();
$campusMajorsFacilities = $this->bindToObject($post,$campusMajorId);
$campusMajorsFacilities->save();
return redirect()->route('major-facilities.index', $campusMajorId)->with('message','Item added Succesfully');
}
public function edit($campusMajorId, $id)
{
$campusMajorsFacilities = CampusMajorsFacilities::find($id);
$data = [
'campusMajorsFacilities' => $campusMajorsFacilities,
'campusMajorId' => $campusMajorId,
'formIs' => 'edit'
];
return view('major-facilities.form', $data);
}
public function update(Request $request,$campusMajorId,$id)
{
$this->validation($request);
$post = $request->all();
$campusMajorsFacilities = $this->bindToObject($post,$campusMajorId,$id);
$campusMajorsFacilities->save();
return redirect()->route('major-facilities.index', $campusMajorId)->with('message','Item updated Succesfully');
}
private function validation($request)
{
$this->validate($request,[
'title' => 'required',
'description' => 'required',
'content' => 'required',
'campus_major_id' => 'required'
]);
}
public function delete($campusMajorId,$id)
{
$campusMajorsFacilities = CampusMajorsFacilities::find($id);
$campusMajorsFacilities->delete();
return redirect()->route('major-facilities.index', $campusMajorId)->with('message','Item deleted Succesfully');
}
private function bindToObject($post,$campusMajorId,$id=null)
{
if(is_null($id)){
$campusMajorsFacilities = new CampusMajorsFacilities;
} else {
$campusMajorsFacilities = CampusMajorsFacilities::find($id);
}
$campusMajorsFacilities->major_campus_id = $campusMajorId;
$campusMajorsFacilities->title = $post['title'];
$campusMajorsFacilities->description = $post['description'];
$campusMajorsFacilities->content = $post['content'];
return $campusMajorsFacilities;
}
}
And my model
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class CampusMajorsFacilities extends Model
{
//
use SoftDeletes;
public function CampusMajor()
{
return $this->belongsTo('App\Model\CampusMajor');
}
public function scopeOfCampus($query, $campus_id)
{
return $query->where('campus_id', $campus_id);
}
public function scopeOfCampusMajor($query, $campus_major_id)
{
return $query->where('campus_major_id', $campus_major_id);
}
}
If I comment out the route I passed the view just fine, I don't know where I went wrong.
You have passed $campusMajorId from controller to view as campusMajor only, not as campusMajorId.
Hence the variable is undefined in the view.
Related
Good Day all
before this get removes i must note that i have tried searching for this but i still cant understand why this is not working
Please can someone help me with this i tried doing this for a few weeks now and i cannot figure out why my delete button does not work i did it on the code igniter3 and it work but i canot seem to get it to work as i am still learning and is new all i want to do is delete a post with a button that i have created ! that all please explain what i did wrong so i can learn this is just a test project to teach myself code igniter 4 thank you so much
sorry for my bad english
so here is my code
Model (Newsmodel.php)
<?php
namespace App\Models;
use CodeIgniter\Model;
class NewsModel extends Model
{
protected $table = 'news';
protected $allowedFields = ['title', 'slug', 'body'];
public function getNews($slug = false)
{
if ($slug === false) {
return $this->findAll();
}
return $this->asArray()
->where(['slug' => $slug])
->first();
}
}
here is Controller(News.php)
<?php
namespace App\Controllers;
use App\Models\NewsModel;
use CodeIgniter\Controller;
class News extends Controller
{
public function index()
{
$model = new NewsModel();
$data = [
'news' => $model->getNews(),
'title' => 'News archive',
];
echo view('templates/header', $data);
echo view('news/overview', $data);
echo view('templates/footer', $data);
}
public function view($slug = null)
{
$model = new NewsModel();
$data['news'] = $model->getNews($slug);
if (empty($data['news'])) {
throw new \CodeIgniter\Exceptions\PageNotFoundException('Cannot find the news item: ' . $slug);
}
$data['title'] = $data['news']['title'];
echo view('templates/header', $data);
echo view('news/view', $data);
echo view('templates/footer', $data);
}
public function create()
{
$model = new NewsModel();
if ($this->request->getMethod() === 'post' && $this->validate([
'title' => 'required|min_length[3]|max_length[255]',
'body' => 'required',
])) {
$model->save([
'title' => $this->request->getPost('title'),
'slug' => url_title($this->request->getPost('title'), '-', true),
'body' => $this->request->getPost('body'),
]);
echo view('news/success');
} else {
echo view('templates/header', ['title' => 'Create a news item']);
echo view('news/create');
echo view('templates/footer');
}
}
}
View(view.php)
<h2><?=esc($news['title'])?></h2>
<p><?=esc($news['body'])?></p>
<button>Delete</button>
i have tried the following in my controller
and it didint delete it from my database but i also did not get an error! it just redirects me to my succes page.
public function delete($id = null)
{
$model = new NewsModel();
$model->delete([
$model->where('id', $id)->delete(),
]);
return view('news/delete');
}
i have also tried this
public function delete()
{
$model = new NewsModel();
if ($this->request->getMethod() === 'post') {
$model->delete([
'title' => $this->request->getPost('title'),
'body' => $this->request->getPost('body'),
]);
echo view('news/delete');
}
DB set up
DB NAME= toets
TABLE= news
id|title|slug|body
i changed my view button to
Delete
and in my controller i made
public function delete($id)
{
$model = new NewsModel();
$model->where('id', (int) $id)->delete();
return view('news/delete');
}
I can't insert data in the database. I'm using yii2 advanced.
I have 3 tables: the default user table, post, and category. Each post has one category, and one category can have many posts.
But when I click on 'Save' in my create form - nothing happens, the page just refreshes. The post table is still empty.
Here's PostController
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use frontend\models\CreateForm;
use frontend\models\Category;
use yii\helpers\ArrayHelper;
class PostController extends Controller
{
public function actionCreate()
{
if(Yii::$app->user->isGuest)
{
return $this->redirect(['/site/login']);
}
$model = new CreateForm();
$category = Category::find()->all();
$items = ArrayHelper::map($category, 'id', 'name');
if ($model->load(Yii::$app->request->post()))
{
if ($model->save())
{
Yii::$app->session->setFlash(
'success',
true
);
return $this->refresh();
} else
{
Yii::$app->session->setFlash(
'success',
false
);
}
}
return $this->render('create', [
'model' => $model,
'items' => $items
]);
}
This is CreateForm model
namespace frontend\models;
use yii\base\Model;
use frontend\models\Post;
use Yii;
class CreateForm extends Model
{
public $category;
public $title;
public $content;
public function attributeLabels()
{
return [
'category' => 'Category',
'title' => 'Title',
'content' => 'Content',
];
}
public function save()
{
$post = new Post();
$post->user_id = Yii::$app->user->id;
$post->category_id = $this->category;
$post->title = $this->title;
$post->content = $this->content;
$post->created_at = time();
}
}
And this the view
use yii\bootstrap\ActiveForm;
use yii\bootstrap\Html;
$form = ActiveForm::begin();
echo $form->field($model, 'category')->dropDownList($items);
echo $form->field($model, 'title')->textInput();
echo $form->field($model, 'content')->textInput();
echo Html::submitButton('Save', ['class' => 'btn btn-primary']);
ActiveForm::end();
What am I doing wrong, I can't understand
Ok. All I had to do is to add $post->save() in the end of my save method in CreateForm.
I have create a filter method in my project where I filtered the data using this method but now I want to refactor the code using queryScope method in laravel can anyone suggest me how to refactor this code.
This code is working fine.
This is my controller index method
public function index(Request $request)
{
$status = Ticket_status::pluck('name');
$tickets = Ticket::with('users','ticketStatus','ticketType','tbl_contacts')
->where('user_id','=',Auth::user()->id)
->latest();
if (request('Open')) {
$tickets = $tickets->where('status_id',1)->get();
} elseif (request('Pending')) {
$tickets = $tickets->where('status_id',2)->get();
} elseif (request('Close')) {
$tickets = $tickets->where('status_id',3)->get();
} else {
$tickets = $tickets->get();
}
return view('ticketing.user.index',compact('tickets','status'));
}
and this is my blade file.. In this all code is running good but I want to some refactor
<div class="col-md-8">
<a
href="{{route('tickets.index')}}"
class="btn btn-sm btn-outline-secondary mr-1">
All
</a>
#foreach ($status as $status_name)
<a
href="/tickets?{{Str::lower($status_name)}}={{ Str::lower($status_name) }}"
class="btn btn-sm btn-outline-secondary mr-1">
{{$status_name}}
</a>
#endforeach
</div>
And this is my model.
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Ticket extends Model
{
use SoftDeletes;
//Table Name
protected $table = 'tickets';
//Primary key
public $primaryKey = 'id';
protected $fillable = [
'ticket_number',
'name',
'description',
'contact_id',
'product_id',
'status_id',
'type_id',
'priority',
'user_id',
'ticket_image',
'start_date',
];
protected $casts = [
'start_date' => 'datetime',
];
protected $dates = [
'start_date',
'deleted_at',
];
protected $filepath = '/storage/';
public function getRouteKeyName()
{
return 'ticket_number';
}
public function setStartDateAttribute($date)
{
$this->attributes['start_date'] = Carbon::parse($date)->format('Y-m-d H:i:s');
}
public function getTicketImageAttribute($value)
{
return asset($value ? $this->filepath.$value: 'uploads/default/products.jpg');
}
public function ticketType() {
return $this->belongsTo( 'App\Ticket_type', 'type_id' );
}
public function ticketStatus() {
return $this->belongsTo( 'App\Ticket_status', 'status_id' );
}
public function tbl_contacts() {
return $this->belongsTo('App\Tbl_contacts', 'contact_id');
}
public function tbl_products() {
return $this->belongsTo('App\Tbl_products', 'product_id');
}
public function users() {
return $this->belongsTo('App\User', 'user_id');
}
public function getPriorityAttribute($value) {
if ($value == 1) {
// return "<span class='dot dot-sm dot-success'></span> Low";
return $value;
} elseif($value == 2) {
// return "<small class='dot dot-sm dot-warning'></small> Medium";
return $value;
} else {
// return "<span class='dot dot-sm dot-danger'></span> High";
return $value;
}
}
/**
* Get all of the Ticket's comments.
*/
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
// public function scopeFilter($query, $filters) {
// if ($stauts = $filters['open']) {
// $query->where('status_id','=',$stauts);
// } elseif ($stauts = $filters['pending']) {
// $query->where('status_id','=',$stauts);
// } elseif ($stauts = $filters['close']) {
// $query->where('status_id','=',$stauts);
// }
// }
}
You can define local scopes on the model to refactor the query. Here are few:
class Ticket extends Model
{
use SoftDeletes;
public function scopeByAuthUser($query)
{
return $query->where('user_id','=', \Auth::user()->id);
}
public function scopeOpen($query)
{
return $query->where('status_id', 1);
}
public function scopePending($query)
{
return $query->where('status_id', 2);
}
public function scopeClose($query)
{
return $query->where('status_id', 2);
}
}
Here's how you can refactor your condition:
// for the first query
$tickets = Ticket::with('users','ticketStatus','ticketType','tbl_contacts')
->byAuthUser()
->latest();
if(request('Open') || request('Pending') || request('Close')) {
$scope = strtolower(request('Open') ?? request('Pending') ?? request('Close'));
$tickets = $tickets->{$scope}()->get();
} else {
$tickets = $tickets->get();
}
I already create 3 models objects : User.php, Post.php, DataPribadi.php
This is User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
public function posts()
{
return $this->hasMany(Post::class);
}
public function datapribadi()
{
return $this->belongsTo(DataPribadi::class);
}
}
This is Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $dates = ['created_at','updated_at'];
public function user()
{
return $this->belongsTo(User::class);
}
}
This is DataPribadi.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class DataPribadi extends Model
{
protected $table = 'datapribadi';
protected $fillable = ['namalengkap','namapanggilan','tempatlahir','tgllahir','jeniskelamin','golongandarah','namaayah','namaibu','avatar','user_id'];
public function getAvatar()
{
if(!$this->Avatar){
return asset('images/default.jpg');
}
return asset('images/'.$this->Avatar);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
This is DataPribadiController.php
<?php
namespace App\Http\Controllers;
use App\Exports\DataPribadiExport;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Http\Request;
use App\User;
use PDF;
use App\DataPribadi;
//use App\Http\Requests;
class DataPribadiController extends Controller
{
public function index(Request $request)
{
#ini untuk ambil data yang masuk di variable $request
#dd($request->all());
#untuk search
if($request->has('cari')){
$data_datapribadi = \App\DataPribadi::where('NamaLengkap','LIKE','%'.$request->cari.'%')->get();
}else{
#untuk tampilkan data
$data_datapribadi = \App\DataPribadi::all(); #INI TAMBAHKAN WHERE ISACTIVE = 1
}
return view ('datapribadi.index',['data_datapribadi' => $data_datapribadi]);
}
public function create(Request $request)
{
$this->validate($request,[
'namalengkap' => 'required|min:5',
'namapanggilan' => 'required|min:5',
'email' => 'required|email|unique:users',
'tempatlahir' => 'required',
'tgllahir' => 'required|date_format:Y-m-d',
'golongandarah' => 'required',
'namaayah' => 'required|min:5',
'namaibu' => 'required|min:5',
'avatar' => 'mimes:jpeg,png|dimensions:max_width=400,max_height=400,min_width=100,min_height=100'
]);
#untuk insert user
$user = new \App\User;
$user->role = 'jemaat';
$user->name = $request->namalengkap;
$user->email = $request->email;
$user->password = bcrypt('jemaat');
$user->remember_token = str_random(60);
$user->save();
#untuk insert datapribadi
$request->request->add(['user_id' => $user->id ]);
$datapribadi = \App\DataPribadi::create($request->all());
if($request->hasFile('avatar')){
$request->file('avatar')->move('images',$request->file('avatar')->getClientOriginalName());
$datapribadi->avatar = $request->file('avatar')->getClientOriginalName();
$datapribadi->save();
}
return redirect('/datapribadi')->with('sukses','Data berhasil diinput');
}
public function edit(DataPribadi $datapribadi)
{
#untuk ambil data yang ingin di edit
//$datapribadi = \App\DataPribadi::find($IdPribadi);
return view('datapribadi/edit',['datapribadi'=>$datapribadi]);
}
public function update(Request $request, DataPribadi $datapribadi)
{
$this->validate($request,[
'namalengkap' => 'required|min:5',
'namapanggilan' => 'required|min:5',
'tempatlahir' => 'required',
'tgllahir' => 'required|date_format:Y-m-d',
'golongandarah' => 'required',
'namaayah' => 'required|min:5',
'namaibu' => 'required|min:5',
'avatar' => 'mimes:jpeg,png|dimensions:max_width=400,max_height=400,min_width=100,min_height=100'
]);
//dd($request->all());
#untuk mengupdate data yang ingin di edit
//$datapribadi = \App\DataPribadi::find($id);
$datapribadi->update($request->all());
if($request->hasFile('avatar')){
$request->file('avatar')->move('images',$request->file('avatar')->getClientOriginalName());
$datapribadi->avatar = $request->file('avatar')->getClientOriginalName();
$datapribadi->save();
}
return redirect('/datapribadi')->with('sukses','Data berhasil diupdate');
}
public function delete(DataPribadi $datapribadi)
{
#untuk menghapus data
//$datapribadi = \App\DataPribadi::find($IdPribadi);
$datapribadi->delete($datapribadi);
return redirect('/datapribadi')->with('sukses','Data berhasil dihapus');
}
public function profile(DataPribadi $datapribadi)
{
//$datapribadi = \App\DataPribadi::find($IdPribadi);
$ptingkatpendidikanmaster = \App\TingkatPendidikanMaster::all();
$ppekerjaanmaster = \App\PekerjaanMaster::all();
$phobimaster = \App\HobiMaster::all();
$pperiodekehadiranmaster = \App\PeriodeKehadiranMaster::all();
//dd($tingkatpendidikanmaster);
//Menyiapkan data untuk chart
$categories = [];
$data = [];
foreach($pperiodekehadiranmaster as $pkm){
if($datapribadi->periodekehadiranmaster()->wherePivot('periode_kehadiran_master_id',$pkm->id)->first()){
$categories[]= $pkm->bulantahun;
$data[]= $datapribadi->periodekehadiranmaster()->wherePivot('periode_kehadiran_master_id',$pkm->id)->first()->pivot->jumlah;
}
}
//dd($categories);
//dd($data);
return view('datapribadi.profile',['datapribadi'=>$datapribadi, 'ptingkatpendidikanmaster'=>$ptingkatpendidikanmaster, 'ppekerjaanmaster'=>$ppekerjaanmaster, 'phobimaster'=>$phobimaster, 'pperiodekehadiranmaster'=>$pperiodekehadiranmaster, 'categories' =>$categories, 'data' =>$data]);
}
public function addriwayatpendidikan(Request $request, $iddatapribadi)
{
//dd($request->all());
$datapribadi = \App\DataPribadi::find($iddatapribadi);
if($datapribadi->tingkatpendidikanmaster()->where('tingkat_pendidikan_master_id',$request->tingkatpendidikanmaster)->exists()){
return redirect('datapribadi/'.$iddatapribadi.'/profile')->with('error','Data Riwayat Pendidikan Sudah Pernah Didaftarkan');
}
$datapribadi->tingkatpendidikanmaster()->attach($request->tingkatpendidikanmaster,['tahun' => $request->tahun]);
return redirect('datapribadi/'.$iddatapribadi.'/profile')->with('sukses','Data Riwayat Pendidikan berhasil dimasukkan');
}
public function addpekerjaan(Request $request, $iddatapribadi)
{
//dd($request->all());
$datapribadi = \App\DataPribadi::find($iddatapribadi);
if($datapribadi->pekerjaanmaster()->where('pekerjaan_master_id',$request->pekerjaanmaster)->exists()){
return redirect('datapribadi/'.$iddatapribadi.'/profile')->with('error','Data Pekerjaan Sudah Pernah Didaftarkan');
}
$datapribadi->pekerjaanmaster()->attach($request->pekerjaanmaster,['utamasampingan' => $request->utamasampingan]);
return redirect('datapribadi/'.$iddatapribadi.'/profile')->with('sukses','Data Pekerjaan berhasil dimasukkan');
}
public function addhobi(Request $request,$iddatapribadi)
{
//dd($request->all());
$datapribadi = \App\DataPribadi::find($iddatapribadi);
if($datapribadi->hobimaster()->where('hobi_master_id',$request->hobimaster)->exists()){
return redirect('datapribadi/'.$iddatapribadi.'/profile')->with('error','Data Hobi Sudah Pernah Didaftarkan');
}
$datapribadi->hobimaster()->attach($request->hobimaster,['intensitas' => $request->intensitas]);
return redirect('datapribadi/'.$iddatapribadi.'/profile')->with('sukses','Data Hobi berhasil dimasukkan');
}
public function addperiodekehadiran(Request $request,$iddatapribadi)
{
//dd($request->all());
$datapribadi = \App\DataPribadi::find($iddatapribadi);
if($datapribadi->periodekehadiranmaster()->where('periode_kehadiran_master_id',$request->periodekehadiranmaster)->exists()){
return redirect('datapribadi/'.$iddatapribadi.'/profile')->with('error','Data Kehadiran Sudah Pernah Didaftarkan');
}
$datapribadi->periodekehadiranmaster()->attach($request->periodekehadiranmaster,['jumlah' => $request->jumlah]);
return redirect('datapribadi/'.$iddatapribadi.'/profile')->with('sukses','Data Kehadiran berhasil dimasukkan');
}
public function editjumlah(Request $request, $id)
{
return $request->all();
}
public function deletejumlah($iddatapribadi, $idperiodekehadiran)
{
$datapribadi = \App\DataPribadi::find($iddatapribadi);
$datapribadi->periodekehadiranmaster()->detach($idperiodekehadiran);
return redirect()->back()->with('sukses','Data Jumlah Kehadiran Berhasil Dihapus');
}
public function exportExcel()
{
return Excel::download(new DataPribadiExport, 'Datapribadi.xlsx');
}
public function exportPdf()
{
$datapribadi = \App\DataPribadi::all();
$pdf = PDF::loadView('export.datapribadipdf', ['datapribadi' => $datapribadi]);
//$pdf = PDF::loadHTML('<h1>Data Jemaat</h1>');
return $pdf->download('datapribadi.pdf');
}
}
I tried to call it from my singlepost.blade.php using this code :
<img src="{{$post->user->datapribadi->getAvatar()}}" alt="">
and the laravel system error :
ErrorException (E_ERROR)
Call to a member function getAvatar() on null
How can i solve this error??
Thanks in advance..
the problem is in User.php public function datapribadi() { return $this->hasOne(DataPribadi::class); } It should be "hasOne".
I have car table with status field and there's "available", and "breakdown".
I also got fleet CRUD, once I add fleet I want to show car with only "available" status. what should I add ?
here's my fleet controller code :
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Models\Fleet;
use App\Models\Car;
use App\Models\Attendance;
use App\Helpers\Enums\FleetStatus;
use App\Models\AttendanceDetail;
use Illuminate\Http\Request;
use Yajra\Datatables\Datatables;
use App\Http\Controllers\Base\MasterController;
use Route;
use Illuminate\View\View;
class FleetController extends MasterController
{
protected $detailView = 'fleet.show';
protected $indexView = 'fleet.index';
protected $createView = 'fleet.form';
protected $editView = 'fleet.form';
protected $routeBindModel = 'fleet'; //Route Model Binding name in RouteServiceProvider
protected $redirectPageWhenFormSuccess = 'fleet.index'; //Route Name
protected $title = 'Fleet';
public function save(Request $request, $obj = null) {
if (!$obj) {
$obj = new Fleet;
}
return $this->saveHandler($request, $obj);
}
//Must have this method if need datatable;
public function datatableBuilder($obj = null) {
return Fleet::query();
}
public function makeDatatable($obj) {
return Datatables::of($obj)
->addColumn('action', function ($model) {
return $this->makeActionButtonsForDatatable($model);
})
->editColumn('status', function($model){
return FleetStatus::getString($model->status);
})
->make(true);
}
public function render(View $view, $route = null, $obj = null, $method = 'POST') {
$statusList = FleetStatus::getArray();
$carStatusList = Car::pluck('plate_no', 'id');
$attendanceList = Attendance::pluck('driver_id', 'id');
$workingHourList = Attendance::pluck('working_hours', 'id');
$view->with(compact('statusList', 'carStatusList', 'attendanceList', 'workingHourList'));
return parent::render($view, $route, $obj, $method);
}
}
here's my add form code :
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<label>Car </label>
{!! Form::select('car_id', $carStatusList, null, array('class' => 'form-control')) !!}
</div>
</div>
Have you try this:
$carStatusList = Car::where('status', 'available')->pluck('plate_no', 'id')
In your model add a scope
public function scopeAvailable($query){
return $query->where('status', '=', 'available');
}
And then wherever you need to use it:
Car::available()->get()
or
Car::available()->pluck('plate_no' , 'id');
More info here: https://laravel.com/docs/5.3/eloquent#query-scopes
If I understand your question correctly, this will create a list of available cars:
Car::where('status','available')->pluck('plate_no', 'id');