how to clear cache search results from server in laravel 5? - laravel-5

i'm new to laravel and i've created a simple search to query my database.
the problem is that everytime i refresh the page all the results from my last search are still showing. also when i load the page after couple of days,results are the. it seems like there is a cache working which i dont know about.
i want to clear these results/cache without clearing the cache for all my app.
Here is the code.
in my routs file:
Route::get('men',function()
{
$query=Request::get('q');
if ($query)
{
$users= \App\User::where('first_name','LIKE',"%$query%")
->orwhere('last_name','LIKE',"%$query%")
->get();
}
else
{
}
return view('page')->withUsers($users);
});
My View:(page.blade.php)
{!! Form::open(['method' => 'GET']) !!}
<div class="container col-lg-6 form-group" style="float:none;margin:auto">
<div class="input-group">
{!! Form::input('search','q', null, ['placeholder' => 'search' ,
'class' => 'form-control', 'autocomplete' => 'off']) !!}
<div class="input-group-btn">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Search</button>
</div><!-- /btn-group -->
</div><!-- /input-group -->
</div>
{!! Form::close() !!}
<div class="container col-lg-6" style="float:none;margin:auto">
#if ($users->count())
<ul class="list-group" style="list-style-type: none;">
#foreach($users as $user)
<li class="list-group-item" style="cursor:pointer">
{{$user->first_name}} {{$user->last_name}}
</li>
#endforeach
</ul>
#else
<h3>Sorry,no users found...</h3>
#endif
</div>
</div> <!--container-->
any Ideas ??..

i've managed to clear last query string in the browser addess bar with:
within my view:
<head>
<script type="text/javascript">
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
</script>
</head>
whenever i refresh the page it clears up the results since nothing is coming from the server.

Related

Cannot access files uploaded by Dropzone.js in Laravel Controller

There is a model called Image and it has a file, title, photographer and etc. I need to let the users to upload multiple images, then display the form of other fields for each image. So I chose Dropzone and I need to do it without Ajax upload. This is the form
#extends('layouts.layout')
<link rel="stylesheet" href="{{asset('css/dropzone.css')}}" />
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12 text-right">
<div class="widget">
<div class="widget-header">
<span> Multi Image Upload </span>
<i class="icon-picture mr-2"></i>
</div>
<div class="widget-content">
#include('layouts.message')
<SECTION>
<DIV id="dropzone">
{!! Form::open([ 'route' => [ 'images.multiUploadStore' ], 'files' => true, 'enctype' => 'multipart/form-data', 'class' => 'dropzone py-5 px-1 text-center w-100', 'id' => 'image-upload' ]) !!}
<DIV class="dz-message needsclick" id="demo-upload">
Drag and Drop Your Files Here
</DIV>
<div class="mx-auto text-center clearfix col-sm-12">
{{Form::submit('Submit',['class'=>'btn btn-primary','name'=>'submit', 'id'=>'submit'])}}
</div>
{!! Form::close() !!}
</DIV>
</SECTION>
</div>
</div>
</div>
</div>
</div>
#endsection
#section('scripts')
<script src="{{asset('js/dropzone.js')}}"></script>
<script>
Dropzone.options.imageUpload = {
maxFilesize : 1,
acceptedFiles: ".jpeg,.jpg,.png,.gif"
};
</script>
#endsection
and this is the controller
public function multiuploadstore(Request $request)
{
$images = $request->file('file');
dd($images);
}
and it returns null. How can I access files inside the controller?
Thanks in advance.

Link Button Not Working in Laravel

