Laravel submit form from a tab and return to same tab in view - laravel

I have a view with multiple tabs. Each having different forms. When I submit a form from one tab, it returns to same page but primary tab. How could I get to return to same tab from which I was working.
Controller
public function recieve(Request $request)
{
$item = Item::find($request->input('item'));
$item->recieved_at = now();
$item->recieved_status = "1";
$item -> save();
return redirect('/files/'.$item->file)->with('success','Item Recieved Confirmed');
}
view - tabs
<ul class="nav nav-tabs" id="fileTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="profile-tab" data-toggle="tab" href="#profile" role="tab"
aria-controls="profile" aria-selected="true">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" id="job-tab" data-toggle="tab" href="#job" role="tab" aria-controls="job"
aria-selected="false">Job</a>
</li>
<li class="nav-item">
<a class="nav-link" id="items-tab" data-toggle="tab" href="#items" role="tab" aria-controls="items"
aria-selected="false">Items</a>
</li>
<li class="nav-item"><a class="nav-link" id="mechanic-tab" data-toggle="tab" href="#mechanic" role="tab"
aria-controls="mechanic" aria-selected="false">Labour Hours</a>
</li>
<li class="nav-item">
<a class="nav-link" id="accounts-tab" data-toggle="tab" href="#accounts" role="tab"
aria-controls="accounts" aria-selected="false">Accounts</a>
</li>
<li class="nav-item">
<a class="nav-link" id="accounts-tab" data-toggle="tab" href="#preview" role="tab"
aria-controls="accounts" aria-selected="false">Print Preview</a>
</li>
</ul>
Form
#if($file->job_status == "Closed")#else
{!! Form::open(['action' => 'FilesController#item','method' => 'POST']) !!}
<div class="row pt-3 pb-1">
<div class="form-group col-xs-12 col-sm-12 col-md-2">
{{Form::text('part_number','',['class'=>'form-control','placeholder'=>'Part Number'])}}
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-6">
{{Form::text('description','',['class'=>'form-control','placeholder'=>'Part Name'])}}
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-2">
{{Form::text('qty','',['class'=>'form-control','placeholder'=>'Qty'])}}
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-2">
{{Form::select('recieved_by',$users,null,['class'=>'form-control'])}}
</div>
</div>
{{Form::hidden('file',$file->id)}}
{{Form::submit('Release Item',['class'=>'form-control btn-danger btn btn-sm'])}}
{!! Form::close() !!}
#endif
I tried using hashtag tab name in return redirect statement,
return redirect('/files/'.$item->file."#items-tab")->with('success','Item Recieved Confirmed');
it would simply highlight the name but would not select. How could I achieve it?

your tab navigation,
<ul class="nav nav-tabs" id="tabMenu" role="tablist">
<li><i class="fa fa-camera" aria-hidden="true"></i> Galleries</li>
</ul>
hidden input field for tab
<input type="hidden" name="tab" value="Galleries">
RedirectRoute
$tab = $request->get('tab');
return back()->withInput(['tab'=>$tab]);
Javascript,
<script>
//redirect to specific tab
$(document).ready(function () {
$('#tabMenu a[href="#{{ old('tab') }}"]').tab('show')
});
</script>

If you want to do it in laravel than all you can do is,
-Give an id to each tab and an write an active class.
-Put a hidden input with value of the parent tab id.
-So, laravel will receive the tab id in the request object.
-Now, you can do whatever and return the tab id with to the view.
-Give a if condition to each tab item and check if matches the id from laravel response.
The match the id set the active class to it.
example
<ul>
<li id='tab1 {{ session()->get('tabId') === 'tab1' ? 'active' : '' }}'></li>
<li id='tab2 {{ session()->get('tabId') === 'tab2' ? 'active' : '' }}'></li>
</ul>
// tab1 body
<form>
<input type="hidden" name="tabId" value="tab1">
...
</form>
// tab2 body
<form>
<input type="hidden" name="tabId" value="tab2">
...
</form>
// controller
public funciton(Request $request) {
//tabId
$tabId = $request->input('tabId')
....
....
return redirect()->back()->with('tabId' => $tabId);
}

You can do this:
Controller:
public function tab() {
$active_tab = "tab2";
return view('tabs.index', compact('active_tab'));
}
View:
<ul class="nav nav-tabs">
<li class="#if($active_tab=="tab1") active #endif"><a data-toggle="tab" href="#home">Home</a></li>
<li class="#if($active_tab=="tab2") active #endif"><a data-toggle="tab" href="#menu1">Menu 1</a></li>
<li class="#if($active_tab=="tab3") active #endif"><a data-toggle="tab" href="#menu2">Menu 2</a></li>
</ul>
<div class="tab-content">
<div id="home" class="tab-pane fade #if($active_tab=="tab1") in active #endif">
<h3>HOME</h3>
<p>Some content.</p>
</div>
<div id="menu1" class="tab-pane fade #if($active_tab=="tab2") in active #endif">
<h3>Menu 1</h3>
<p>Some content in menu 1.</p>
</div>
<div id="menu2" class="tab-pane fade #if($active_tab=="tab3") in active #endif">
<h3>Menu 2</h3>
<p>Some content in menu 2.</p>
</div>
</div>

