Select Option fetch data to label using ajax in laravel - ajax

I tried fetching the data after selecting an accountnumber using ajax it did work but after selecting once I can't select again. I've been trying some solutions but it didn't work.
AccountController.php
public function index()
{
$AM= new AM();
$AM->setConnection('AM');
$key= '';
$getData = $AM->where('Accountnumber',Auth::user()->AccountNum)->get();
foreach($getData as $row){
$key= $row->key;
}
$getAMData = $AM->where('key',$key)
->select('Accountnumber')
->get();
$ci= $AM->where('Accountnumber',Auth::user()->AccountNum)
->select('Accountnumber', 'cn', 'ca','ct','accountstatus')
->get();
return view('account')->with('getAMData',$getAMData)->with('ci',$ci);
}
// Methods
public function fetch(Request $request){
if($request->ajax())
{
$AM= new AM();
$AM->setConnection('AM');
$getData = $AM->where('Accountnumber',$request->Accountnumber)->get();
foreach($getData as $row){
$key= $row->key;
}
$getAMData = $AM->where('key',$key)
->select('Accountnumber')
->get();
$ci= $AccountMaster->where('Accountnumber',$request->Accountnumber)
->select('Accountnumber', 'cn', 'ca','ct','accountstatus')
->get();
return view('partials._fetch')->with('getAMData',$getAMData)->with('ci',$ci);
}
}
ajax.js
$(document).ready(function(){
$('#accountnum').change(function(){
var Accountnumber = $(this).val();
$.ajax({
url:"/fetch",
method: "GET",
data: {Accountnumber:Accountnumber},
success:function(data){
$('#showbill').html(data);
}
});
});
});
account.blade.php
<div class="container-fluid col-sm-12">
<h3><b> Account </b></h3>
</div>
</div>
<div class="container-fluid py-4 col-sm-12" id="showbill">
#include('partials._fetch')
</div>
I think my problem would be on my controller, maybe the fetch method is not right. anyone can help me with this? Thank you!
_fetch.blade.php
<div class="row">
<div class="col-md-3 col-sm-12">
#if(count($getAMData) > 0)
<select class="form-control form-control-md" id="accountnum" name="accountnum">
<option value="" selected></option>
#foreach($getAMData as $row)
<option value="{{ $row->Accountnumber }}">{{ $row->Accountnumber }}</option>
#endforeach
</select>
#else
<p></p>
#endif
</div>
<div class="col-md-12 col-sm-12 py-4">
<div class="card">
<div class="card-body">
#if(count($ci) > 0 )
#foreach ($ci as $row )
<div class="col-md-6 col-sm-12">
<label>Account Number:</label> <label><strong>{{ $row->Accountnumber }}</strong></label>
</div>
<div class="col-md-6 col-sm-12">
<label>Name:</label> <label><strong>{{ $row->cn}}</strong></label>
</div>
<div class="col-md-6 col-sm-12">
<label> Address:</label> <label><strong>{{ $row->ca}}</strong></label>
</div>
<div class="col-md-6 col-sm-12">
<label>Type:</label> <label><strong>{{ $row->ct}}</strong></label>
</div>
<div class="col-md-6 col-sm-12">
<label>Account Status:</label> <label><strong>{{ $row->ct}}</strong></label>
</div>
#endforeach
#else
<p>NO DATA FOUND</p>
#endif
</div>
</div>
</div>
<div class="col-md-12 col-sm-12">
<table class="table table-responsive-md table-hover ">
<thead style="text-align: center;" class="bg-warning ">
<tr>
<th scope="col">(MW)</th>
<th scope="col">Morning (0001H to 1200H)</th>
<th scope="col">Afternoon (1201H to 1800)</th>
<th scope="col">Evening (1801 to 2400)</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"></th>
</tr>
</tbody>
</table>
</div>
</div>
I tried using the same concept with the paginate using ajax well it didn't turn out to be what I expected haha

Okay so here is my solution to this problem. Instead of including the Select Option I put it inside my account.blade.php so that it wont regenerate two select options.

Related

foreach showing two results in just one column - Laravel 9.x / Livewire

