I have a datatable using yajrabox package with links to various strings. When I click on a link, it takes me to a 404 page with "/teams/"string". How do I make this string a viewable page? I have tried using a slug, but I could be using it incorrectly and honestly I don't know where my error is. Here is the impt. parts of my code:
Route:
Route::get('/teams/display/{$slug}', 'TeamsController#display');
TeamsController:
public function display($slug)
{
$teamdatas = LoserData::findBySlugOrFail($slug);
return view('teams/display', compact('teamdatas'));
}
DataTable.blade
{data: 'homeTeam', name: 'homeTeam', "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
$(nTd).html(""+oData.homeTeam+"");
}},
teams/display.blade.php
<div class="container">
<div class="row">
<div class="col-sm-8">
<h1> Welcome the {{$teamdatas->slug}} profile page</h1>
</div>
</div>
I guess I should have checked the routing docs because it was definitely a routing issue. To solve it put this in your routes:
Route::get('/teams/display/{teamdatas?}', 'TeamsController#display', function ($teamdatas = 'Oilers-84') {
return $teamdatas;
});
Here is the info from the Laravel Docs 5.5:
Optional Parameters
Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:
Route::get('user/{name?}', function ($name = null) {
return $name;
});
Route::get('user/{name?}', function ($name = 'John') {
return $name;
});
Related
On click I'm sending the id as data and then using query showing the name of user from WordPress database. I'm getting the response back from server but It is not adding when try to use .html(response).May be this is something to do with permission ?Like only admin can use the response?
If that's the case what I can do.
This is the ajax function:
function get_user_id() {
let get_current_user_id =jQuery(this).attr('id');
cur_user = '<?php echo get_current_user_id() ;?>';
var postdata_name = {action: "incoming_user_name_ajax_call",
param_user_to_chat: get_current_user_id,};
jQuery.post(ajaxurl, postdata_name, function (response) {
jQuery("#name-incoming-user").html(response);});
}
This is the function in functions.php
add_action("wp_ajax_incoming_user_name_ajax_call", "incoming_user_name_ajax_call_fn");
add_action("wp_ajax_nopriv_incoming_user_name_ajax_call", "incoming_user_name_ajax_call_fn");
function incoming_user_name_ajax_call_fn() {
global $wpdb;
$param_user_to_chat=isset($_REQUEST['param_user_to_chat'])?trim($_REQUEST['param_user_to_chat']):"";
if (!empty($param_user_to_chat)) {
$posts = $wpdb->get_results("SELECT distinct(display_name) FROM wp_users where
ID=$param_user_to_chat");
echo $posts[0]->display_name;
}
wp_die();}
Posting the HTML as well for everyone who want to know what jQuery(this).attr('id'); is doing. It is getting id "4" or "2" depending on click.
<div id="target">
<div class="user-list-hide" id="user-hide" style="display: block;">
<div id="4"><span>Jason Bradley</span>/div>
<div id="2"><span>John Saddington</span></div>
</div>
</div>
There was issue in your jquery code i.e : the way you were calling your get_user_id() function here you have use wrong selector to get your inner divs . So, to make it work change :
jQuery("#target").on("click", "div", get_user_id);
to
jQuery("#user-hide").on("click", "div", get_user_id);
I am trying to finer all active courses and here is what I've tried.
Middleware is called Status
public function handle($request, Closure $next)
{
$status = $request->status();
if($status == 'active'){
return view('admin.courses.index');
}
return $next($request);
}
My route calls the middleware
Route::get('admin/courses/?active', 'Admin\CoursesController#index')->middleware('status');
My view has a button that calls the route
<a href="{{ route('admin.courses.index') }}" style="margin-left:10px;" class="btn btn-success">
Active <span class="badge badge-light">{{$course_count_active}}</span>
<span class="sr-only">total courses</span></a>
This code is not filtering the records and I'm not sure what I'm doing wrong.
First of all the correct syntax for giving parameter is {parameter?}
Now second want to ask you did you register your middleware in kernel.php file (i guess you did but still need to confirm)
now your button giving no parameter to route
{{ route('admin.courses.index') }}
which means your middleware will run $next($request)
so if you need to see the view part(which you saying filter) , do it like that {{ route('admin.courses.index',['status'=>'active']) }}
now last give named route, if you don't give name route then you can't call route(), give name route like below
Route::get('admin/courses/{status?}', 'Admin\CoursesController#index')->name('admin.courses.index')->middleware('status');
and give parameter name status like above
Update
Another mistake you can't get parameter as you did.
$status = $request->status();
simply write below
$status = $request->status;
Update
So now you facing error setCooking on member function because you return view('someview') form middleware which is not a good practice at all.
in the middleware use redirect() to some route.
so I suggest you make a route in which you return view.
and redirect to this route from middleware will solve your problem.
After 2 days I realized I was missing the csfr token. Here is what I've modified and now it works.
Form - blade
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
In the Ajax script:
$.ajax({
url: url,
method: method,
data: {
"_token": $('#token').val(),
'item':$('#item').val(),
"description": $('#description').val(),
},
success: function(response) {
},
error: function(xhr) {
var error = xhr.responseJSON;
if ($.isEmptyObject(error) == false) {
$.each(error.errors, function(key, value) {
$('#' + key)
.closest('.form-group')
.addClass('has-error')
.append('<span class="help-block"><strong>' + value + '</strong></span>')
});
}
}
I'm bit stuck at a place. I've got some views of small HTML sections which when combined gives the complete HTML page. I'm trying to build a website builder with Jquery, where I'm having a drop event which adds those particular views:
For example I've got HTML for slideshow:
<div id="slideshow" data-nitsid="2">
<div class="revolution-slider">
<ul>
<!-- SLIDE -->
#foreach($contents->slider as $sliders)
<li data-transition="{{ $sliders->transition }}" data-slotamount="{{ $sliders->slotamount }}" data-masterspeed="{{ $sliders->masterspeed }}">
<!-- MAIN IMAGE -->
<img src="{{ URL::asset($sliders->url) }}" alt="">
</li>
#endforeach
</ul>
</div>
</div>
In my JQuery code:
nitsbuilder.dropeventhandler = function ($item, $position) {
var nits_id = $item.data('nitsid');
$.ajax({
method: 'POST',
url: dropurl,
data: { nits_id: nits_id, _token: token},
dataType: 'json',
success: function (data) {
nitsbuilder.adder($item, $position, data);
}
});
}
Before I was having html codes in the database so it was easier to pull out the html and add to the HTML page, now I'm having html in views, how can I push/include this HTML code or view to ajax request so that my nitsbuilder.adder function executes placing the view through my controller.
My present Controller is:
class DropeventController extends Controller
{
public function htmlcode(Request $request)
{
$pluginid = $request['nits_id'];
$code = Plugins::findOrFail($pluginid);
$htmlcode = $code->code;
return response()->json(['htmlcode' => $htmlcode]);
}
}
Please guide me. Thanks
You can easily create html strings from blade views using \View::make
e.g. let's assume you have the following folder strucutre
project
...
ressources
views
snippets
snippetA
snippetB
You could now create a route / controller accepting a "name" parameter and then do the following
$name = "snippetA"; // get this from parameters
$html = \View::make("snippets.$name")->render();
You might need to also add variables depending on your views
$name = "snippetA"; // get this from parameters
$errors = []; // error variable might be needed
$html = \View::make("snippets.$name", compact('errors'))->render();
You can then return this html string
return ['html' => $html];
And access it from your ajax done function.
I hope this helps
Suppose your html is in view file called abc.blade.php, you can return the rendered view from your controller in json.
return response()->json([
'htmlcode' => View::make('abc')->render();
]);
I'd like to add to my Laravel's blog app the ability to open a preview of a post in a new window/tab before saving it to the DB (like in Wordpress). Which is the best way to do it?
I've been reading a few posts about it, and for now I've tried something like this:
When I click the 'Preview' button in my create post view I make an ajax call to the store method, which will return a laravel's blade rendered view...
$('.preview-post').click(function () {
var formData = $('#post-form').serializeArray();
$.ajax({
method: 'POST',
url: '/admin/posts/store',
dataType: 'json',
data: formData
}).done(function (response) {
var w = window.open();
w.document.write(response);
});
});
Here's the method in my controller:
public function store(Request $request)
{
$title = $request->input('title');
$post = new Post();
$post->title = $title;
if ($request->ajax()) {
return response()->json(view('home.posts.ajax-post', compact('post'))->render());
}
}
As you can see, for the moment I've just included the title field, not the whole post, just to check if it works.
Finally, here's the view I'm returning and opening in a new window:
#extends('layouts.home')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="post-content">
{{$post->title}}
</div>
</div>
</div>
</div>
#endsection
So that's it. The problem is: it seems to work (I'm able to get the view in a new tab with the expected title I typed in the form field), but I guess I'm doing something wrong, because I have for example some animated svg elements that don't render in the proper way in the new window. I've also started with Laravel Framework like a month ago, so I don't know if I'm following the correct workflow to do something like this.
It's my first post in here and I haven't yet figured out to format my post properly yet, but here it goes.
So basically I can only get my code to work if i point directly to a php-file. If I try to call a method within my controller, nothing seems to happen.
My JavaScript:
$(document).ready(function() {
$(".guide_button").click(function(){
var id = $(this).text();
var data = {};
data.id = id;
$.getJSON("/guides/hehelol", data, function(response){
$('#test').text(response.id);
});
return false;
});
});
My markup:
<div id="content_pane">
<ul>
<li>RL</li>
<li>LG</li>
<li>RG</li>
<li>SG</li>
<li>GL</li>
<li>MG</li>
</ul>
</div>
<div class="description">
<h3>Description</h3>
<p id="test">This text area will contain a bit of text about the content on this section</p>
</div>
My Controller:
<?php
class Guides extends CI_Controller {
public function Guides()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
}
public function index()
{
$this->load->view('guides_view');
$title = 'Some title';
}
public function hehelol() //The controller I am desperatly trying to call
{
$id = $_GET['id'];
$arr = array ('id'=>$id);
echo json_encode($arr);
}
}
It might be my controller I have done something wrong with. As it is the code only works if create a hehelol.php file and refer to it directly like this.
$.getJSON("hehelol.php", data, function(response){
$('#test').text(response.id);
});
Anyone who knows what I need to do to make my controller work properly? Help please! :)
i just put your exact code in its entirety in my codeigniter app and it worked for me. Meaning I used this: ...$.getJSON("/guides/hehelol",...
Because you are making a $_GET request, you have to enable query strings.
In your config.php file, make sure this line is set to TRUE:
$config['allow_get_array']= TRUE;