When I click my ticket it is not opening the specified page. While clicking the link of my ticket it shows in the link the correct ticket id, but the page is not opening. The error is:
404 not found
Ticket.blade.php
<tr>
#foreach ($ticketsinfos as $ticketinfo)
<td>IR-173049</td>
<td>Dito</td>
<td>{{ $ticketinfo->companies->name }}</td>
<td>{{ Str::limit($ticketinfo->ticket_title, 50, '...') }}</td>
<td><button class="btn btn-danger btn-sm" type="button">Action Needed<br></button><br></td>
<td>Tako Kiknadze</td>
<td>{{ $ticketinfo->created_at }}</td>
<td>{{ $ticketinfo->updated_at }}</td>
</tr>
#endforeach
</tr>
web.php
<?php
use App\Http\Controllers\AdminsUserController;
//use App\Http\Controllers\UserController;
//use App\Http\Controllers\CompaniesController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\TicketsController;
//use App\Models\AdminsUser;
//use App\Models\Companies;
use Illuminate\Support\Facades\Route;
/*
|----------------------------------------------z----------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::resource('/dashboard', DashboardController::class);
Route::resource('/tickets', TicketsController::class);
Route::resource('/admin/users', AdminsUserController::class);
// Route::resource('/companies', CompaniesController::class);
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Controller
public function show(Tickets $tickets)
{
$tickets = Companies::with('tickets')->get();
$severities = Severities::with('severity')->get();
$ticketsinfos = Tickets::with('companies')->findOrFail(2);
return view('customer.index', compact($tickets))->with(['tickets' => $tickets])->with(['severities' => $severities])->with(['ticketsinfos' => $ticketsinfos]);
//dd($ticketsinfos->toArray());
}
When I use 'dd' it works.
I think the problem is with your return in controller
try this
return view('customer', compact('tickets', 'severities', 'ticketsinfos'));
Your having a problem because there is no defined route tickets/show/{{ $ticket->id }}. Since you are using route resource if you want to show the ticket with the given id you need to use tickets/{{ $ticket->id }} or using the route() helper. `{{ route('tickets.show', $ticket->id) }}
Tip : Its easier to use the route() helper than using the the raw url of the path when accessing named routes, since you will not be confused when passing a parameter for the route. Example {{ route('tickets.show', $ticket->id) }} will generate the url as tickets/1
Related
I am working on a web programing assignment for University and have hit a snag when trying to make a like and dislike function for a comments area of a guestbook.
Evectively the numbers on the site should update by one once either the dislike or like button is pressed.
My comments display correctly however the liking function is broken and I have no idea how to fix it.
Here is my code
Controller (LikesController.php)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class LikesController extends Controller (It says my Like Controller is never used not sure why this is)
{
public function likepl (Comment, $comment) { (Says that Comment is "Undefined" also not sure why this is)
$comment -> likePlus (); (Method likePlus not found)
return redirect () -> action ('CommentController#index');
}
}
Model (comment.php)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public static function paginate(int $COMMENTS_PER_PAGE)
{
}
public function likePlus()
{
$this -> likes += 1;
$this -> update ();
}
public function dislikePlus()
{
$this -> dislikes += 1;
$this -> update ();
}
}
View Page (comment.comments)
#extends('setup')
#section('content')
<div class="container main-table">
<div class="box">
<h1 class="title">Guest book Comments</h1>
#if (count ($comments) > 5)
<table class="table is-striped is-hoverable">
<thead>
<tr>
<th>User</th>
<th>Comment</th>
<th>Date</th>
<th>Likes</th>
<th>DisLikes</th>
</tr>
</thead>
<tr>
#foreach ($comments as $c)
{{--declaring the comments--}}
<tr>
<td>{{ $c -> user }}</td>
<td>{{ $c -> comments }}</td>
<td>{{ $c -> created_at -> format ('D jS F') }}</td>
<td>{{ $c -> likes }}</td>
<td>{{ $c -> dislikes }}</td>
</tr>
<tr class="icon heart">
<td><a class="button" href="/comment/{{ $c -> id }}/like/"><ion-icon name="md-heart"></ion-icon></a></td>
<td><a class="button" href="/comment/{{ $c -> id }}/dislike/"><ion-icon name="md-heart-empty"></ion-icon></a></td>
</tr>
#endforeach
</table>
{{ $comments -> links() }} {{-- Setting pagination--}}
#else
<div class="notification is-info">
<p>
The Guest book is empty. Why not add a comment?
</p>
</div>
#endif
</div>
</div>
#endsection
I have annotated the code with the visible errors that I get as for better refference.
Here are the Routes that I am using for the comments page.
Route::get('/','CommentController#index');
Route::get('/comment/{comment}/like/', 'LikesController#likePl');
Route::get('/comment/{comment}/dislike/', 'DislikesController#dislikePl');
The update() function does not work the way you are using it. You either update the property and use the save() function, or you pass an array to the update function.
// 1.
$this->likes += 1;
$this->save();
// 2.
$this->update([
'likes' => $this->likes + 1,
]);
Update: Even better would be to use the built-in increment() function:
$this->increment('likes');
This way there is no need to know the number of likes beforehand.
Can we please work on the following first.
LikePl function name should match the name on the router (action#method)
The method inside the controller accept request like so
public function likePl (Request $request) { //code here }
Methods inside the Model are accessed using their namespace like so
App\Comment::likePlus() or you can import the namespace like "use App\Comment" there after inside the method just simply do "Comment::likePlus()".
Please refer here Laravel models for more information.
Fixing this will remove the errors you currently have. Use "dd($request->all())" inside your likePl method to check if you are getting the values you expect.
Hope this help a bit...
I'm getting an error undefined variable when I'm trying to get username ($user->name) from User model using id which is foreign key ($feedback->user_id) of Feedback model.
#php
use App\Feedback;
use App\User;
$feedbacks = Feedback::all();
#endphp
<!DOCTYPE html>
<html>
<body>
ADMIN DASHBOARD | LOGOUT<br><br>
<h3>Feedbacks</h3>
<table border="1">
<tr><th>ID</th><th>Left By</th><th>Feedback</th></tr>
#foreach ($feedbacks as $feedback)
<tr>
<td>{{ $feedback->id}}</td>
<td>{{ $user->name }}</td>
<td>{{ $feedback->feedback }}</td>
</tr>
$uid=$feedback->user_id;
$user= User::find($uid);
#endforeach
</table>
</body>
</html>
I would suggest creating a relation between feedback and user (If you don't have one already). Your relation would be put within the Feedback model and look like this:
// Feedback.php
public function user() {
return $this->belongsTo('App\User');
}
This will relate the feedback to the user using the user_id on the feedback table.
After this, when calling feedback within your controller, you can then eager load the user relation with each feedback. This can be done within the following:
$feedbacks = Feedback::with('user')->get();
Finally, within your template, you will able to call the user through each feedback by doing the following:
{{ $feedback->user->name }}
Note: This example assumes name is a field on your user table.
So i have created 2 models 'Team' & 'Match', 1 controller 'MatchController' and a view 'matches/index.blade.php'.
The Team model has an ID & name.
The Match model has an ID, homeTeam_id & awayTeam_id.
The MatchController has an index method.
The view shows all the matches in de database correctly, but with the homeTeam_id, what I would like is show the name for the teams, from the Team model.
How do i do that? This is what i have now in my view:
#foreach ($matches as $key => $value)
<tr>
<td>{{ $value->id }}</td>
<td>{{ $value->date }}</td>
<td>{{ $value->homeTeam_id }}</td>
<td>{{ $value->awayTeam_id }}</td>
</tr>
#endforeach
You can create two relationships between Team and Match models:
public function homeTeam()
{
return $this->belongsTo('App\Team', 'homeTeam_id', 'id');
}
public function awayTeam()
{
return $this->belongsTo('App\Team', 'awayTeam_id', 'id');
}
And then load the data:
$matches = Match::with('homeTeam', 'awayTeam')->get();
To display team name do this:
{{ $value->homeTeam->name }}
In the following code I used {!! URL::route('editCatForm',['id'=>$row->id]) !!} to go to named route editCatForm with query string ?id=5 or whatever that comes dynamically on $row->id
#foreach($categories as $row)
<tr>
<td>{{ $count++ }}</td>
<td>{{ $row->category_name }}</td>
<td>{{ $row->category_status }}</td>
<td>edit / delete</td>
</tr>
#endforeach
My route for this is
Route::get('editCatForm/{id?}',array('uses'=>'Categories#editCat','as'=>'editCatForm'));
but still it shows url like
http://localhost/projects/brainlaratest/editCatForm/2
instead of
http://localhost/projects/brainlaratest/editCatForm?id=2
The route points to function
public function editCat($id)
{
$catEdit = Category::find(Input::get('id'));
$categories = $this->getCat();
return view('categoriesAddForm',compact('categories','catEdit'));
}
What may be the issue that query string isn't working here?
Format of your url is editCatForm/{id?} so if you provided id it will try to replace {id} with your number and you will get editCatForm/5.
Problem is in your controller action. function editCat($id) already takes $id from route - you should replace Input::get('id') with just $id.
URL::route(...) can be replaced by just helper function route(...).
If you want get rid of /id you can remove {id} from your route and then route(...) will just add ?id=5 instead of /5. You would have to remove $id argument from function and get id by Request::input('id');.
This is how route() function is supposed to work.
If you insist on having the query string then you need to append the ?=2 to the URL manually and you cannot do routing based on this.
One way of building the query string is like this
$params = array('id' => 5);
$queryString = http_build_query($params);
URL::to('projects/brainlaratest/editCatForm?'.$queryString )
The Routing is correct. Your problem is to getting the $id in the action.
Since, you have passed $id in route parameter, you can capture the segment $id inside your action.
public function editCat($id)
{
$catEdit = Category::find($id); // edit this line.
$categories = $this->getCat();
return view('categoriesAddForm',compact('categories','catEdit'));
}
source: http://laravel.com/docs/5.0/routing#route-parameters
I am using laravel 4. I have a table which displays data from "cars" table. I want to click at one of car's name and display all information about this vehicle in a new page. I have done this:
index.blade.php
#foreach ($cars as $car)
<tr>
<td>
{{ HTML::link('/desc', $car->Description, array('id' => 'linkid'), true) }}</td>
{{ Form::open(array('action' => 'CarController#showDesc', $car->id)) }}
{{ Form::hidden('id', $car->id) }}
{{ Form::close() }}
<td>
{{ $car->License }}</td>
<td>{{ $car->Make }}</td>
<td>{{ $car->status }}</td>
</tr>
#endforeach
CarController.php
public function index() {
$cars = Car::all();
return View::make('pages.index', compact('cars'));
}
public function showDesc() {
$description = //here want to get name, year, color from "cars" where id = POST['id']
return View::make('pages.description', compact('description'));
}
//pages description is the new page which will display all information about that specific vehicle ,
routes.php
Route::resource('cars', 'CarController');
Route::post('desc', array('uses' => 'CarController#showDesc'));
The problem is that browser shows: This webpage not available
you are using Route::resource('cars', 'CarController'); and this will create restful routes to access your controller and in the car controller you should have functions like index(), show($id), destroy($id).
To see what are the valid routes, run php artisan routes in your project directory.
if you want to follow the restful pattern which is what you want to do I think by using a resource route. be sure you have the function show($id) in your CarController. the route to that function is cars/{$id} so in your view make a link to this route:
{{link_to_action('CarsController#show', $car->Description, $car->id)}}
I hope this will fix the problem
in your view you have a link to "/description" and in the routes.php file you don't have a route to that link