When I trigger foreach to show the products registered in each process, separated by line and column, it returns the result all in just one line.
Sorry about my English
Example:
ID
header 2
1525, 1523
Item 1, Item 2
what i want to show
ID
Name
1525
Item 1
1523
Item 2
Back End:
`
public function viewLicitacao($id) {
$licitacao = Licitacao::find($id); $this->showModalLicitacao();
$this->licitacao_id_processo = $licitacao->id;
$this->licitacao_destino = $licitacao->cliente_nome;
$this->licitacao_date_entrega = $licitacao->date_entrega;
}
Front End:
#if($isModal == true)
#foreach($licitacao as $processo)
<div class="modal fade" id="ModalLicitacao">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Licitação - {{ $processo->id; }}</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h4>Informações das licitações</h4>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label>N° do Processo: </label>
<input class="form-control" type="number" wire:model="licitacao_id_processo" placeholder="Informe o N° do processo" readonly/>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="destino">Destinatário: </label>
<input type="text" class="form-control" wire:model="licitacao_destino" placeholder="Informe o nome do destinatário" readonly/>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Data da entrega:</label>
<input type="text" wire:model="licitacao_date_entrega" class="form-control" placeholder="Data de entrega da mercadoria" readonly>
</div>
</div>
</div>
<h4>Produtos da licitação</h4>
<div class="row">
<table class="table table-hover text-nowrap">
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Preço Unit</th>
<th>Quantidade</th>
<th>Qt. Disponivel</th>
<th>Ações</th>
</tr>
</thead>
<tbody>
#forelse($processo->products as $key => $products)
<td>{{ $products->id_product }}</td>
#empty
<td>Nenhum produto foi cadastrado nessa licitação</td>
#endforelse
</tbody>
</table>
</div>
</div>
<div class="modal-footer justify-content-between">
<button type="submit" wire:click="closeModalLicitacao()" class="btn btn-default">Fechar</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
#endforeach
<!-- /.modal -->
#endif
The two tables have relationships
Modal ProductLicitacao:
class ProductLicitacao extends Model
{
use HasFactory;
protected $fillable = [
'id_product', 'licitacao_id', 'name', 'qty', 'price_unit',
];
public function licitacao()
{
return $this->belongsTo(Licitacao::class);
}
}
Modal Licitacao:
class Licitacao extends Model
{
use HasFactory;
protected $fillable = [
'processo_number', 'cliente_nome', 'status', 'user_id', 'date_entrega', 'alert',
];
protected $dates = [
'date_entrega'
];
public function products()
{
return $this->hasMany(ProductLicitacao::class, 'licitacao_id');
}
}
Help me to solve this problem that seems to be simples, but it is being monstrous for my iniation in web development.
Try adding the tag <tr> before the <td>
<tbody>
#forelse($processo->products as $key => $products)
<tr>// <-- here
<td>{{ $products->id_product }}</td>
//other columns(td), you may have
</tr>// <-- and here
#empty
<td>Nenhum produto foi cadastrado nessa licitação</td>
#endforelse
</tbody>

insert data not working perfectly in laravel 8

Here I am doing l laravel CRUD operation.
I have a table named scores.
Schema::create('scores', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('match_id');
$table->unsignedBigInteger('team_id');
$table->unsignedBigInteger('player_id');
$table->unsignedBigInteger('scoreupdate_id');
$table->unsignedBigInteger('outby_id');
$table->timestamps();
$table->foreign('match_id')->references('id')->on('matchhs')->onDelete('cascade');
$table->foreign('team_id')->references('id')->on('teams')->onDelete('cascade');
$table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
$table->foreign('scoreupdate_id')->references('id')->on('scoreupdates')->onDelete('cascade');
$table->foreign('outby_id')->references('id')->on('scoreupdates')->onDelete('cascade');
});
Where I want to store data from the different tables so I did this with my Score.php model
class Score extends Model
{
use HasFactory;
use HasFactory;
protected $fillable =['team_id','match_id','player_id','scoreupdate_id','outby_id','out_type',
'one','two','three','four','six'];
public function team(){
return $this->belongsTo(Team::class,'team_id');
}
public function matchh(){
return $this->belongsTo(Matchh::class,'match_id');
}
public function player(){
return $this->belongsTo(Player::class,'player_id');
}
public function scoreupdate(){
return $this->belongsTo(Scoreupdate::class,'scoreupdate_id');
}
}
And This to my ScoreController.php
public function index()
{
$data=Score::all();
$team=Team::all();
$match=Matchh::all();
$player=Player::all();
$scoreupdate=Scoreupdate::all();
return view('admin.manage.score.index',compact('data','team','match','player','scoreupdate'));
}
public function store(Request $request)
{
Score::insert([
'match_id' => $request->match_id,
'team_id' => $request->team_id,
'player_id' => $request->player_id,
'scoreupdate_id' => $request->scoreupdate_id,
'outby_id' => $request->outby_id,
]);
$notification = array('message'=>'Scoreupdate Inserted!','alert-type'=>'success');
return redirect()->back()->with($notification);
}
And This is my index.blade.php
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0">Score</h1>
</div><!-- /.col -->
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#teamModal">
+ Add New
</button>
</ol>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- Main content -->
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">All score list here</h3>
</div>
<!-- /.card-header -->
{{-- card body --}}
<div class="card-body">
<table id="example1" class="table table-bordered table-striped table-sm">
<thead>
<tr>
<th>SL</th>
<th>Match Name</th>
<th>Team Name</th>
<th>Player Name</th>
<th>Out type</th>
<th>Out by type</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach ($data as $key => $row)
<tr>
<td>{{ $key + 1 }}</td>
<td>{{ $row->matchh->match_name }}</td>
<td>{{ $row->team->team_name }}</td>
<td>{{ $row->player->player_name }}</td>
<td>{{ $row->scoreupdate->out_type }}</td>
<td>{{ $row->scoreupdate->out_by_type }}</td>
<td>
<a href="#" class="btn btn-info btn-sm edit"
data-id="{{ $row->id }}" data-toggle="modal"
data-target="#editModal"><i class="fas fa-edit"></i></a>
<a href="{{ route('score.delete', $row->id) }}"
class="btn btn-danger btn-sm" id="delete"><i
class="fas fa-trash"></a>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<!-- /.card-body -->
</div>
</div>
</div>
</div>
</section>
</div>
{{-- insert modal --}}
<!-- Modal -->
<div class="modal fade" id="teamModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Player Modal</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="{{ route('score.store') }}" method="Post">
#csrf
<div class="modal-body">
<div class="from-group">
<label for="player_name">Match Name</label>
<select class="form-control" name="match_id" required="">
#foreach ($match as $row)
<option value="{{ $row->id }}">{{ $row->match_name }}</option>
#endforeach
</select>
</div>
<div class="from-group">
<label for="player_name">Team Name</label>
<select class="form-control" name="team_id" required="">
#foreach ($team as $row)
<option value="{{ $row->id }}">{{ $row->team_name }}</option>
#endforeach
</select>
</div>
<div class="from-group">
<label for="player_name">Player Name</label>
<select class="form-control" name="player_id" required="">
#foreach ($player as $row)
<option value="{{ $row->id }}">{{ $row->player_name }}</option>
#endforeach
</select>
</div>
<div class="from-group">
<label for="out type">Out type</label>
<select class="form-control" name="scoreupdate_id" required="">
#foreach ($scoreupdate as $row)
<option value="{{ $row->id }}">{{ $row->out_type }}</option>
#endforeach
</select>
</div>
<div class="from-group">
<label for="out by type">Out by type</label>
<select class="form-control" name="outby_id" required="">
#foreach ($scoreupdate as $row)
<option value="{{ $row->id }}">{{ $row->out_by_type }}</option>
#endforeach
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<button type="Submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
The problem is that when I add not out in my out type box which is id(1) in scoreupdates table and add caught in my out by type box which is id(2) of my scoreupdates table
It's inserting and retrieving not out and (-) where both of them hold id(1) of scoreupdates table but I want that when I insert not out it will insert and retrieve not out and when add caught it will insert and retrieve caught.
scores table database
I think this could be wrong you are doing
<td>{{ $row->scoreupdate->out_type }}</td
<td>{{ $row->scoreupdate->out_by_type }}</td>
On both column, you are trying to retrieve data from scoreUpdate, but looking at your database it looks like they are two foreign columns and it should be some other relation from relation outby_id, not scoreupdate_id.
On model, its showing,
public function scoreupdate(){
return $this->belongsTo(Scoreupdate::class,'scoreupdate_id');
}
It's retriving data from scoreupdate_id not outby_id

Problem with Laravel CRUD (Edit Function)

Good day everyone. I'm quite new to new to Laravel and was doing some basic CRUD coding, I was able to code the add and view function, but I'm having a hard time with the edit and delete function.
I have 4 files, the master blade file, the web.php (routing), the blade file and the Form Controller.
This is the promo.blade.php file:
<table class="table table-striped" id="table1" >
<thead>
<tr>
<th class="text-center">ACTION</th>
<th class="text-center">Offer ID</th>
<th class="text-center">Promo Name</th>
<th class="text-center">Promo Price</th>
<th class="text-center">Status</th>
</tr>
</thead>
<tbody>
#foreach ($data as $key => $item)
<tr>
<td class="text-center">
<a href="#" data-bs-toggle="modal" data-bs-target="#show" data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
<span class="badge bg-success"><i class="bi bi-eye-fill"></i></span>
</a>
<a href="#" data-bs-toggle="modal" data-bs-target="#edit" data-mainid="{{$item->id}}" data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
<span class="badge bg-primary"><i class="bi bi-pencil-square"></i></span>
</a>
<a href="#" data-bs-toggle="modal" data-bs-target="#delete" data-myofferid="{{$item->offerId}}" data-mytitle="{{$item->promoName}}" data-myprice="{{$item->promoPrice}}">
<span class="badge bg-danger"><i class="bi bi-trash"></i></span>
</a>
</td>
<td class="date text-center">{{ $item->offerId }}</td>
<td class="date text-center">{{ $item->promoName }}</td>
<td class="number text-center">{{ $item->promoPrice}}</td>
<td class="number text-center">{{ $item->isActive }}</td>
</tr>
#endforeach
</tbody>
</table>
<!--Start Modal Edit -->
<div class="modal fade text-left" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel160" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable" role="document">
<div class="modal-content">
<div class="modal-header bg-primary">
<h5 class="modal-title white" id="add">
Edit Promo
</h5>
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
<i data-feather="x"></i>
</button>
</div>
<div class="modal-body">
<form action="{{ route('promo.edit') }}" method="POST">
#csrf
<div class="form-group">
<label for="">ID</label>
<input type="text" class="form-control" name="id" id="id" value="">
<span style="color:red">#error('id'){{$message}} #enderror</span>
</div>
<div class="form-group">
<label for="">Offer ID</label>
<input type="text" class="form-control" name="offerId" id="offerId" value="">
<span style="color:red">#error('offerId'){{$message}} #enderror</span>
</div>
<div class="form-group">
<label for="">Promo Name</label>
<input type="text" class="form-control" name="promoName" id="promoName" value="">
<span style="color:red">#error('promoName'){{$message}} #enderror</span>
</div>
<div class="form-group">
<label for="">Promo Price</label>
<input type="number" class="form-control" name="promoPrice" id="promoPrice" value="">
<span style="color:red">#error('promoPrice'){{$message}} #enderror</span>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light-secondary" data-bs-dismiss="modal">
<i class="bx bx-x d-block d-sm-none"></i>
<span class="d-none d-sm-block">CANCEL</span>
</button>
<button type="submit" class="btn btn-primary ml-1">
<i class="bx bx-check d-block d-sm-none"></i>
<span class="d-none d-sm-block">SAVE</span>
</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- End Modal Edit-->
Then this is the web.php file, for the routing:
Route::get('promo.promo', [App\Http\Controllers\FormControllerPromo::class, 'viewRecord'])->middleware('auth')->name('promo.promo');
Route::post('promo.add', [App\Http\Controllers\FormControllerPromo::class, 'addPromo'])->name('promo.add');
Route::post('promo.delete/{id}', [App\Http\Controllers\FormControllerPromo::class, 'viewDelete'])->middleware('auth');
Route::get('promo.edit', [App\Http\Controllers\FormControllerPromo::class, 'viewRecord'])->middleware('auth')->name('promo.edit');
Route::get('promo.edit/{id}', [App\Http\Controllers\FormControllerPromo::class, 'viewDetail'])->middleware('auth');
Route::post('promo.edit', [App\Http\Controllers\FormControllerPromo::class, 'edit'])->name('promo.edit');
This is the master.blade.php file:
<!--Start Modal edit for Promo-->
<script type="text/javascript">
$('#edit').on('show.bs.modal', function (event){
var button = $(event.relatedTarget)
var mainid = button.data('mainid')
var id = button.data('myofferid')
var title = button.data('mytitle')
var price = button.data('myprice')
var modal = $(this)
modal.find('.modal-body #id').val(mainid);
modal.find('.modal-body #offerId').val(id);
modal.find('.modal-body #promoName').val(title);
modal.find('.modal-body #promoPrice').val(price);
})
</script>
<!--End Modal edit for Promo-->
I think this is the part where the code wont execute properly.
This is the FormControllerPromo.php file:
// view form
public function index()
{
return view('promo.promo');
}
// view record
public function viewRecord()
{
$data = DB::table('promo')->get();
return view('promo.promo',compact('data'));
}
// view detail
public function viewDetail($id)
{
$data = DB::table('promo')->where('id',$id)->get();
return view('promo.promo',compact('data'));
}
// edit promo
public function edit(Request $request){
$id = $request->input('id');
$offerId = $request->input('offerId');
$promoName = $request->input('promoName');
$promoPrice = $request->input('promoPrice');
DB::table('promo')
->where('id', $id) // find your user by their email
->limit(1) // optional - to ensure only one record is updated.
->update(array('offerId' => $offerId, 'promoName' => $promoName, 'promoPrice' => $promoPrice)); // update the record in the DB.
$data = DB::table('promo');
return view('promo.promo',compact('data'));
}
I've been trying to code this for almost a week now with no success, any help is highly appreciated. :)
the update seems right, should work. but when you pass the $data variable to your view, you should call ->get(), because otherwise you return a query builder instance, that later raises the Undefined property error when trying to access {{$item->offerId}}.
change second last line in your example
//from this:
$data = DB::table('promo');
//to this:
$data = DB::table('promo')->get();
//or to this if you want to show only one record:
$data = DB::table('promo')->where('id', $id)->get();

Change return View to return Route on laravel search function

I am trying to correct my code of the searchresult function of my controller so my concern is that when I paginate the search result or make form validation, I get the following error message : The GET method is not supported for this route. Supported methods: POST.
so if you can help me, I want to change my code from returning the view to returning the route.
here is my code
Route:
Route::get('/post/searchpost', 'PostsController#index')->name('index');
Route::post('/post/search_result', 'PostsController#searchresult')->name('searchresult');
Blades
//index
<div class="posts">
<h1>Posts</h1>
<div class="sreach">
<form action="{{route('searchresult', app()->getLocale())}}" method="POST">
{{ csrf_field()}}
<div class="form-row">
<div class="form-group col-md-2">
<label class="lbl" for="cat_id">title</label>
<select class="form-control" id="cat_id" name="cat_id">
<option value="" selected></option>
#foreach ($categoriesas $cat)
<option value="{{$cat->id}}">{{$cat->cat_mame}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-2">
<label class="lbl" for="title">Post title</label>
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group col-md-2">
<label class="lbl" for="content">Post content</label>
<textarea class="form-control" id="content" name="content">
</textarea >
</div>
</div>
<center>
<button type="submit">search</button>
</center>
</form>
</div>
<table id="posts">
<thead>
<tr>
<th>{{__('main.title')}}</th>
<th>{{__('main.post_category')}}</th>
<th>{{__('main.content')}}</th>
</tr>
</thead>
<tbody>
#foreach($posts as $postkey => $post)
<tr>
<td>{{$post->title}}</td>
<td>{{$post->category->cat_name}}</td>
<td>{{$post->content}}</td>
</tr>
#endforeach
</tbody>
</table>
Blades
//searchresult
<div class="posts">
<h1>Posts</h1>
<div class="sreach">
//that make me search again in result page using the same form
<form action="{{route('searchresult', app()->getLocale())}}" method="POST">
{{ csrf_field()}}
<div class="form-row">
<div class="form-group col-md-2">
<label class="lbl" for="cat_id">title</label>
<select class="form-control" id="cat_id" name="cat_id">
<option value="" selected></option>
#foreach ($categoriesas $cat)
<option value="{{$cat->id}}">{{$cat->cat_mame}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-2">
<label class="lbl" for="title">Post title</label>
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group col-md-2">
<label class="lbl" for="content">Post content</label>
<textarea class="form-control" id="content" name="content">
</textarea >
</div>
</div>
<center>
<button type="submit">search</button>
</center>
</form>
</div>
<table id="posts">
<thead>
<tr>
<th>{{__('main.title')}}</th>
<th>{{__('main.post_category')}}</th>
<th>{{__('main.content')}}</th>
</tr>
</thead>
<tbody>
#foreach($details as $detailkey => $detail)
<tr>
<td>{{$detail->title}}</td>
<td>{{$detail->category->cat_name}}</td>
<td>{{$detail->content}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Controller
public function index()
{
$ctegories= Category::all();
return view('search')->with('posts', Post::all())->with('ctegories', $ctegories);
}
public function searchresult(Request $request)
{
$title= $request->title;
$content= $request->content;
$cat_id= $request->cat_id;
$posts= Post::where('title', 'like', '%' . $title. '%')
->whereHas('category', function(Builder $query) use ($sujet_id){
$query->where('id', $cat_id);
})
->orderBy('created_at','desc')
->get();
//for form dropdown categories
$categories= Category::all();
if(count ($posts) > 0)
{
//this what i want to change from view to route with those parameters
return view('searchresult')
->withDetails($posts)->withQuery($title)
->withCategories($categories);
} else {
return redirect()->route('index')->with('success','search not found');
}
}
So otherwise the function works perfectly and I get the data
i will appreciate your help thank you
You should use below method to return route with parameters:
return \Redirect::route('nameOfRoute', ['param1'=>'value1','param2'=>'value2',...])->with('message', 'Search found!!!');

checkboxes and icons disappear on search

I am using livewire components but when begin typing in the search input field, the checkboxes and icons in the component disappear, and they only reappear after refreshing the page. What could be the cause of this behaviour?
Blade view
<div>
<div class="row">
<div class="col-md-12 grid-margin stretch-card">
<div class="card">
<div class="card-header">
<div class="d-flex justify-content-between align-items-center flex-wrap grid-margin">
<div>
<h4 class="mb-3 mb-md-0">User Roles</h4>
</div>
<div class="d-flex align-items-center flex-wrap text-nowrap">
<div class="form-inline">
<div class="input-group mr-2 mb-2 mb-md-0 d-md-non d-xl-flex">
<input type="text" wire:model="search" class="form-control" placeholder="Search role...">
</div>
<div class="input-group mr-2 mb-2 mb-md-0 mt-3 d-md-non d-xl-flex">
<select wire:model="sortAsc" class="form-control form-control-s mb-3">
<option value="1">Ascending</option>
<option value="0">Descending</option>
</select>
</div>
<div class="input-group mr-2 mb-2 mb-md-0 mt-3 d-md-non d-xl-flex">
<select wire:model="perPage" class="form-control form-control-s mb-3">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
</div>
</div>
<a href="{{ route('create-role') }}" class="btn btn-success btn-icon-text mr-2 mb-2 mb-md-0">
<i class="btn-icon-prepend" data-feather="plus"></i>
Add Role
</a>
<button wire:click="deleteRoles" type="button" class="btn btn-danger btn-icon-text mb-2 mb-md-0">
<i class="btn-icon-prepend" data-feather="trash-2"></i>
Delete Role
</button>
</div>
</div>
</div>
<div class="card-body">
#if (count($roles) > 0)
<div class="table-responsive">
<table id="dataTableExample" class="table">
<thead>
<tr>
<th></th>
<th>#</th>
<th>Display Name</th>
<th>Description</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach ($roles as $role)
<tr wire:key="{{ $role->name }}">
<td>
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" wire:model="selectedRoles.{{ $role->id }}" class="form-check-input">
</label>
</div>
</td>
<td>{{ $role->id }}</td>
<td>{{ $role->display_name }}</td>
<td>{{ $role->description }}</td>
<td>{{ $role->created_at->diffForHumans() }}</td>
<td>
<a href="{{ route('edit-role', $role->name) }}" class="btn btn-primary btn-icon-text mr-2 mb-2 mb-md-0">
<i class="btn-icon-prepend" data-feather="edit-2"></i>
Edit
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
<div>{{ $roles->links() }}</div>
</div>
#else
<p>No user roles found.</p>
#endif
</div>
</div>
</div>
</div>
</div>
Corresponding livewire component
<?php
namespace App\Http\Livewire\Roles;
use Livewire\Component;
use App\Models\Role;
use Livewire\WithPagination;
class Index extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $selectedRoles = [];
public $search = '';
public $perPage = 10;
public $sortField = 'id';
public $sortAsc = true;
public function render()
{
return view('livewire.roles.index', [
'roles' => Role::search($this->search)
->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')
->simplePaginate($this->perPage)
])
->extends('layout.master');
}
public function createRole(){
return view('livewire.roles.create')
->extends('layout.master');
}
public function deleteRoles(){
Role::destroy($this->selectedRoles);
}
}
What could be causing this issue?
try changing this lines, actually I use this like livewire documentation
'roles' => Role::where('someColumn','like','%'.$this->search.'%')
orWhere('anotherColumn','like','%'.$this->searchTerm.'%')
->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')
->paginate($this->perPage)

Resources