I want to edit a record in a CRUD application in laravel where I have a button which has been linked to go to the index view but when I click it, it redirects me to the UPDATE method of the controller.
This is my form:
{!! Form::open(['route' => ['players.update', $player->id], 'method' => 'PUT', 'files'=>'true']) !!}
<div class="row col-md-10 col-md-offset-1 panel">
<div class="col-md-8 col-md-offset-2">
<br />
<div class="form-group">
{{ Form::label('name', 'Player Name') }}
{{ Form::text('name', $player->name, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('file', 'Upload Image') }}
{{ Form::file('pic') }}
</div>
<div class="form-group">
{{Form::button('Save Record', ['type' => 'submit', 'class' => 'btn btn-success'])}}
{!! Form::close() !!}
<a href="{{ route('players.index') }}">
<button class="btn btn-danger" >Cancel</button>
</a>
</div>
</div>
</div>
I have the following button for going back to the index page but this is taking me to the UPDATE method of the controller:
<a href="{{ route('players.index') }}">
<button class="btn btn-danger" >Cancel</button>
</a>
This is my index method in the controller:
public function index()
{
$players = Player::paginate(5);
return view('players.index', compact('players'));
}
This is the UPDATE method in the controller:
public function update(Request $request, $id)
{
return "Hi";
}
This is my route file contents:
Route::resource('news', 'NewsController');
Route::resource('competition', 'CompetitionsController');
Route::resource('players', 'PlayersConroller');
Everything looks fine to me but I don't know what goes wrong here.
Any help is appreciated in advance.
I am not sure if it will solve your issue, try to put your button code outside the form-group div.
You can change your code as
Cancel
You can check your html you have put button inside form tag which of type submit that's why it is submitting the form again.
Replace your form code with:
<div class="row col-md-10 col-md-offset-1 panel">
<div class="col-md-8 col-md-offset-2">
{!! Form::open(['route' => ['players.update', $player->id], 'method' => 'PUT', 'files'=>'true']) !!}
<br />
<div class="form-group">
{{ Form::label('name', 'Player Name') }} {{ Form::text('name', $player->name, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('file', 'Upload Image') }} {{ Form::file('pic') }}
</div>
<div class="form-group">
{{Form::button('Save Record', ['type' => 'submit', 'class' => 'btn btn-success'])}}
</div>
{!! Form::close() !!}
</div>
<a href="{{ route('players.index') }}">
<button class="btn btn-danger">Cancel</button>
</a>
</div>

My view displays raw blade code

I'm new to Laravel and using ver 5.5. I have a simple view and when I run the code, instead of showing the rendered version, I see the Blade commands.
This is the view code:
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>{{ $title }}</h2>
</div>
<div class="pull-left">
{!! \App\Combine\BaseCombine::tableHeader($collection) !!}
{!! \App\Combine\BaseCombine::tableData($collection) !!}
{!! \App\Combine\BaseCombine::tableFooter($collection) !!}
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('transaction.index') }}"> Back</a>
</div>
</div>
</div>
#endsection
Here is the code in the controller that executes the view:
return view('general.genericTable',
[
'title' => __FUNCTION__,
'transactionID' => $transactionsID,
'type' => $type,
'collection' => TransactionCombine::agents($transactionsID, $type),
]
);
This is what I see when I execute the code:
#section('content')
{{ $title }}
{!! \App\Combine\BaseCombine::tableHeader($collection) !!} {!! \App\Combine\BaseCombine::tableData($collection) !!} {!! \App\Combine\BaseCombine::tableFooter($collection) !!}
Back
#endsection
What have I done wrong?

Laravel allow links on message

I need allow links in messages, when user send message. How I can do this?
#foreach($mess as $message)
<li data-mid="{{ $message->id }}">
<div class="row margin-left-none margin-right-none">
<div class="col-md-2 padding-right-none">
<div class="avatar-user text-center">
<img src="{{ $message->sender->avatar }}" alt="$message->sender->name">
</div>
</div>
<div class="col-md-10 padding-left-none">
<div class="user-name">
<span>{{ $message->sender->name }}
#if(Helper::getOnlineUser($message->sender->id))
<i data-toggle="tooltip" title="Онлайн" class="material-icons online">fiber_manual_record</i>
#else
<i data-toggle="tooltip" title="Оффлайн" class="material-icons offline">fiber_manual_record</i>
#endif
<span class="date float-right">{{ $message->created_at->formatLocalized('%H:%m') }}</span></span>
</div>
<div class="message">
{{ $message->body }}
</div>
</div>
</div>
</li>
#endforeach
This is view of messages.
How I can allow links?
Example: http://example.com in messages =>
http://example.com
I need {!! $message->body !!} ? Or How?
In controller:
$mess = Chat::conversations($conversation)->for($user)->getMessages($limit, $request->page);
I use this package: https://github.com/musonza/chat
There are built in PHP functions to handle this:
<div class="message">
{!! strip_tags($message->body, '<a>') !!}
</div>
Where the second argument is your whitelist.
This has the following advantages:
You're not messing with regex
You're still using blade {!!!!}
You're using well tested code dev'ed by the core team
You can do it by using following php function in your blade,
<?php
function getDataWithURL($text){
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, "".$url[0]." ", $text);
} else {
// if no urls in the text just return the text
echo $text;
}
}
?>
You just need to add this code in your blade file and then you can check and replace the links in your text like this,
<div class="message">
<?php getDataWithURL($message->body);?>
</div>
I hope you will understand.

