PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] How to include variable from one to another view in Laravel?

 March 18, 2022     laravel, laravel-5, php     No comments   

Issue

I am beginner in Laravel framework.I have a CategoryController and allCategory.blade.php view.I have a welcome.blade.php view.Now i want insert all categories value in the welcome.blade.php view.When i tried its show Undefined variable: $category

allCategory.blade.php :

@if(count($categories))
<table id="example2" class="table table-bordered table-hover" width="65%">
    <thead>
        <tr>
            <th># Index</th>
            <th>Category Name</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
    @foreach($categories as $category)
        <tr>
            <td>{{$category->sortInd}}</td>
            <td>{{$category->categoryName}}</td>
            <td><a class="btn btn-info badge bg-light-blue" href="{{route('editCategory',$category->id)}}">Edit</a> | <a class="btn btn-danger badge bg-red" href="{{route('deleteCategory',$category->id)}}">Delete</a></td>
        </tr>
    @endforeach
    </tbody>
</table>
{{$categories->links()}}
@endif

CategoryController:

public function index()
{
    $categories = Category::where('id', '>=', 1)->paginate(10);              
    return view('jobs.allCategory', ['categories' => $categories]);
}

I want this kind of solution. But it doesn't work.

Now i want to show category value same as allCategory.blade.php.How can pass the variable value from one view to another view?

N.B: If you needed any file please let me know.


Solution

Ok I have got a solution with compact() function. In my WelcomeController I just query all data of category and use compact() function. compact() function create an array from variables and their values. So I can query different table data and use compact() so that I can view all data in welcome blade with foreach() loop. Something like below:

WelcomeController :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Category;
use App\Job;
use App\Http\Requests;

class WelcomeController extends Controller
{
    public function index()
    {
        $jobs = Job::all();
        $categories = Category::all();
        return view("welcome",compact('categories','jobs'));
    }

Retrieve the value in welcome.blade.php :

 @foreach($categories as $category)
    <ul class="trends">
       <li><a href="#">{{ $category->categoryName}} &nbsp;<span class="item-numbers">(2,342)</span></a></li>
    </ul>
 @endforeach

Hope it will help someone.



Answered By - Chonchol Mahmud
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing