Passing data from blade to route - php

I'm trying to list articles from database then show details of the article when the user clicks on it.
How can I pass the article id or name to the route/controller to display the details? I tried butting the id in the href but I don't know how to handle it from routes!
this is the articles list:
#foreach ($articles as $article)
<div class="panel-body">
{{$article->name}}
</div>
#endforeach
where do I go from here?

Your View file for listing all the articles:
#foreach ($articles as $article)
<div class="panel-body">
{{$article->name}}
</div>
#endforeach
routes.php
Route::get('/article/{id}', 'ArticlesController#show');
ArticlesController.php
/**
* Show the article of the given id.
*
* #param int $id The id of the article
* #return \Illuminate\View\View
*/
public function show($id)
{
$article = Article::findOrFail($id);
return view('articles.show', compact('article'));
}
show.blade.php
<h3>{{ $article->name }}</h3>
<p>{{ $article->body }}</p>

If you want to use route() you can pass parameters in second argument
#foreach ($articles as $article)
<div class="panel-body">
{{ $article->name }}
^^^ or whatever is the name of your route
</div>
#endforeach
Naturally this assumes you have your route defined in app\Http\routes.php. For example
Route::get('/articles/{id}', ['as' => 'article.show', 'uses' => 'ArticleController#show']);
Alternatively you can use url()
#foreach ($articles as $article)
<div class="panel-body">
{{ $article->name }}
</div>
#endforeach

Related

Laravel records not geting as dynamic

views/show.blade.php
#extends('layouts.app')
#section('content')
<h5>Showing Task {{ $task->title }}</h5>
<div class="jumbotron text-center">
<p>
<strong>Task Title:</strong> {{ $task->title }}<br>
<strong>Description:</strong> {{ $task->description }}
</p>
</div>
#endsection
Controllers/HomeController.php
public function show(Task $task)
{
return view('show', compact('task', $task));
}
routes/web.php
Route::get('show/{id}', 'HomeController#show')->name('show');
views/viewalltask.blade.php
<td>{{$data->title}}</td>
No error / No Record / instead of particulr record display blank page
You need to change the route configuration to:
Route::get('show/{task}', 'HomeController#show')->name('show');
This way Laravel's IOC container knows how to resolve/bind it.
Must match the variable name used in the method definition:
public function show(Task $task)

