I have wrong route in laravel - laravel

I get data from Search and when i want to store/create again, its show "pages not found"
Controller Search
public function searchGuest(Request $request)
{
$q = $request->input('q');
if ($q != "") {
$guests = Guest::where('guestsid', 'LIKE', '%' . $q . '%')
->whereIn('id_status', [2])
->paginate(5);
if (count($guests) > 0) {
$lokasis = Lokasi::all();
return view('guests.guestsId', compact('guests', 'lokasis'));
} else {
return view('guests.searchNotFound');
}
}
}
View
<form action="{{ action('GuestController#store') }}" method="post" id="myform">
{{ csrf_field() }}
<fieldset>
and when i click submit button show this page
but form already get data, but still /searchGuest, in this pict is must /guestsId right?

check if query returns any results otherwise check your query dd(DB::getQueryLog());

Related

How to make controller to insert time on click and remove time on another click?

This is my controller where i want to know what is correct.
public function complete($id)
{
$task = Task::find($id);
if($task->done_at = '' or $task->done_at = null) {
$task->done_at = date( 'Y-m-d H:i:s', strtotime( 'now' ) );
$task->save();
} elseif ($task->done_at !== null) {
$task->done_at = null;
$task->save();
}
return redirect()->route('tasks.index')->withSuccess('Done');
}
and this is my index page where there is form for clicking
<form action="{{ route('tasks.complete', $task->id) }}" method="post">
#csrf
<button type="submit" name="submit"><p style="border-radius: 50%; color:red"></p></button>
</form>
You can check with that using empty() function. and do not repeat yourself you can also use save function in last after if else condition.
Thanks :)

How to show POST data from external url api to my blade file

External site data to be displayed on my blade
MerchantCode
MerchantRefNo
Particulars
Amount
PayorName
PayorEmail
Status
RefNo
Here's my Controller
function getPaymentDetail()
{
$data = Http::get('https://external.com/api')->json();
return view('paymentdetails',['data'=>$data]);
}
I would code it like this.
In your controller.
function getPaymentDetail() {
$res = Http::get('https://external.com/api')->json();
$temp_data = json_decode($temp_data, TRUE);
foreach($temp_data as $key => $value) {
$data[$key] = $value;
}
return view('paymentdetails', $data);
}
In your blade file, you can access by {{ $MerchantCode }}

Array works fine on localhost but not working on live server (gives error message Undefined offset: 0) - Laravel-5.8

Everything works perfectly okay on localhost but when migrated to godaddy live server(cpanel) I keep getting this error (Undefined offset: 0) on my blade view
I have tested the application on my localhost using XAMPP running PHP 7.2.12 and it works very fine but now I moved it to godaddy cpanel running PHP 7.3 and it keeps giving me this error
//This is my Route
Route::get('/conversations', 'DoctorsController#Conversations');
//This is my Controller
public function Conversations(Request $request){
//authenticate user
if($request->us == 'guest'){
return redirect()->intended('login');
}else{
$unread=DB::table('messaging')
->where([
['Reciever', Auth::user()->id],
['ReadStatus', '=', '']
])
->get();
$pending=$unread->count();
//retrieve previous chat;
$conversations=DB::table('messaging')
->where('Sender', Auth::user()->id)
->orWhere('Reciever', Auth::user()->id)
->groupBy('Sender')
->orderBy('ReadStatus', 'asc')
->get();
//retrieve profile of users in the previous chat
$profiles = array();
$read_status = array();
foreach($conversations as $conversation){
if($conversation->Sender == Auth::user()->id){
//check user role to know which database to query
$userRole=DB::table('role_user')
->where('user_id', $conversation->Reciever)
->get();
if($userRole[0]->role_id === 2){
#retrieve the sender details from doctors table
$profile=DB::table('doctors')
->where('doctor_id', $conversation->Reciever)
->get();
}else{
//retrieve the sender details from users table
$profile=DB::table('profiles')
->where('user_id', $conversation->Reciever)
->get();
}
if(in_array($profile, $profiles)){
}else{
array_push($profiles, $profile);
}
//retrieve the reciever details
}else if($conversation->Reciever == Auth::user()->id){
//check user role to know which database to query
$userRole=DB::table('role_user')
->where('user_id', $conversation->Sender)
->get();
if($userRole[0]->role_id === 2){
$profile=DB::table('doctors')
->where('doctor_id', $conversation->Sender)
->get();
}else{
$profile=DB::table('profiles')
->where('user_id', $conversation->Sender)
->get();
}
//retrive unread chat;
$unreadconvers=DB::table('messaging')
->select('ReadStatus')
->where([
['Reciever', Auth::user()->id],
['Sender', $conversation->Sender],
['ReadStatus', '=', '']
])
->get();
if(in_array($profile, $profiles)){
}else{
$profile['unreads'] = $unreadconvers->count();
array_push($profiles, $profile);
//array_push($read_status, $unreadconvers->count());
}
}
$i++;
}
return view('conversations')->with(['profile'=>$profiles, 'pending'=>$pending, 'unreads'=>$read_status]);
//return to the conversation blade
}
}
//This is my Blade template
#foreach($profile as $profile)
<div class="col-md-4 element-animate">
<div class="media d-block media-custom text-center">
<img src= "{{ URL::to(isset($profile[0]->image) ? $profile[0]->image : '../img/user.png') }}" alt="Image Placeholder" class="img-fluid img-fluid-doctors">
<div class="media-body">
<a href="{{ isset($profile[0]->doctor_id) ? url('/chat-doctor?db='.$profile[0]->doctor_id) : url('/chat-doctor?us='.$profile[0]->user_id) }}" class="envelop"><i class="far fa-envelope"></i><span class="unread">{{ isset($profile['unreads']) ? $profile['unreads'] : 0 }}</span>
<h3 class="mt-0 text-black">{{ $profile[0]->name }}</h3>
</a>
</div>
</div>
</div>
#endforeach
At the Controller, this code is expected to retrieve all the messages from the database linking to the logged in user either send or received, store them using an array and display them at the blade template looping through each of the array.
Currently that is what it does on localhost but on live server I get this error message Undefined offset: 0 (View: /resources/views/conversations.blade.php)
You are overwriting the variable in your foreach loop, therefore on the second iteration, it's looping in the profile object instead of your original array.
Change your controller to:
'profiles' => $profiles,
And change your foreach to loop through $profiles instead:
#foreach ($profiles as $profile)
And replace your $profile[0] with $profile.
I have found the solution to this issue, I was using === instead of == where I have this code
if($userRole[0]->role_id === 2)
I now change this line of code to
if($userRole[0]->role_id == 2)
and now is working perfectly well.
Thank you for your response Chin Leung.

