Let me explain situation, person is searching through skills and when clicked displays list of handymans with that skill, when user clicks on one of them, full details are displayed. user then clicks assign job link and is taken to a page with jobs along with checkbox and when desired job is chosen, and submit button clicked, I want a database to update "job_id" value in "handymen" database. How could that be done?
#extends('layouts.master')
#section('title', 'Assign Job')
#section('content')
#section('content')
<form action="{{url('assignjob')}}" method="POST">
{{ csrf_field() }}
#foreach ($jobs as $job)
<div>
<label>{{$job->name}}</label>
<input type='checkbox' value='{{$job->id}}' name='jobs[]'/>
</div>
#endforeach
<input type="submit" name="submitBtn" value="Assign Job">
</form>
#endsection
function search()
{
$skills = Skill::all();
return view('layouts/search',['skills' => $skills]);
}
function details($skillId)
{
$skill = Skill::find($skillId);
$handymen = $skill->handymen;
return view('layouts/details', ['skill' => $skill,'handymen' => $handymen]);
}
function assignJob($handymanId)
{
$assignJob = Hadnyman::find($handymanId);
$jobs = Job::all();
return view('layouts/assignjob',['jobs' => $jobs]);
}
function jobassign(Request $request)
{
return redirect('assignjob');
}
function skilledHandyman($handymanId)
{
$skilledHandyman = Handyman::find($handymanId);
return view('layouts/skilledHandyman', ['skilledHandyman' => $skilledHandyman]);
}
If a specific code is needed, please let me know
You should look at Eloquent Relationship.
Handymen has many Job
class Handymen extends Model {
...
public function jobs() {
return $this->hasMany(App\Job::class);
}
}
In your controller
function assignJob(Request $request, $id)
{
$handymen = Handyman::findOrFail($id);
// $request->get('jobs') = [1, 6, 7, etc...]
$handymen->saveMany($request->get('jobs'));
return ...;
}
Related
Before looking for a page I wanted to check if the id exists, so if I don't find it, give up looking I tried as follows:
My controller product
public function search(Request $request)
{
$id = $request->input('id');
if($produto = Produto::find($id)) {
return view('produtos.show', compact('produto', 'id'));
}
// $search_results=Produto::findOrFail($id);
return 'Not found';
}
->My Route->
Route::get('/produtos/editar/{id?}','App\Http\Controllers\ProdutosController#search')->name('searchproduct');
->My Blade Form
<form id="search" method="GET" action="{{ route('searchproduct') }}" >
<input id="q" name="q" type="text" /></br>
<button type="submit" id="submitButton" >Alterar</button>
</form>
</div>
</div>
</div>
</div>
->My Jquery Script
jQuery(document).ready(function(){
jQuery("form#search").on('submit',function(e){
e.preventDefault();
var q = jQuery("#q").val();
window.location.href = jQuery(this).prop('action')+"/" + encodeURIComponent(q)
});
});
How can i check in database before? send It's always going to the default 404 page
It's enough to check $search_results variable. I changed findOrFail with find because findOrFail may throw an error.
public function search(Request $request) {
$search_results = Produto::find($request->input('id'));
if ($search_results == NULL) {
abort(404);
}
return view('produtos.show', ['produto' => $search_results, 'id' => $request->input('id')]);
}
Also yo can use:
public function search(Request $request) {
$search_results = Produto::where('id', '=', $request->input('id'))->first();
if ($search_results == NULL) {
abort(404);
}
return view('produtos.show', ['produto' => $search_results, 'id' => $request->input('id')]);
}
Two ways to go about it:
exists:
if (Produto::where('id', $id)->exists()) {
$search_results=Produto::find($id);
}
findOr:
$search_results = Produto::findOr($id, function () {
// whatever you want to do if no record is found
});
to show a 404 page use this code :
public function search(Request $request)
{
//if not found it will trigger 404 not found page
$produto = Produto::findOrFail($id = $request->input('id'));
//otherwise it will return the view of produtos.show
return view('produtos.show', compact('produto', 'id'));
}
or you can use this code too to use a custom return
public function search(Request $request)
{
$id = $request->input('id');
if($produto = Produto::find($id)) {
return view('produtos.show', compact('produto', 'id'));
}
//otherwise return your error view or message
return 'Not found';
}
-> your route must be get not post
Route::get('/produtos/editar/{id?}','ProdutosController#search')->name('searchproduct');
-> no need for #csrf for get method
<form id="search" method="GET" action="{{ route('searchproduct') }}" >
<input id="q" type="text" /></br>
<button type="submit" id="submitButton" >Alterar</button>
</form>
I'm trying, implement the checked checkbox by is_active attribute in database.. if is_active true then checked.
<div class="flex items-center">
<input type="checkbox" class="mx-2" id="option1" wire:click="updateActive" wire:model="selectedItems" value="{{ $items_id->id }}" />
</div>
and also i'm implement when checkbox click then update is_active..
below my component code
public $selectedItems = [];
public $selectId = [];
public $items_id;
protected $listeners = ['updatedSelect'];
public function mount() {
$this->items_id = Carts::find($this->items_id);
$this->selectId = $this->items_id->id;
// $this->selectedItems = $this->items_id->is_active;
}
public function render() {
return view('livewire.checkbox-cart')->extends('layouts.app');
}
public function updateActive() {
if (!empty($this->selectedItems)) {
Carts::whereIn('id', $this->selectedItems)->update(['is_active' => true]);
$this->selectId = $this->selectedItems;
$this->emit('updateTotal');
$this->emit('updatedSelectAll');
} else {
Carts::whereIn('id', $this->selectId)->update(['is_active' => false]);
$this->emit('updateTotal');
}
// dd($this->selectedItems);
}
as you can see, i've been try to make default value of $this->selectedItems by is_active.. it's work, but when i'm uncheck displaying error message because int given instead array..
Any help with this would be greatly appreciated!
There are two mysql tables 1.seats (id,number), 2.reservedseats(id,seat_id,sceering_id).
I show all the seats of a specific sceering as checkboxes in show.blade:
{!!Form::model($screening,['method'=>'post', 'action'=>
['ReserveController#store',$screening->auditorium->id]])!!}
<input type="hidden" name="screening_id" value="{{$screening->id}}">
#foreach($seats as $seat)
<label class="checkbox-inline">
{!!Form::checkbox('seat_id[]',$seat->id,null)!!} Number: {{$seat->number}}
</label>
#endforeach
<div class='form-group'>
{!!Form::submit('Create Post',['class'=>'btn btn-primary '])!!}
</div>
{!!Form::close()!!}
When I click a checkbox it goes the the seat_id[] array. So I send a hidden input and an array with the seat_ids then I want to store in the reservedseats Mysql table. But in the store controller I have the problem. I'm trying something like:
public function store(Request $request){
$screening_id = $request->screening_id;
$seat_ids = $request->seat_id;
foreach($seat_ids as $seat_id){
Seatreserved::create($seat_id,$screening_id);
}
}
So it not working but how can I solve that?
Try this code
public function store(Request $request)
{
$screening_id = $request->screening_id;
$seat_ids = $request->seat_id;
foreach($seat_ids as $seat_id) {
Seatreserved::create([
'seat_id' => $seat_id,
'screening_id' => $screening_id
]);
}
}
Also you can use
public function store(Request $request)
{
$screening_id = $request->screening_id;
$seat_ids = $request->seat_id;
$data = [];
foreach($seat_ids as $seat_id) {
$data[] = [
'seat_id' => $seat_id,
'screening_id' => $screening_id
];
}
Seatreserved::insert($data);
}
That is better way to perform this as it will interact with database for once.
You can also create a new instance of your model to store values.
Example:
foreach($seat_ids as $seat_id) {
$reserved = new Seatreserved();
$reserved->seat_id = $seat_id;
$reserved->screening_id = $screening_id;
$reserved->save();
}
I have this 2 methods in the ParticipantController. The index() show some info of all registrations in a conference. Then the search() is for a search form in the view, to show only in the table the results where the user that did the registration has the name like the name introduced by the user in the search form:
class ParticipantController extends Controller
{
public function index($id){
$conference = Conference::find($id);
$conference->load('registrations.participants.answers.question');
return view('participants.index')->with('conference', $conference);
}
public function search($id, Request $request)
{
$name = $request->get('participant');
$conference = Conference::findOrFail($id);
$searchInfo = $conference->load([
'registrations.participants' => function ($query) use ($name) {
return $query->where('name', 'like', '%' . $name . '%');
},
'registrations.participants.answers.question'
]);
return view('participants.index', compact('conference'));
}
}
The routes that I have for this methods:
Route::get('conference/edit/{id}/participants', [ 'uses' => 'ParticipantController#index', 'as' => 'participants.index']);
Route::get('conference/edit/{id}/participants/search', [
'uses' => 'ParticipantController#search',
'as' => 'participants.search'
]);
My doubt is how to show the search results in the view after the user enter some data in the search form and click "Search". When the user acesses the participants/index.blade.php all registrations are already listed in a table with the foreach below using the code in the index() method:
#foreach($conference->registrations as $registration)
<tr>
<td>{{$registration->customer->name}} {{$registration->customer->surname}}</td>
<td>{{ $registration->status === 'C' ? 'Complete' : 'Incomplete' }}</td>
<td><a data-regid="{{$registration->id}}"> More details</a></td>
</tr>
#endforeach
And its working fine the table show the details of all registrations like:
User that did the registration Quantity Status Details
Jake K 2 Complete Show
...
But in this view there is also the search form, my doubt is how to show in the table the search results when the user introduces a name in the search form and click "Search".
HTML of the search form in the view:
<form action="{{ route('participants.search', ['id' => $conference->id] )}}" method="get">
<div class="form-row">
<div class="form-group col col-md-6">
<label for="participant">Search</label>
<div class="input-group">
<input type='text' name="participant" class="form-control" placeholder="Name" />
</div>
</div>
</div>
</form>
Change you search action like this
public function search($id, Request $request)
{
$name = $request->get('participant');
$conference = Conference::with(['registrations.participants' => function() ($query) use ($name){
return $query->where('name', 'like', '%' . $name . '%');
},'registrations.participants.answers.question'])->findOrFail($id);
return view('participants.index', compact('conference'));
}
Now if you look carefully both index and search has the same return type conference only the difference is in search we have filtered participants.
OR
you can just add the search functionality in index action too
public function index($id, Request $request){
$name = $request->get('participant');
if($name){
$conference = Conference::with(['registrations.participants' => function() ($query) use ($name){
return $query->where('name', 'like', '%' . $name . '%');
},'registrations.participants.answers.question'])->findOrFail($id);
}else{
$conference = Conference::with('registrations.participants.answers.question')->findOrFail($id);
}
return view('participants.index')->with('conference', $conference);
}
You can use datatables
Link : https://github.com/yajra/laravel-datatables
Check . this will solve your problem .
This would help you with what you need - https://datatables.net/examples/styling/bootstrap
I am trying to update and existing record in pivot table for many to many relationsip with laravel.
I have store function that work :
public function store(Request $request)
{
$produk = new product($request->input()) ;
$feat = (boolean)$request->input('featured');
$input = $request->all();
$input['featured'] = $feat;
$inputproduk = product::create($input);
$inputproduk->categories()->attach($request->input('kat_id'));
return redirect()->back()->with('message', 'Memasukkan Data Produk Berhasil');
}
But why my update function didn't work???
public function update(Request $request, $id)
{
$produk = Product::FindorFail($id);
$produk = new product($request->input()) ;
$input = $request->except('_method', '_token', 'picture', 'kat_id');
$inputproduk = product::whereId($id)->update($input);
$inputproduk->categories()->sync($request->input('kat_id'));
return redirect()->back()->with('message', 'Mengedit Data Produk Berhasil');
}
The error is :
Call to a member function categories() on integer
What is that mean? Please help me.
View :
<div class="form-group">
<label for="KategoriProduk">Kategori Produk</label>
<select class="form-control" name="kat_id[]" multiple>
#foreach($kategoris as $value)
<option value="{{$value->id}}">{{$value->namekat}}</option>
#endforeach
</select>
</div>
I only post partially because if I post it all then the question is just full of code. Tell me if you need more.
It's because of the following line.
$inputproduk = product::whereId($id)->update($input);
When you call update it will return an integer not a product object.
What you can do is first get the product and then update.
$inputproduk = product::whereId($id)->first();
$inputproduk->update($input);
$inputproduk->categories()->sync($request->input('kat_id'));