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
Related
I have 2 tables in my db, 1 for Collabs and 1 for Projects
I want when I view a project, to display the collabs based on that project (if i press view on a project for example project with id = 10, to display all Collabs for the project that is id 10).
For Collabs table I have id_project that is wanted to be in relationship with id from Project table, so when I insert a new collab in my Collabs table it takes the id from the project I inserted.
For now, this is how I display the collabs, and i display them all for all projects but I don't want that.
#forelse ($istoric as $istProj)
<div class="mb-3">
<table class='table'>
<tr class="table-row-heads">
<th>Id Proiect</th>
<th>Tip actiune </th>
<th>Colaborator </th>
<th>Suma </th>
<th>Data </th>
</tr>
<tr class="table-row-data">
<td>{{ $istProj->id_proiect }}</td>
<td>{{ $istProj->action_type }}</td>
<td>{{ $istProj->colaborator_id }}</td>
<td>{{ $istProj->suma }}</td>
<td>{{ $istProj->data }}</td>
</tr>
</table>
</div>
#empty
<div class="card-body">
<h2>Nu au fost gasite inregistrari</h2>
</div>
#endforelse
You should really consider reading and watching more videos on how relationships and eloquent works, I hope this below is a good reference for you to get started, please read carefully, and sorry I couldn't translate back to romanian, and to avoid any mistakes, I kept my code in english.
Caloboratori = Colaborators
Istoric Proiecte = Project History
id || auto_increment
project_id || bigInteger()
colaborator_id || bigInteger()
Proiecte = Project
id || auto_increment
Project Model
/* To load the history, we will be using hasMany relationship, because for each
project, we have lots of history, please read more on one-to-many relationships here
https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
Istoric Proiecte = Project History
id || auto_increment
project_id || bigInteger()
colaborator_id || bigInteger()
*/
public function histories() {
return $this->hasMany(ProjectHistory::class);
}
Project History Model
//We will reverse the one-to-many relationship, with belongsTo here. | example: project_id
public function project() {
return $this->belongsTo(Project::class);
}
//We will reverse the one-to-many relationship, with belongsTo here. | example: colaborator_id
public function colaborator() {
return $this->belongsTo(Colaborator::class);
}
Projects Controller:
// Show a list of all projects
public function index() {
//Get all projects
$projects = Project::all();
//Load all of the project relationships that we will be using
$projects->load('histories.colaborator');
return view('projects.index', compact('projects'));
}
// Show a single project
public function show(Project $project) {
//Load all of the project relationships that we will be using
$project->load('histories.colaborator');
//Assign the loaded project history
$histories = $project->histories;
return view('projects.show', compact('project', 'histories'));
}
projects.index Blade: in this blade, you can forloop thru all of your projects model, and assign them as $project, since we loaded the relationships earlier from the controller.
You can easily access the relationships using $project->histories then assign each history model to $history.
Then you can go one step inside of the history relationship and call the inner relationship of colaborator with $history->colaborator
#foreach ($projects as $project)
<p>Project id: {{ $project->id }}
<p>Project name: {{ $project->name }}
<h1>Project History list</h1>
#foreach ($project->histories as $history)
<ul>
<li>ID: {{$history->id}}</li>
<li>Name: {{$history->name}}</li>
<li>Colaborator Name: {{$history->colaborator->name}}</li>
</ul>
#endforeach
#endforeach
projects.show Blade: in this blade, we have a single project, and you can forloop thru all of your history models, since we loaded the relationships from the controller.
We assigned the histories collection as $histories then assign each history model to $history
Then you can go one step inside the history relationship and call the inner relationship of colaborator with $history->colaborator
<p>Project name: {{ $project->name }}
<h1>Project History list</h1>
#foreach ($histories as $history)
<ul>
<li>ID: {{$history->id}}</li>
<li>Name: {{$history->name}}</li>
<li>Colaborator Name: {{$history->colaborator->name}}</li>
</ul>
#endforeach
If you use model Collab, and within you have project relation , than you can use
Collab::query()
->with('project', function ($q) use ($id) {
return $q->where('id', $id);
})
->get();
Or you can use query builder as well
DB::table('collabs')
->select('collabs.*')
->join('projects', 'projects.id', '=', 'collabs.project_id')
->where('projects.id', $id)
->get();
Just adjust it according what you really need.
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
I'm pulling data from database and i would like to make {{$data->title}} clickable.
“This for a new server. In the past, I’ve tried on existing servers.”
#foreach($ress as $key => $data)
{{$data->title}}
#endforeach
Define a route in your routes.php and call it in your template
{{ $data->title }}
This is the best way to do it. You can change the variable names to the ones you have used in your code.
Route.php
Route::get('/show/{id}','DataController#show')->name('showdata);
Link on say dataindex.blade.php
#foreach($datas as $data)
{{ $data->title }}
#endforeach
DataController.php
public function show(){
$datas = DB::table('table_name')->get();
return view('dataindex', ['datas'=>$datas]);
}
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 }}
Let me explain situation first.
I am on the page with list of skills available such as "Plumber","Carpenter" and "Painter", when I click on one of those skills I want to get a list of handymans that have that skill and once clicked on a handyman I get full details about him.
Skills are displayed however when I click on one of the skills it doesn't want to retrieve any data. Both tables "handymen" and "skills" have many to many relationship and also there is a junction table. What am I doing wrong here?
Route::group(['middleware' => ['web']], function () {
Route::get('home', 'HandymanController#home');
Route::get('search', 'HandymanController#search');
Route::get('details/{handyman}', 'HandymanController#details');
Route::post('assignjob', 'HandymanController#assignJob');
Route::get('addjob', 'HandymanController#addJob');
Route::post('addjform', 'HandymanController#addjForm');
Route::get('jobs', 'HandymanController#jobs');
Route::get('jobsdetails/{jobId}', 'HandymanController#jobsdetails');
Route::get('deletejob', 'HandymanController#deleteJob');
Route::post('deletejform', 'HandymanController#deletejForm');
Add Job View:
#extends('layouts.master')
#section('title', 'Add Job')
#section('header2')
<ul>
<li>Assign job</li>
</ul>
#show
#section('content')
<h1>Handyman details</h1>
<ul>
#foreach ($handymen as $handyman)
<a href= "{{ url("HandymanController#details", $handyman->id) }}">
{{$handyman->name}}
</a>
#endforeach
</ul>
Controller:
function search()
{
$skills = Skill::all();
return view('layouts/search',['skills' => $skills]);
}
function details($skillId)
{
$skill = Skill::find($skillId);
$handymen = $skill->handymen;
return view('layouts/details', ['handymen' => $handymen]);
}
According to your edited question, first you list all the skills in your search view. So, in your search view, you would have something like this:
#foreach ($skills as $skill)
<a href= "{{ action("HandymanController#addjForm", $skill->id) }}">
{{ $skill->name }}
</a>
#endforeach
So you have to define in your routes file this route:
Route::post('addjform/{skill}', 'HandymanController#addjForm');
This will point to Handyman Controller, where you will list all handymen that have that skill:
public function addjForm(Request $request, Skill $skill)
{
$handymen = $skill->handymen;
return view('layouts/skilledHandymen', ['skill' => $skill,'handymen' => $handymen]);
}
For this to work, you have to define in Skill Model, the association:
public function handymen()
{
return $this->belongsToMany(Handyman::class,
'handyman_skill',
'skill_id',
'handyman_id');
}
The controller will point to a view where you will list all handymen that have such skill:
In your case, it would be easier if you define an association in Skill model that links to Handyman:
#foreach ($handymen as $handyman)
<a href= "{{ action("HandymanController#details", $handyman->id) }}">
{{ $handyman->name }}
</a>
#endforeach
When you choose a handyman, it will take you to Handyman controller details:
function details(Request $request, Handyman $handyman)
{
return view('layouts/details', ['handymen' => $handymen]);
}
For this you will define this route:
Route::get('/handyman/{handyman}', 'Handyman#details');
And this will point you finally to the details of chosen handyman, and you can show his details:
<p>{{ $handyman->id }}<p>
<p>{{ $handyman->name }}</p>
The thing that is important to understand here is that you will first have a Collection of skills that will lead you to a Collection of Handymen, and not just a single one. After you choose a Handyman from the second list you will be able to show his details. If you try to jump over this step you will be trying to show details of a list.
Hope this helps...