Method Illuminate\Events\Dispatcher::find does not exist - php

Hi currently trying to link to a page that describes all details about an event from a page that has the list of events. When a user clicks on specific event on the event page, it will link to another page of the event's description.
I did learn a few ways from other questions on stackoverflow, but I dont have the clue what is going on with the error
Here are some of the code details
My Route file
//route to see the page to all events
Route::get('/event', [EventController::class, 'index']);
//route for individual events
Route::get('/event/{id}', function ($id) {
$myEvent = Event::find($id);
return view('event-desc', ['events' => $myEvent]);
});
event-desc is the page for a certain event's description
Here is the event.blade.php (all events) short sum up
#foreach($events as $event)
<li><a href="{{URL::to('/event/' . $event->id)}}"></li>
<p>{{$event->event_name}}</p>
#endforeach
and also what about the event-desc.blade.php (the event description page) ? how should i start and display the specific clicked event and display the data that is only to that specific event?
pretty new in laravel, perhaps a link to a video regarding this would be okay!

I would suggest placing the path to the model, which in most cases would look like
use App\Models\Event;
You should put it below
use Illuminate\Support\Facades\Route;
Hope this helps!

Related

Best way to keep pagination after editing a record

I'm very new to Laravel and I'm trying to make a crud application with a view for each db table that has a list with edit buttons on each record.
So far I have the paginated list which is working good and an edit view that is working as well.
The problem I'm facing is that whatever page I'm in I can click the edit button and edit the record successfully, but when I save the record I always go back to page 1.
I don't know and I'd like to know which is the correct (best) way to achive this.
Should I use session? Or maybe append the page parameter to the edit form action? Or else?
EDIT:
this is what edit method returns:
return redirect('/lyrics')->with('success', 'Lyric updated!');
Thank you for any help that points me in the right direction.
You can pass the page parameter to the edit page and then after the user update the record, you can redirect to the page you were in.
Something like this:
In the link where you call the edit page:
Edit
In your edit form:
<form>
... fields
<input type="hidden" name='page' value={{ app('request')->input('page') }}
</form>
Then in your update method at the controller
public function update($request)
{
// update
return redirect('homepage', ['page' => $request->page]);
}

How to pass variables and retrieve data from DB with jQuery Mobile, PHP and MySQL

I'm really new to jQuery so I need your professional help in solving some of the issues I have.
I want to create an application for checking out events.
User chooses type of events (choose_type.php) -> user chooses city (choose_city.php) -> user chooses event(choose_event.php) -> user sees event details (event.php)
I have a user interface which is done with jQuery Mobile. I have created it based on Restaurant Picker application here
All of the choices in these html files are displayed as <li> elements with href links.
PHP code for my index page is given below.
if (!isset($_POST['SUBMIT'])){ //ERROR: Undefined index
$typequery= "SELECT * FROM eventtype ";
$typeresult = mysql_query($typequery);
while($row=mysql_fetch_array($typeresult))
{ $typeid=$row['TypeID'];
$typeimg=$row['Image'];
$typename=$row['TypeName'];
?>
<li> <img src="<?php echo $typeimg?>"/><h3><?php echo $typename ?></h3></li>
<?php } } ELSE {} ?>
This reads all types and their image paths from the DB, however, when I click on a link to go to choose_city.php, I am returned a blank screen which only says UNDEFINED.
How should I continue working on this? I need to keep passing parameters like typeid to next query, and typeid,cityid to the next one in order to get to event details.
my database is structured like City --< Events , EventType ---< Events, Event (contains only PK and FK ) ---- Description.
Any guidance would be really helpful. Thank you in advance,

How to update an array in TWIG template?

Currently, I am using TWIG to display all my information(articles and comments). So, I first retrieve the articles and comments from my database and pass it into an array. This is then passed into my twig template. Then, I load each article and underneath each article I load in the comments of that article. These comments and the buttons associated with it(aka the new comment button) are all made in my macro.
The macro just gets the array that contains the comments that I need to display. It display the comments for the article and with each comment it makes a reply button, so the user can reply to any comment or start a new thread.
I have used AJAX and jQuery to store the new comments in my database without refreshing the page. However, I need to figure out a way to display the new comment without refreshing the whole page. Does anyone have any idea on how to do this in TWIG?
The problem I am facing with TWIG right now is how to post comments asynchronously. I load the array of the current database of comments initially when I load the page. However, if my database updates, how do I update my array with it. Any suggestions? I was thinking of grabbing my array whenever a new comment is added, which is why my question is "How to retrieve an array in TWIG template?", but I am open to other suggestions.
Your issues isnt a twig issue, twig only is a templating engine and is only used on the initial render of the page. What you should be doing is when you get a successful response from your ajax save is create a new DOM element using jquery and add it to the page using your same schema. Something like:
<script>
$.post(
TheUrlThatYouPostTo,
TheDataThatYouPassWithThePost,
function(returnData, textStatus){
//assuming that your return data is an array conatianing a boolean successful key
if(returnData.successful){
$('#YourCommentsWrapperElement').append('<div class="comment">#some other elements containing the posted values</div>');
}
}
);
</script>
Or if you want twig to generate the comment HTML for you and you dont want to have it hard coded into your javascript you can have the controller action that your are posting to with the AJAX post return a rendered version of the comment. Something like this:
#Your Controller file
public function ajaxPostAction(Request $request){
//Handle post data to create a new commentObject
return array(
'comment' => $commentObject
);
}
Then you create a view file for that action:
#your view for the controller action ajaxPost.html.twig
<div class="comment">
<h3>{{comment.author}}</h3>
<p>{{comment.content}}</p>
</div>
Then your javascript will just take the return data and append it to the existing list:
<script>
$.post(
TheUrlThatYouPostTo,
TheDataThatYouPassWithThePost,
function(returnData, textStatus){
//Now your return data is the HTML for the new comment so just append it to the rest
$('#YourCommentsWrapperElement').append(returnData);
}
);
</script>