You can show a specific tab with this code:
function showTab(tab){
$('.nav-tabs a[href="#' + tab + '"]').tab('show');
};
// Verify location.hash
if (window.location.hash !== ''){
showTab(window.location.hash);
}
But I would pass a query string in the return, just to make sure there will be no collision with other eventual hashes. Something like:
return redirect('/files/'.$item->file."?openTab=your-tab-id")->with('success','Item Recieved Confirmed');
Of course, you would change the "location.hash" verification to a query string verification. Example: https://davidwalsh.name/query-string-javascript

Related

VueJs and Laravel dom contents not updating

I have this method when I click to view any member it shows the detail of the member in the modal, but it's not working as I expected
vue js code
const member_modal = ref(null);
const viewMember = (member) => {
axios.get("/api/member/" + member.id).then((response) => {
member_modal.value = response.data;
console.log(member_modal.value);
$("#viewMemberModal").modal("show");
});
};
code inside the modal (DOM)
<a href="#" #click.prevent="viewMember(member)"
><i class="fa fa-eye ml-2"></i
></a>
<img class="profile-user-img img-fluid img-circle"
src="{{ member_modal.image }}"
alt="User profile picture"
/>
</div>
<h3 class="profile-username text-center">
{{ member_modal.full_name }}
</h3>
<ul class="list-group list-group-unbordered mb-3">
<li class="list-group-item">
<b>Gender</b>
<a class="float-right">{{ member_modal.gender }}</a>
</li>
<li class="list-group-item">
<b>Phone</b>
<a class="float-right">{{ member_modal.phone }}</a>
</li>
<li class="list-group-item">
<b>Email</b>
<a class="float-right">{{ member_modal.email }}</a>
</li>
</ul>
I want when I click on the view member button it shows the specific member data in the modal, but it's not updating the values in the (dom), when I click the view member, it fetches the data from the database successfully and assigns the response result to the member_modal but the change is not showing the data in the modal

Dynamic Bootstrap Tabs In Laravel 6

I have two tables , categories and articles.Each category has id and name. Each articles has id, name and category id. Now I want to load the data on each tab when a user click on specific category_id.so related product will be shown on each category tab. I have tried but couldn't get the solution.
Category Model
public function articles()
{
return $this->belongsToMany('App\Article')->withTimestamps();
}
Article Model
public function categories()
{
return $this->belongsToMany('App\Category')->withTimestamps();
}
Controller
public function index(){
$categories = Category::with('articles')->whereIn('id', [1, 2, 3])->get();
return view('welcome',compact( 'categories'));
}
Here is the view :
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
#foreach($categories as $category)
<a class="nav-item nav-link " id="nav-home-tab-{{$category->id}}" data-toggle="tab" href="#nav-home-{{$category->id}}" role="tab" aria-controls="nav-home" aria-selected="true">{{$category->title}}</a>
#endforeach
</div>
</nav>
#foreach($categories as $category)
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-home-{{$category->id}}" role="tabpanel" aria-labelledby="nav-home-tab-{{$category->id}}">
<div class="row clearfix">
#foreach($category->articles as $article)
<div class="col-md-3">
<div class="box mt-2 mb-2">
<img src="{{ $article->images ['thumb'] }}">
</div>
</div>
#endforeach
</div>
</div>
</div>
#endforeach
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
#foreach($categories as $count => $category)
<a id="nav-home-tab-{{$category->id}}" data-toggle="tab" href="#nav-home-{{$category->id}}" role="tab" aria-controls="nav-home" aria-selected="true"
#if($count == 0) class="nav-item nav-link active" #else()class="nav-item nav-link "#endif>{{$category->title}}</a>
#endforeach
</div>
</nav>
<div class="tab-content" id="nav-tabContent">
#foreach($categories as $count => $category)
<div #if($count == 0) class="tab-pane fade show active" #else class="tab-pane fade" #endif id="nav-home-{{$category->id}}"
role="tabpanel" aria-labelledby="nav-home-tab-{{$category->id}}">
<div class="row ">
#foreach($category->articles as $article)
<div class="col-md-3">
<div class="box mt-2 mb-2">
<img src="{{ $article->images ['thumb'] }}">
<h2>قالب بوتسراپ شرکتی</h2>
<p>قالب بوت استراپ شرکتی کاملا فارسی سازی شده و رایگان میتوانید دانلود کنید</p>
<div class=" download-btn">
<i class="fa fa-download"></i> دانلود قالب
<i class="fa fa-laptop"></i> دموی قالب
</div>
</div>
</div>
#endforeach
</div>
</div>
#endforeach
</div>

