I'm new on laravel and i'm trying to make on-many relationship between category table and devices table on database
in Category view i have all the category and i want when i clik to one category to go to another view and show all the devices of this category
It work's with my using rsource controller for the category and in the show method : CatgoryController.php
public function show($id)
{
$categories = Category::find($id);
$all_devices = $categories->devices;
return view('devices',compact('categories',$categories,'all_devices',$all_devices));
}
and in the devices view i do:
devices.blade.php
#extends('master')
#section('content')
<div class="container">
<div class="row">
<div class="col-lg-12 text-center animateup">
<div class="inline-icons-text section-heading">
<div class="inline-icon">
<hr/><hr/><hr/><hr/><hr/>
</div>
<span class="inline-icon heading-width">
<span class="heading-text"><h1> {{$categories->category_name}} </h1> </span>
</span>
<div class="inline-icon">
<hr/><hr/><hr/><hr/><hr/>
</div>
</div>
</div>
</div>
<br/>
<div class="row text-center">
#foreach ($all_devices as $device)
<div class="col-md-3 col-sm-6 animateleft">
<h5>
{{$device->device_name}}
<span class="glyphicon glyphicon-chevron-right"></span>
</h5>
</div>
#endforeach
</div>
</div>
#stop
this is how i show all the category and when click on one it goes to anthor view to show the devics of this category
categories.blade.php
<div class="row text-center">
#foreach ($categories as $categories)
<div class="col-md-3 col-sm-6 animateleft">
<h5>
{{$categories->category_name}}
<span class="glyphicon glyphicon-chevron-right"></span>
</h5>
</div>
#endforeach
</div>
and finally this my rout Rout.php
Route::resource('categories','CategoryController');
Route::resource('/devices','DevicesController');
My Problem is that evry thing work but the page that have the devices of one category dosen't have the style of the site
move ur stylesto public folder into any subfolder..here i'm using devices as sub-folder and devices.css is my file..
place link like this in ur master blade.with ur folder and file names
<link rel="stylesheet" href="{{URL::to('/')}}/devices/devices.css">
Related
I have this:
#foreach($portfolios as $portfolio)
<div id="item-portfolio-expand{{$portfolio->id}}" class="item-portfolio-expand expand">
<div class="container">
<div class="row">
<div class="col-md-8">
col-md-8
</div>
<div class="col-md-4">
{{ __('Name')}}: {{ $portfolio->name }}
</div>
</div>
</div>
</div>
#endforeach
And this:
#foreach ($portfolios as $portfolio)
<div class="portfolio-item {{ $portfolio->tag }} " data-category="transition">
<img src="{{ $portfolio->image_show }}">
<a href="#item-portfolio-expand{{$portfolio->id}}" data-toggle="collapse" data-target="#item-portfolio-expand{{$portfolio->id}}" data-parent="#item-portfolio-expand{{$portfolio->id}}">
<div class="portfolio-item-overlay">
<p class="name-item-overlay">{{ $portfolio->name }}</p>
</div>
</a>
</div>
#endforeach
And when I press the in the first foreach, I want to toggle the second foreach at <div id = "item-portfolio-expand {{$ portfolio-> id}}" class = "item- portfolio-expand expand "> I just don't succeed at anything, I use bootstrap and jquery in the project. .item-portfolio-expand has display: none;
And at the same time when one is open and another is selected, to close the one already open and to appear the one selected with toggle.
**Using Laravel 5.8.31 & Bootstrap 4. Browser: Chrome v. 76.0.3 and Opera 62.0.33 **
I was testing my CRUD functions by deleting a post. After deletion, the controller redirects back to my post index page. When the page loaded, the bootstrap side bar wrapped down below the other posts, rather than being on the right. Was fine before the post deletion.
I tried altering the cols and floating elements to no avail. Using Chrome dev tools, I see that the sidebar is nested in an em tag that isn't in the code. Tried clearing application cache. No effect.
I suspect this has something to do with the blade templating language and parsing the html, although if it is, I have no idea how to fix this other than manually installing the sidebar on every page.
How do I fix this??
Code can be found here: https://github.com/gkennedy87/Hillcrest/tree/master/resources/views
(It seems that laravel is also wrapping the footer in an em tag. Although this is a non-issue at the moment)
// views/posts/index.blade.php
#extends('layouts.app')
#section('content')
#include('inc.navbar')
<div class="container">
<div class="row">
<div class="col-md-8 col-lg-8">
<h1>Our Blog</h1>
#if (count($posts) > 0)
#foreach ($posts as $post)
<div class="card mb-4">
<img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap">
<div class="card-body">
<h2 class="card-title">{{$post->title}}</h2>
<p class="card-text">{!! str_limit($post->body,200,'...')!!}</p>
Read More →
</div>
<div class="card-footer text-muted">
Posted on {{$post->created_at}} by
Start Bootstrap
</div>
</div>
#endforeach
{{$posts->links()}}
#else
<p>No Posts Found</p>
#endif
</div>
#include('inc.sidebar')
<!--end row -->
</div>
<!--end container -->
</div>
#include('inc.footer')
#endsection
// views/inc/sidebar.blade.php
<!--sidebar start -->
<div class="col-md-4 col-lg-4">
<div class="sticky">
<!-- Search Widget -->
<div class="card my-4">
<h5 class="card-header">Search</h5>
<div class="card-body">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for...">
<span class="input-group-btn">
<button class="btn btn-secondary" type="button">Go!</button>
</span>
</div>
</div>
</div>
<!-- Categories Widget -->
<div class="card my-4">
<h5 class="card-header">Categories</h5>
<div class="card-body">
<div class="row">
<div class="col-lg-6">
<ul class="list-unstyled mb-0">
<li>
Web Design
</li>
<li>
HTML
</li>
<li>
Freebies
</li>
</ul>
</div>
<div class="col-lg-6">
<ul class="list-unstyled mb-0">
<li>
JavaScript
</li>
<li>
CSS
</li>
<li>
Tutorials
</li>
</ul>
</div>
</div>
</div>
</div>
<!-- Side Widget -->
<div class="card my-4">
<h5 class="card-header">Side Widget</h5>
<div class="card-body">
You can put anything you want inside of these side widgets. They are easy to use, and feature the new Bootstrap 4 card containers!
</div>
</div>
</div>
</div>
<!-- sidebar end -->
I setup the app on a local environment, the sidebar always stays on the right even after deleting a post. The response also doesn't contain any em tags. Could you try clearing the cache:
php artisan cache:clear
php artisan view:clear
php artisan config:clear
Ok, so I saw similar questions asked but none of the answers helped, so forgive me if it seems like this is a duplicate.
I am following a course on Udemy(It's been over a week, the instructor hasn't answered my question, although they are active on the site, hence why I'm here), the codes I'm displaying below is how the instructor has taught us to write it.
THE ISSUE:
We are creating a blog and that blog has many categories and many posts but each post belongs to only ONE category which can hold multiple posts.
Here's a look at the model for them.
public function user(){
return $this->belongsTo('App\User');
}
public function posts(){
return $this->hasMany('App\Post');
}
POST MODEL
public function user(){
return $this->belongsTo('App\User');
}
public function category(){
return $this->belongsTo('App\Category');
}
They each have their own controller also, we also have a frontend controller that displays all the categories and when you click read more it takes you to a single page that displays the most recent post within that category.
This single page also has a pagination that supposed to display the previous post that belongs to that category.
INSTEAD: it takes the user to the most recent post regardless of what category it belongs to and display all the previous post for all category.
Below is the frontend controller
public function index()
{
return view('categories')
->with('categories',Category:orderBy('created_at', 'desc')->take(24)->get())
->with('tag', Tag::all())
->with('posttitle', Post::orderBy('created_at', 'desc')->first());
}
public function tag($id)
{
$tag = Tag::find($id);
return view('tag')->with('tag', $tag)
->with('categories', $tag->tag)
->with('categories', Category::all());
}
public function singlePage($slug)
{
$post = Post::where('slug', $slug)->first();
$next_id = Page::where('id', '>', $page->id)->min('id');
$prev_id = Post::where('id', '<', $post->id)->max('id');
return view('single')->with('post', $post)
->with('categories', Category::all())
->with('posttitle', Post::take(1)->get())
->with('next', Post::find($next_id))
->with('prev', Post::find($prev_id));
}
Here's a look at the view for the index function
<div class="container">
<div class="container-fluid">
<div class="row medium-padding120 bg-border-color">
<div class="container">
<div class="col-lg-12">
<div class="offers">
<div class="row">
<div class="col-lg-8 col-md-12 col-sm-12 col-xs-12">
<div class="heading">
<h4 class="h2 heading-title">Discover New Articles</h4>
<div class="heading-line">
<span class="short-line"></span>
<span class="long-line"></span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="case-item-wrap">
#foreach($categories as $category)
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="case-item">
<div class="case-item__thumb">
<img src="{{ $category->featured_img }}" alt="{{ $category->title }}"style="width:300px; height:280px;">
</div>
<h6 class="case-item__title text-center">{!! $category->title !!}</h6>
<!--<i class="seoicon-clock"></i>
<time class="published" datetime="2016-04-17 12:00:00">
{{ $category->created_at->diffForHumans() }}
</time>-->
<h6 style="width:300px; height:85px;">{!! str_limit($category->summary, 85) !!}
<small> Read More</small>
</h6>
</div>
</div>
#endforeach
</div>
</div>
<div class="padded-50"></div>
</div>
</div>
</div>
</div>
and this is the single page view below
<div class="content-wrapper"> <div class="container"> <div>
<main class="main">
<div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-1 col-md-7 col-md-offset-1 col-lg-6 col-lg-offset-3 panel panel-reading">
<article class="hentry post post-standard-details">
<h2>{!! $post->posttitle !!}</h2>
<div class="post__content">
<div class="post-additional-info">
<div class="post__author author vcard">
By:
<div class="post__author-name fn">
{{ $post->user->username}}
</div>
</div>
{{--Post date aka page date --}}
<span class="post__date">
<time class="published" datetime="2016-03-20 12:00:00">
<i class="seoicon-clock"></i> {{ $post->created_at->diffForHumans() }}
</time>
</span>
{{--End Post date aka page date--}}
</div>
{{--Post content aka page content--}}
<div class="post__content-info">
<p> {!! $post->content !!} </p>
</div>
{{--End post content aka page content--}}
</div>
<div class="pagination-arrow">
{{--Left arrow--}}
#if($prev)
<a href="{{ route('post.single', ['slug' => $prev->slug] )}}" class="btn-prev-wrap">
<svg class="btn-prev">
<use xlink:href="#arrow-left"></use>
</svg>
<div class="btn-content">
<div class="btn-content-title">Previous Page</div>
<p class="btn-content-subtitle">{{ $prev->posttitle}}</p>
</div>
</a>
#endif
{{--Right arrow--}}
#if($next)
<a href="{{ route('post.single', ['slug'=>$next->slug]) }}" class="btn-next-wrap">
<div class="btn-content">
<div class="btn-content-title">Next Page</div>
<p class="btn-content-subtitle">{{ $next->posttitle }}</p>
</div>
<svg class="btn-next">
<use xlink:href="#arrow-right"></use>
</svg>
</a>
#endif
</div>
</article>
</div>
</main>
HERE'S the post database
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('posttitle');
$table->string('slug');
$table->text('content');
$table->softDeletes();
$table->timestamps();
});
}
IN CONCLUSION:
Using this how would you filter it so that only the post that has the same category_id is displayed when the user clicks on that category?
Thank you
As a bonus I did a short screen recording of the problem https://www.screencast.com/t/vVzRMSunH
Sorry, I cant repeat all your codes but if I well understand you, you want to be able to show on a single page, all posts belonging to a specifique category. If it is, the controller who generate view should be like this. Question of organisation, I prefer to have a specific Controller for all action about category model.
/**
* Show all posts of category $id
*
* #param int $id : Id of category mapped via route
*/
public function show($id)
{
$category = Category::findOrFail($id);
$posts = $category->posts()->paginate();
return view('category.show', compact('posts'));
}
And your resources/views/category/show.blade.php can be like this
#extends('layouts.app')
#section('content')
#foreach($posts as $post)
<h2>{{ $post->title }}</h2>
{{-- and you can show more informations about post here --}}
#endforeach
{{-- pagination links --}}
{!! $posts->links() !!}
#stop
I'm making a forum and the if the user clicks on a theme, he gets redirected to the page where all the topics are that belong to that theme. What I'm trying to do, is that the user is able to click on the topic and gets redirected to it.
The problem is, is that I get an error saying Undefined variable: theme
As you can see in the code below, I have a foreach loop in my theme.blade.php which loops all the topics that belong to the theme he/she clicked on. In the beginning of the loop there is a <a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}"1 which is supposed to bring the user to the topic that he/she clicked on. The URL that is supposed to show is forum.dev/theme/{theme_id}/topics/{topic_id} But that doesn't work.
And it gives me that error code. All the code is below, If i miss some code, Please inform me about it and i'll update the question. Thanks in advance
theme.blade.php
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 125, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Posted by: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Last reply</h6>
<p class="center-align"></p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{theme_id}/topics', 'ThemesController#show')->name('showtheme');
Route::get('/theme/{theme_id}/topics/{topic_id}', 'TopicsController#topic')->name('showtopic');
Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function() {
//THEMES
Route::get('/theme/{theme_id}/edit', 'ThemesController#edit')->name('edittheme');
Route::patch('/theme/{theme_id}/edit', 'ThemesController#update')->name('updatetheme');
Route::get('/theme/create', 'ThemesController#create')->name('createtheme');
Route::post('/theme/create', 'ThemesController#save')->name('savetheme');
Route::delete('/theme/{theme_id}/delete', 'ThemesController#destroy')->name('deletetheme');
//TOPICS
Route::get('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#edit')->name('edittopic');
Route::patch('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#update')->name('updatetopic');
Route::get('/theme/{theme_id}/topics/create', 'TopicsController#create')->name('createtopic');
Route::post('/theme/{theme_id}/topics/create', 'TopicsController#save')->name('savetopic');
Route::delete('/theme/{theme_id}/topics/{topic_id}/delete', 'TopicsController#destroy')->name('deletetopic');
});
Route::get('user/profile', 'UserController#profile')->name('showprofile');
Route::post('user/profile', 'UserController#update_avatar');
TopicsController.php (Show method)
public function show($id)
{
$theme = Theme::find($id);
$topics = Topic::find($id);
return view('topics/topic')->with('topics', $topics)->with('theme', $theme);
}
I don't know if you can use the double '->with' option.
What will work is the following:
return view('topics/topic', ['topics' => $topics, 'theme'=> $theme]);
You pass an array as the second argument of the view. You can add as many keys and variables as you like.
I am making a forum and when the user clicks on a theme, a page shows up with every topic that belongs to that theme. The problem is, how do I do this?
I made a for each loop which shows ALL the topics from the database instead of the topics that belong to that theme I clicked on. How can I do this?
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{id}', 'ThemesController#show')->name('showtheme');
ThemesController.php
I only show the show method because I believe that's the only one necessary here
public function show($id)
{
$topics = Topic::all($);
return view('themes/topics')->with('topics', $topics);
}
topics.blade.php
#extends('layouts.default')
#section('content')
<div class="container main-content">
<div class="row first-row">
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 100, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Gepost door: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Laatste reply</h6>
<p class="center-align">Naam</p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
</div>
</div>
#endsection
I believe I'm doing something obviously wrong but I can't see what.
Help will be appreciated. Thanks in advance
If I miss some code, Please inform me.
How are you defining the relationship between a theme and it's topics? You'll have to have something like a one to many or many to many established in your database for you to be able to query correctly. Do you have anything like that yet?
****edit****
public function show($id)
{
$topics = Theme::find($id)->topics;
return view('themes/topics')->with('topics', $topics);
}