php search engine filter

So I have this simple search engine which works perfectly, but now I want the user to select a region to search in, I added a field in the database table called location which holds an id of the region for each datarow in the table and when the user inserts id of the region in the input field when searching, the specific result will be shown according to the id, but I ran into problems with queries as my query maybe incorrect, I tested, the so called filter works to some extent, but it shows the two erros Notice: Undefined index: userID in ... and Notice: Undefined index: name in ... which I know it is because of the way I query, so:
What am I doing wrong?
Is it possible to use one query which makes all the code work?
Thx everyone.
<form method="POST" action="">
<input type="text" name="search">
<input type="text" name="location">
<button type="submit" name="submit">search</button>
</form>
<?php
require_once 'db.php';
if(isset($_REQUEST['submit'])) {
$search = str_replace(array('%','_'),'',$_POST['search']);
if ($search){
if (isset($_POST["location"])){
$location= $_POST["location"];
$query = "SELECT * FROM shoplist WHERE name LIKE :search OR userID LIKE :search";
$much = $muc->prepare($query);
$much->bindValue(':search', '%' . $search . '%', PDO::PARAM_INT);
$much = $muc->prepare('SELECT location from shoplist WHERE location =:location');
$much->bindParam(':location', $location);
$much->execute();
if ($much->rowCount() > 0) {
$result = $much->fetchAll();
foreach( $result as $row ) {
$userID = $row['userID'];
$name = $row['name'];
$location= $row['location'];
}
}
}
}
}
?>
As always simple things are the correct answer:
changed this:
$query = "SELECT * FROM shoplist WHERE name LIKE :search OR userID LIKE :search";
$much = $muc->prepare($query);
$much->bindValue(':search', '%' . $search . '%', PDO::PARAM_INT);
$much = $muc->prepare('SELECT location from shoplist WHERE location =:location');
$much->bindParam(':location', $location);
to this:
$query = "SELECT * FROM shoplist WHERE location=:location AND (name LIKE :search OR userID LIKE :search)";
$much = $muc->prepare($query);
$much->bindValue(':search', '%' . $search . '%', PDO::PARAM_INT);
$much->bindParam(':location', $location);

redirect issue in codeigniter when using if statement

i have been trying to redirect the same previous page after delete or inserting the data using if condition in the controller to flash the message but there is something i am missing.
<form action="<?php echo base_url(); ?>curdler/add/tbl_category/addCat/category" method="post">
Category Title<input type="text" name="category_title"/> </br>
<input type ="submit" value="submit">
</form>
<?php echo $this->session->flashdata('msg'); ?>
Controller
public function add() {
$data = $_POST;
$tableName = $this->uri->segment(3);
$content = $this->uri->segment(4);
$folderName = $this->uri->segment(5);
$this->load->model('curdmodel');
if($this->curdmodel->add($data, $tableName)){
$this->session->set_flashdata('msg', 'Category added');
redirect('welcome/index/'.$content.'/'.$folderName);
} else{
$this->session->set_flashdata('msg', 'Category Not Added');
}
}
when using the if statement it goes to the different url but without if statement its working fine.
model
public function add($data, $tableName) {
$this->db->insert($tableName, $data);
}
Your redirect only occurs if the if statement evaluates to be true, if the condition is false codeigniter is just setting the flashdata and then ending the script.
Consider changing the order of your code to something like
if($this->curdmodel->add($data, $tableName)){
$this->session->set_flashdata('msg', 'Category added');
} else{
$this->session->set_flashdata('msg', 'Category Not Added');
}
redirect('welcome/index/'.$content.'/'.$folderName);
Additionally, your model does not contain any return value and therefore will never pass data back for the if statement to be evaluated. You should update your model as follows:
public function add($data, $tableName) {
$this->db->insert($tableName, $data);
if($this->db->affected_rows() > 0) {
return true;
}
}

Resources