Hello guys I want to have values from a different table but same database and print them on a dropdown, av tried several ways and here but i get this error
Call to undefined method Illuminate\Auth\SessionGuard::myVehicles() (View: /home/vagrant/www/martin/resources/views/templates/applications.blade.php)
I want to get them from a table containing all vehicles and print all the vehicles so that the user can select one of the vehicles in the database... here is vehicle model
<?php
namespace Martin\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Vehicle extends Authenticatable
{
use Notifiable;
//
use Notifiable;
protected $guard="vehicle";
protected $table="vehicles";
protected $fillable=[
'id',
'vehicle_registration_number',
'vehicle_type',
'vehicle_size',
];
public function user(){
return $this->belongsTo('Martin\Models\User');
}
public function myVehicles(){
$vehicles = Martin\Models\Vehicle::all();
foreach ($vehicles as $vehicle) {
echo $vehicle->vehicle_registration_number."-".$vehicle->vehicle_type."-".$vehicle->vehicle_size.' seaters <br/>';
}
}
}
and the following is the dropdown i want to display my data in the applications.blade.php so that a user can select one of the vehicles...
<select name="vehicle_registration_number" class="form-control">
{{Auth::guard('vehicle')->myVehicles()}}
</select>
and also I may ask what name do you assign to the select box do you assign it the same name as the original name of the input i.e text area of the original one? kindly reply... if any more clarification is needed am ready... Thankyou!
First, you should use a controller to handle your models, rather than trying to handle a class from the class itself. If so, you need to create static methods.
In whatever controller you are calling that page, you can get all vehicles
public function page()
{
//whatever you are doing here
...
$vehicles = Martin\Models\Vehicle::get();
foreach ($vehicles as $vehicle) {
$vehicle->description = $vehicle->vehicle_registration_number ."-". $vehicle->vehicle_type ."-". $vehicle->vehicle_size .' seaters';
}
return view('view_name', compact('vehicles'));
}
Then in your view you loop through vehicles in your select
<select name="vehicle_registration_number" class="form-control">
<option>Select Vehicle</option><!--selected by default-->
#foreach($vehicles as $vehicle)
<option value="{{ $vehicle->vehicle_registration_number }}">
{{ $vehicle->description }}
</option>
#endforeach
</select>
Related
I am using Livewire to create a form which databinds to the model like this, notice <select/> has multiple attribute:
#php
$items = ['bag','hat','mug','stickers'];
#endphp
<select wire:model="extra" multiple >
<option disabled value="select" >Select</option>
#foreach ($items as $item)
<option value="{{$item}}" >{{$item}}</option>
#endforeach
</select>
and the model class has a var on top:
public $extra = [''];
I would like to select multiple <option/> with just a click, currently you have to use the keyboard [command] + click.
I am trying to add logic in the model class but state of public $extra = ['']; is an issue.
example:
<option value="{{$item}}" wire:click="buildArr('{{$item}}')">{{$item}}</option>
then from model, $this->extra[] does not build on the array but rather refreshes and returns the last <option/> clicked:
public function buildArr($item){
$this->extra[]= $item;
}
How can I allow 1 click to build on this array? Do I need AlpineJS?
the answer to my problem was here https://laravel-livewire.com/docs/2.x/alpine-js#interacting-with-livewire-from-alpine
using: $wire.myMethod() from blade.
I am trying to fetch a list from the database into a <select> for the form.
I have a controller with classes (index, getLocations, create, store, edit, update, destroy)
public function getLocations($locationList)
{
$locationList = Locations::select('id', 'locationName')->get();
return view('pages.dataEntry.reports.reports', compact('locationList'));
}
getLocation has its own Model:
class Locations extends Model
{
use HasFactory;
protected $table = 'kaec_locations';
}
and I get undefined variable $locationList is undefined
<select class="form-select" name="caseLocation">
<option value="none" selected disabled>Select Location</option>
#foreach ($locationList as $item)
<option value="{{ $item->id }}"> {{ $item->locationName }} </option>
#endforeach
</select>
Is this a good practice? to have a separate Model for locations?
How come the variable is undefined? Should it be included in the route?
Route::controller(ReportController::class)->group(function () {
Route::get('reports', 'getLocations');
Route::get('reports', 'index')->name('reports.index'); // Index page (DataTable)
Route::get('reports/create', 'create')->name('reports.create'); // The form for adding new records
Route::post('reports/create', 'store')->name('reports.store'); // Add new to DB
Route::get('reports/edit/{report}', 'edit')->name('reports.edit'); // The form for editing records
Route::put('reports/edit/{report}', 'update')->name('reports.update'); // Update record to DB
Route::get('reports/{report}', 'destroy')->name('reports.destroy'); // Delete from DB
});
Got it. I do not need to make a function for this. I already have function for adding new data create(). The Create refers to the blade file of the form I want to display that <select>
So I just removed the getLocations() and added the code to create()
/**
** Add new Record Page.
**/
public function create()
{
$locationList = Locations::all();
return view('pages.dataEntry.reports._addForm', compact('locationList'));
}
Hello Guys, I am just passing my query to notification blade, but its gave error. I dont know what i did wrong with bellow code. If you guys fix this issue i will be very glad. Thanks in advance
Notification seen model
<?php
namespace App\Models\Backend;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class notificationseen extends Model
{
use HasFactory;
protected $table = 'notificationseens';
public function Notification()
{
return $this->belongsTo(Notification::class, 'notificationID');
}
}
Notification Model
<?php
namespace App\Models\Backend;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
use HasFactory;
protected $table = 'notifications';
public function notificationseen()
{
return $this->belongsTo(notificationseen::class, 'notificationID');
}
}
View Blade
#foreach( $notification as $notify )
#if($notify->Notification->seen == 0)
<!-- Single Notification --><a href="{{ route('singlenotify', $notify->id) }}" id="notifysee" data-id="{{ $notify->id }}">
<div class="alert unread custom-alert-3 alert-primary" role="alert"><i class="bi bi-bell mt-0"></i>
<div class="alert-text w-75">
<h6 class="text-truncate">{{ $notify->name }}</h6><span class="text-truncate">{{ $notify->description }}</span>
</div>
</div></a>
#else
<!-- Single Notification --><a href="{{ route('singlenotify', $notify->id) }}">
<div class="alert custom-alert-3 alert-primary" role="alert"><i class="bi bi-bell mt-0"></i>
<div class="alert-text w-75">
<h6 class="text-truncate">{{ $notify->name }}</h6><span class="text-truncate">{{ $notify->description }}</span>
</div>
</div></a>
#endif
#endforeach
Table structure
$table->increments('id');
$table->integer('userid');
$table->integer('notificationID');
$table->integer('seen')->default('0')->comment("0 for unseen 1 for seen");
$table->timestamps();
Can you please help me out. I cant see any issue but its me error "Attempt to read property "seen" on null"
Ok, some things:
I would use belongsTo in the notificationseen class unless one notificationseen could have more than one Notifications to belong to ;)
Do a Notification have more than one notificationseen references? I do not think so, so in your Notification change the reference to hasOne.
In your blade, use #if($notify->notificationseen->seen == 0) or you call better "Notification::with('notificationseen')->get()" in your controller and then pass it to your view.
You can try to add an isset to avoid the error :
#if(isset($notify->Notification->seen) && $notify->Notification->seen == 0)
(...)
#else
(...)
#endif
EDIT : in your code, you defined 2 belongsTo methods, but according to the official Laravel documentation, you must define a hasOne method and the inverse of it, the belongsTo method.
i am trying to make a list of locations that you can rent. but to rent the place you need to fill in some information. to fill in this information you excess another page. how do i make it so laravel knows the page belongs to a certain location
this is what ive done now but i keep getting the error:
Call to undefined method App\Reservation::location()
as soon as i have filled in the fields of information
this is the blade file that links to the the create reservation file
#foreach
($locations as $location => $data)
<tr>
<th>{{$data->id}}</th>
<th>{{$data->name}}</th>
<th>{{$data->type}}</th>
<th><a class="btn" href="{{route('Reservation.index', $data->id)}}">rent</a></th>
</tr>
#endforeach
this is the create reservations blade
<form action="{{ route('location.store') }}" method="post">
#csrf
<label>title</label>
<input type="text" class="form-control" name="name"/>
<label>type</label>
<select>
<option value="0">klein</option>
<option value="1">groot</option>
</select>
<button type="submit" class="btn">inschrijven</button>
</form>
this is what the location controller looks like
public function store(Request $request)
{
$location = new Reservation;
$location->name = $request->get('name');
$location->type = $request->get('type');
$location->location()->associate($request->location());
$location->save();
return redirect('/location');
}
and the relationships in my models should also work
class Reservation extends Model
{
public function locations()
{
return $this->belongsTo('Location::class');
}
}
class Location extends Model
{
public function reservations()
{
return $this->hasMany('Registration::class');
}
}
ive been stuck at this all day and i really dont know where to look anymore
The error you are getting is because of the wrong function name, you are calling location, while it is locations.
public function locations(){}
&
$location->location()->associate($request->location());
and you can pass the variable as a query parameter, you'll need to pass this data as an array in your blade file.
Web.php
Route::get('/somewhere/{id?}, function(){
//do something
})->name('test');
Blade
route('test', ['id' => $id]);
Controller Method
public function store(Request $request, $id) //Adding the query parameter for id passed in Route.
{
$location = new Reservation;
$location->name = $request->get('name');
$location->type = $request->get('type');
$location->location()->associate($id);
$location->save();
return redirect('/location');
}
I'm trying to give a dropdown list in view, but that should be from database.
This is my Controller:
public function index()
{
$this->load->view('welcome_message');
}
public function subject()
{
$this->load->view('sub');
}
In view simple dropdown list i have given,Each subject is having different id, which I have given in value of the option tag.
And only the subjects which statuses are active should be listed.
How to do that???
load your model in your controller assuming asadminmodel
Change your controller to
public function subject()
{
$this->load->model('adminmodel');
$data['info']=$this->adminmodel->get_dropdown();
$this->load->view('sub', $data);
}
In your model add get_dropdown() function
public function get_dropdown()
{
$this->db->where('status', 'active');
$query=$this->db->get('tablename');
return $query->result_array();
}
In your view page
<select>
<?php foreach($info as $info){ ?>
<option value="<?php echo $info['id']; ?>"><?php echo $info['subject']; ?></option>
<?php } ?>
</select>