Showing a lister when grid button clicked? (in Agile Toolkit)

I have a grid that have buttons in one of it's columns like this:
how can I show a lister or a new grid when the button clicked?
$grid=$page->add('Grid');
$grid->setModel('Tickets',array('subject','date','time','department','status','text'));
$grid->addColumn("button",'read_ticket_id','Read');
if($_GET['read_ticket_id']){
// this generates javascript to be executed on buttion click
//how can I show a lister or a new grid when the button clicked?
}
Check out examples in ATK4 Codepad.
http://agiletoolkit.org/codepad/gui/grid
Edit:
This is snippet from one of my pages. Maybe you can find it useful.
The idea behind this is that you actually generate JavaScript inside this IF statement and JavaScript then is sent back to your browser which then can make another request for something (reload existing object, create new, redirect to somewhere etc.)
...
if($_GET['ticket']){
// Join this report with selected ticket
$this->grid->model->addToTicket($_GET['ticket']);
// Reload
$this->js(null,array(
$x->js()->reload(),
$this->js()->univ()->successMessage('Successfully saved')
))->execute();
}
...
With $_GET['ticket'] you get ID of record in grid in which you clicked button "Add to Ticket". $x is some other object in this page, for example, some form, field, tab or other grid. With $this->grid->model you get reference to model associated with this grid and in that model I have custom action/method defined - addToTicket which do something with database.
You can also redirect to other page with $this->js()->redirect() or $this->js()->location() etc. Basically you can do whatever you want, but all of this need to generate JavaScript as result or instructions for your browser what to do next.
And don't forget to add ->execute() at the end! That will stop further parsing your page and will instantly generate JS response.
I found a good example for this question:
http://agiletoolkit.org/doc/grid/interaction
==========
$g=$p->add('Grid');
$g->setSource('user');
$g->addColumn('name');
$g->addColumn('surname');
$g->addColumn('button','info','More Info');
$g->dq->where('name is not null')->limit(5);
if($_GET['info']){
$g->js()->univ()->dialogURL('More info',
$this->api->getDestinationURL(
null,array(
'more_info'=>$_GET['info'],
'cut_object'=>'myform'
)))
->execute();
}
if($_GET['more_info']){
$f=$this->add('Form','myform');
$f->addField('readonly','name');
$f->addField('readonly','surname');
$f->setSource('user');
$f->setConditionFromGET('id','more_info');
}

Yii ajaxLink() only works once

I have a CListView that uses ajaxLink() in the _view file.
View (index.php)
<?php
Yii::app()->clientScript->registerScript('ajaxUpdate',
"
//javascript function to update the listview using ajax
function updateItemList(){
$.fn.yiiListView.update('itemList');
return false;
}
", CClientScript::POS_READY);
?>
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'id'=>'itemList',
)); ?>
partial (_view.php)
<?php echo CHtml::ajaxLink('Delete',array('libdbitems/delete','id'=>$data->id),
array('type'=>'POST','success'=>'function(){updateItemList()}'),
array('confirm'=>'Are you sure you want to delete this item?',
'id'=>'delete-'.$data->id)); ?>
The controller is basically just the default actionIndex() that is generated with Gii.
Here's the problem: when I click my Delete link the first time after a page load, it behaves as expected. After that, clicking Delete does nothing. (It refreshes the ListView, but no changes are made.)
I'm pretty sure the problem lies in how Yii is binding click() events to my links in javascript, but I have no idea how to fix it. I have tried using the live=true option as others have suggested, but it does nothing.
Does anyone know how to fix this issue so that my Delete link works multiple times without having to reload the page?
Is your delete link part of the itemlist that's refreshed? If that's the case, the script won't re-register with the new link when a new link is created.
Two options:
1) Make sure your link is not being refreshed, and is a permanent aspect of the page
2) Write a custom jQuery handler rather than using Yii's ajaxLink. You'll need to use .on and delegated events. Something of the form:
$("#parentContainer").on("click", ".deleteLinkClass', updateItemList)
Where the parentContainer is a permanent item on the page, and deleteLinkClass will be a class you'll need to assign to the delete links you're using.
Eh, a stupid fix. I realized I had accidentally left a CHtml::$liveEvents = false in my controller that I put there while I was still earlier in the troubleshooting phase.
The solution is just to leave CHtml::$liveEvents = true (default) and to make sure all the links have unique IDs.

Categories