Search result breaks delete action

I have a page where I display campaigns. I wont show all the code but the basic structure is like so
#foreach ($campaigns as $campaign)
{!! Form::open(array('class' => 'form-inline delete', 'method' => 'DELETE', 'route' => array('campaigns.destroy', $campaign->id))) !!}
<div class="panel panel-default">
#if (!empty($campaign->campaignName))
<div class="panel-heading campaignPanelHeading">
<h4>{{ $campaign->campaignName }}</h4>
</div>
<div class="panel-footer">
<a href="{{ route('campaigns.destroy', $campaign->id) }}" class="btn btn-danger" id="deleteCampaign" data-method="delete" data-token="{{ csrf_token() }}">
Delete
</a>
</div>
#endif
</div>
{!! Form::close() !!}
#endforeach
If I try to delete an item, the following is triggered.
$("#deleteCampaign").on("submit", function(){
return confirm("Do you want to delete this item?");
});
Now on this page where I display all campaigns I have a search box. You start typing and an autocomplete list displays. When you select an option, this is triggered
select: function (event, ui) {
$.ajax({
url: "/returnDataForCampaigns",
type: "GET",
datatype: "html",
data: {
value : ui.item.value
},
success: function(data) {
$('.container').html(data.html);
$('.selectpicker').select2();
}
});
},
This essentially calls a function which gets the selected Campaign, and injects it into the following partial
#if(!empty($campaign))
{!! Form::open(array('class' => 'form-inline delete', 'method' => 'DELETE', 'route' => array('campaigns.destroy', $campaign->id))) !!}
<div class="panel panel-default">
#if (!empty($campaign->campaignName))
<div class="panel-heading campaignPanelHeading">
<h4>{{ $campaign->campaignName }}</h4>
</div>
<div class="panel-footer">
<a href="{{ route('campaigns.destroy', $campaign->id) }}" class="btn btn-danger" id="deleteCampaign" data-method="delete" data-token="{{ csrf_token() }}">
<span class="glyphicon" aria-hidden="true"></span>
Delete
</a>
</div>
#endif
</div>
{!! Form::close() !!}
#endif
Finally, this data is then injected into the page's container. Now this all works fine. When I check the source after it has been injected everything looks correct.
I have 2 other buttons which I removed above which show or edit the campaign, these work fine. The thing that is not working is the delete button for the searched campaign. For some reason when I click this it goes to the campaigns show page. This button works when I display all campaigns, its only when search is performed it does not work.
I have checked the code for the delete button for when all campaigns are displayed vs a searched campaign. Everything is the same apart from the Javascript which has been applied to the delete button when all campaigns are shown as well as some hidden inputs
<a data-token="dsfsd" data-method="delete" id="deleteCampaign" class="btn btn-danger" onclick=" if ($(this).hasClass('action_confirm')) { if(confirm($(this).data('message') || "Are you sure you want to do this?")) { $(this).find("form").submit(); } } else { $(this).find("form").submit(); }">
Delete
<form style="display:none" method="POST" action="http://localhost:8000/campaigns/43">
<input type="hidden" value="delete" name="_method">
<input type="hidden" value="dsfsd" name="_token">
</form>
</a>
This is a searched button
<a data-token="dsfsd" data-method="delete" id="deleteCampaign" class="btn btn-danger" href="http://localhost:8000/campaigns/9">
Delete
</a>
So my main question is why this may be happening? I would also like to try and find out why the searched version of the delete button also takes you to the show page?
Any advice appreciated
Thanks
Try this for your delete jquery code:
$(document).on("submit", "#deleteCampaign", function(e){
e.preventDefault();
return confirm("Do you want to delete this item?");
});
Its because you are adding content after DOM load.

Resources