Using Laravel blade syntax for Bootstrap tabs and routing

I have already read some links regarding Bootstrap tab in Laravel but none of them is working for me.
I have 3 views (movies, scheduled_movies, group_movies) each view has its respective controller. Now I want to show all these views using bootstrap tab component. I tried a lot, but tabs don't work properly or sometimes all the data of three view shown in the first tab and sometimes only tabs are shown but not the data.
Here is my code-
app.blade.php
#include('components.navbar')
#yield('base')
base.blade.php
#extends('layouts.app')
#section('base')
<div class="container-fluid">
<div class="row">
<div class="col-2 d-none d-md-block bg-light sidebar" >
#include('components.sidebar')
</div>
<main id="main" class="col-10 ml-sm-auto col-lg-10 px-3">
#yield('content')
</main>
</div>
</div>
#endsection
I have separate tab.blade.php where all my bootstrap tab code is written. and I have included all 3 views using #include
tab.blade.php
#extends('layouts.base')
#section('content')
<div class="p-3">
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link" id="movies-tab" data-toggle="tab" href="#movies" role="tab" aria-controls="movies"
aria-selected="true">All movies</a>
</li>
<li class="nav-item">
<a class="nav-link" id="scheduled-movies-tab" data-toggle="tab" href="#scheduled-movies" role="tab"
aria-controls="scheduled-movies" aria-selected="false">Scheduled Movies</a>
</li>
<li class="nav-item">
<a class="nav-link" id="group-movies-tab" data-toggle="tab" href="#group-movies" role="tab" aria-controls="group-movies"
aria-selected="false">Group Movies</a>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show" id="movies" role="tabpanel" aria-labelledby="movies-tab">
#include('movies.all_movies.index')
</div>
<div class="tab-pane fade" id="scheduled-movies" role="tabpanel" aria-labelledby="scheduled-movies-tab">
#include('movies.scheduled-movies.index')
</div>
<div class="tab-pane fade" id="group-movies" role="tabpanel" aria-labelledby="group-movies-tab">
#include('movies.group-movies.index')
</div>
</div>
</div>
#endsection
MoviesController
class MoviesController extends Controller
{
public function index()
{
$movies = Movie::all();
return view('movies.all_movies.index')->with('movies', $movies);
}
}
The same code is written in ScheduledMoviesController.
This is my web.php file
Route::get('movies', 'MoviesController#index');
Route::get('scheduled-movies', 'ScheduledMoviesController#index');
Route::get('group-videos', 'GroupMoviesController#index')->middleware('verified');
Till now I have figured out some cases
Maybe my blade syntax #yield or #include is wrong.
Maybe routing is wrong (because of the first page of the tab showing data from all three).

Laravel 5: How to get posts by specific categories and display in tabs