can`t access laravel Query Builder result from view

I can`t access the values from this variable $post from controller:
in blade view:
#foreach($post as $p)
<div class="col-md-3">
<div class="boxpromo">
<h4>{{ $p->title }}</h4>
<p>{{ $p->post_description }}</p>
<div class="price">
<div class="data">{{ $p->out_date }}</div>
<div class="cost">{{ $p->price }}</div>
</div>
</div>
</div>
#endforeach
In controller i have:
public function postSearchTransport(Request $request, Post $post){
...
// create a new query builder
$post = $post->newQuery();
// add some filters from the user in the search query
if($request->has('searchWord')){
$post->where('title', 'LIKE', '%'.$request->input('searchWord').'%');
}
...//more filters
$post->paginate(5);
return view('searchVehicle.showSearchPost')->with('post', $post);
}
What i did wrong? I should be able to access in the view all the values from the query result, but i can`t this way.
Should i convert that $post std object from controller into an array then return it to the view???
You must pass to the view the returned value of paginate method like this :
$posts = $post->paginate(5);
return view('searchVehicle.showSearchPost')->with('post', $posts);

How to make url with slug in Laravel 5.4?

Right now I am trying to do a Laravel url with slug. I have my main page where I show all categories:
<div class="panel-body">
#foreach($categories as $category)
<p>{{ $category->name }}</p>
#endforeach
</div>
And now I want to go to a certain category page. I created routes:
Route::get('/test', 'FuelAccountingController#index')->name('fuel_accounting');
Route::get('/test/{slug}', 'FuelAccountingController#element');
And functions in controller:
public function index()
{
$categories = DB::table('fuel_accounting_categories')
->select('id', 'name', 'slug')
->get();
return view('pages.fuel_accounting')->with('categories', $categories);
}
public function element($slug)
{
$category = DB::table('fuel_accounting_categories')
->select('slug')
->where('slug', '=', $slug)
->first();
return view('pages.fuel_accounting_element')->with('category', $category);
}
And when I am trying to reach a page (for example: laravel.dev/test/current_category) it does not work. Can someone explain me how to do that?
Error: Sorry, the page you are looking for could not be found. (1/1) NotFoundHttpException
FIXED
<div class="panel-body">
#foreach($categories as $category)
<p>{{ $category->name }}</p>
#endforeach
</div>

NotFoundHttpException in RouteCollection.php line 161 - Laravel 5.2

I cant make this route works
routes.php:
Route::get('/home/category/{$category_id}', 'HomeController#category');
HomeController.php
public function category(Request $request, $category_name, $category_id)
{
$listProject = $this->project->getProjectsFromCategory($category_id);
return view('category', [
'projects' => $listProject,
]);
}
view
<div class="col-md-12">
#foreach ($categories as $key => $cat)
{{ $cat->name }}
#endforeach
</div>
url in browser
http://localhost:8888/ko/public/home/category/6
and i'm getting
NotFoundHttpException in RouteCollection.php line 161
Does anyone see something wrong ?
I have another route which is working
Route::get('/home/project/{project_id}','ProjectDetailsController#index');
Thanks for help
here is more code
routes.php
Route::auth();
Route::get('/', 'HomeController#index');
Route::get('/home', 'HomeController#index');
Route::get('/home/project/{project_id}','ProjectDetailsController#index');
Route::post('/home/create/addProject', 'CreateController#addProject');
Route::get('/home/create', 'CreateController#index');
Route::get('/home/category/{$category_id}', 'HomeController#category');
HomeController.php
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct(ProjectRepository $project, UserRepository $user, CategoryRepository $category)
{
//$this->middleware('auth');
$this->project = $project;
$this->user = $user;
$this->category = $category;
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$listProject = $this->project->getAllProjects();
$categories = $this->category->getAllCategories();
foreach($listProject as $key => $project){
// get creator of project
$user[$key] = $this->user->getCreator($project->creator_id);
// get category of project
$category[$key] = $this->category->getCategory($project->category_id);
}
return view('home', [
'projects' => $listProject,
'user' => $user,
'category' => $category,
'categories' => $categories
]);
}
public function category(Request $request, $category_id)
{
$listProject = $this->project->getProjectsFromCategory($category_id);
return view('category', [
'projects' => $listProject,
]);
}
}
view
#extends('layouts.app')
#section('content')
<div class="container-fluid">
<div class="row">
#foreach ($projects as $key => $project)
<div class="col-md-4" style='margin-bottom:20px;'>
<div class="border" style="border:1px solid #e7e7e7; padding:15px;">
ID : {{ $project->id }} <br>
TITLE : {{ $project->title }} <br>
CATEGORY : {{ $category[$key]->name }} <br>
CREATOR : {{ $user[$key]->name }} <br> <br>
DETAILS : See project
</div>
</div>
#endforeach
</div>
<div class="row">
<div class="col-md-12">
#foreach ($categories as $key => $cat)
{{ $cat->name }}
#endforeach
</div>
</div>
</div>
#endsection
Your controller is wrong. You send to controller from route only 1 parameter ($category_id). But accept another parameter: $category_name in your controller.
Removing $category_name from controller will fix an error.
You need just define only parameters from route in your controller. /category is not a parameter.
Documentation
Also there is a wrong code in your blade file:
Replace
{{ $cat->name }}
With
<a href='{{url("home/category/$cat->id")}}'> {{ $cat->name }} </a>

Laravel search for topic by title and generate link

I'm laravel newbie. I have something like this:
Now I wan't to make when click first topic redirect to that topic url. For that I need to use DB:: function. I wan't search by given title, and then get id, etc.. For example i have posts:
table name same as topic title in image above, so I need use DB::select or ::table and search by that name, then print results. How to?
Layout:
#extends('layouts.main')
#section('content')
<div class="panel panel-default">
<div class="panel-heading">Forum</div>
<div class="panel-body">
#forelse($forums as $forum)
#forelse($topics as $topic)
{{ $topic->title }}<br>
#empty
<div class="alert alert-danger">Apgailėstaujame tačiau šis forumas yra tuščias!</div>
#endforelse
#empty
<div class="alert alert-danger">Apgailėstaujame tačiau forumas nerastas</div>
#endforelse
</div>
</div>
{!! $topics->render() !!}
#stop
Thanks in advance ;(
UPDATED
viewForum Controller:
<?php
namespace App\Http\Controllers;
use DB;
use View;
class viewForum extends Controller
{
public function showForum($fname, $fid)
{
$forums = DB::table('forums')
->where('id', $fid)
->where('seo_name', $fname)
->select()
->get();
$topics = DB::table('topics')
->where('forum_id', $fid)
->select()
->paginate(1);
return View::make('forum', compact('forums', 'topics'));
}
}
In your topic href do this:
{{ $topic->title }}<br>
In your routes.php add this
Route::get('topic/{fname}/{fid}', 'viewForum#showForum');

Categories