I'm building a news site. I want to get 6 latest posts by 3 specific categories, lets say id 2, 3, 4, and display them in 3 tabs on homepage.
Each tab is divided in 2 column, col-6. one col-6 display the latest record, the other display a list of another 5 records after the latest one.
Here's my controller, I get 6 latest records from database
$post_tabs = Post::with('categories')->latest()->limit(6)->get();
this I get 3 categories that have id 2, 3, 4
$category_tabs = Category::with('posts')->whereIn('id', [2, 3, 4])->get();
In blade view, I managed to display that 3 category tabs, like so
<div class="collapse navbar-collapse justify-content-between" id="navbarToggler1">
<ul class="nav nav-sm navbar-nav mr-md-auto mr-lg-0 mt-2 mt-lg-0 align-self-end" role="tablist">
#foreach($category_tabs as $tab_category)
<li class="nav-item">
<a class="nav-link bg-color-tech active" id="nav-outdoor-tab-{{$tab_category->id}}" data-toggle="tab" href="#nav-outdoor-{{$tab_category->id}}" role="tab" aria-selected="true">
{{$tab_category->title}}
</a>
</li>
#endforeach
</ul></div>
Now I don't know how to display posts of those categories by tabs, this is what i 've got so far, which only display 6 latest post and I don't know how to make tabs work either
#foreach($post_tabs as $key => $post_tab)
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-outdoor-{{-- {{$tab_category->id}} --}}" role="tabpanel">
<div class="row clearfix">
<div class="col-lg-6">
#if ($loop->first)
<article>
<div class="entry-image mb-3">
<img src="{{asset($post_tab->thumbnail)}}" alt="{{$post_tab->title}}">
#foreach($post_tab->categories as $category)
<div class="entry-categories">{{$category->title}}</div>
#endforeach
</div>
<div class="entry-title">
<h3><a target="_blank" href="{{route('post.show',[$post_tab->slug])}}">{{$post_tab->title}}</a></h3>
</div>
<div class="entry-content clearfix">
<p>{{$post_tab->description}}</p>
</div>
</article>
</div>
<div class="col-lg-6">
#foreach($post_tabs as $key => $post_tab)
#if($key > 0)
<article>
<div class="entry-image">
<img src="{{asset($post_tab->thumbnail)}}" alt="">
</div>
<div class="entry-c">
<div class="entry-title">
<h4>{{$post_tab->title}}</h4>
</div>
</div>
</article>
#endif
#endforeach
#endif
</div>
</div>
</div>
</div>
#endforeach
Thank you so much.
You don't need to fetch category and post separately. You can fetch both using eager loading. For that you can define extra relationship in Category model like this
Category Model
public function latestPosts(){
return $this->hasMany('App/Post')->latest()->take(6);
}
public function beforeLatestPosts(){
return $this->hasMany('App/Post')->latest()->slice(6)->take(5);
}
Fetch in controller
$category_tabs = Category::with('latestPosts', 'beforeLatestPosts')->whereIn('id', [2, 3, 4])->get();
In view
Print Tab header
<div class="collapse navbar-collapse justify-content-between" id="navbarToggler1">
<ul class="nav nav-sm navbar-nav mr-md-auto mr-lg-0 mt-2 mt-lg-0 align-self-end" role="tablist">
#foreach($category_tabs as $tab_category)
<li class="nav-item">
<a class="nav-link bg-color-tech active" id="nav-outdoor-tab-{{$tab_category->id}}" data-toggle="tab" href="#nav-outdoor-{{$tab_category->id}}" role="tab" aria-selected="true">
{{$tab_category->title}}
</a>
</li>
#endforeach
</ul>
</div>
Print Tab Content
#foreach($category_tabs as $tab_category)
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-outdoor-{{$tab_category->id}}" role="tabpanel">
<div class="row clearfix">
<div class="col-lg-6">
#foreach($tab_category->latestPosts as $latestPost)
<article>
<div class="entry-image mb-3">
<img src="{{asset($latestPost->thumbnail)}}" alt="{{$latestPost->title}}">
</div>
<div class="entry-title">
<h3><a target="_blank" href="{{route('post.show',[$latestPost->slug])}}">{{$latestPost->title}}</a></h3>
</div>
<div class="entry-content clearfix">
<p>{{$latestPost->description}}</p>
</div>
</article>
#endforeach
</div>
<div class="col-lg-6">
#foreach($tab_category->beforeLatestPosts as $beforeLatestPost)
<article>
<div class="entry-image">
<img src="{{asset($beforeLatestPost->thumbnail)}}" alt="">
</div>
<div class="entry-c">
<div class="entry-title">
<h4>{{$beforeLatestPost->title}}</h4>
</div>
</div>
</article>
#endforeach
</div>
</div>
</div>
</div>
#endforeach

Drupal dropdown menu that moves content down

I need to have a menu/mega menu that will shift the content down when the dropdown opens like on Facebook for Business menu: https://www.facebook.com/business
I saw the same effect on Avast: http://www.avast.com
Thanks for the help, it's much appreciated!
For behavior you want you can use: slideDown() function of JQuery. Then you can bind mouseover and mouseout events over menu items and call slideDown and slideUp function.
Example in JSFiddle
HTML:
<div class="container">
<div class="navbar navbar-default">
<div class="navbar-header">
<a class="navbar-brand" href="#">Bootstrap 3</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown active menu-item" data-menuItem="#getstarted">
Getting started <b class="caret"></b>
</li>
<li class="menu-item" data-menuItem="#css">CSS</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="active">Customize</li>
</ul>
</div>
</div>
<div class="col-xs-12 menu-panel" hidden="" id="getstarted">
<ul>
<li>Sign-in page</li>
<li>Sticky footer</li>
<li>Offcanvas</li>
<li>Carousel</li>
<li>Theme</li>
</ul>
</div>
<div class="col-xs-12 menu-panel" hidden="" id="css">
<ul>
<li>Sign-in page</li>
<li>Sticky footer</li>
</ul>
</div>
</div>
JavaScript:
$(".menu-item").mouseenter(
function(e){
e.preventDefault();
e.stopPropagation();
var $active=$(".active-menu-panel");
$active.removeClass("active-menu-panel").hide();
var selector=$(this).attr("data-menuItem");
if($active.length){
$(selector).addClass("active-menu-panel").show();
} else {
$(selector).addClass("active-menu-panel").slideDown();
}
}
);
$(".menu-panel").mouseleave(function(){
$(this).removeClass("active-menu-panel").slideUp();
});

Resources