-1

I currently have 2 different tables(article and cost), controllers, models and routes. I am currently in a view of the cost but I want to access a column from the article database, how do I do so? The page is displaying undefined variable: article error.

Currently in view page of cost table, this results in an error

value="{{ $article->articlecost }}"

Web.php

Route::resource('articles', ArticleController::class);
Route::resource('costs', CostController::class);

CostController

use App\Models\Cost;
use App\Models\Article;

class SalaryController extends Controller
{
    public function create()
    {
        $articles = Article::all();
        return view('costs.create');
    }
}

1 Answers1

-1

You should pass the article to the view if you want to use it.

use App\Models\Cost;
use App\Models\Article;

class SalaryController extends Controller
{
    public function create()
    {
        $articles = Article::latest()->paginate(5);
        return view('costs.create', compact('articles'));
        
        // or
        // return view('costs.create', [
        //     'articles' => $articles 
        // ]);
    }
}

xenooooo
  • 1,106
  • 1
  • 2
  • 8
  • Every time you think you want to post an answer to a new question, the first thing you should do is take 5 or 10 minutes to search Stack Overflow (via Google search) and hunt aggressively for duplicate pages that already exist. If you find a duplicate that resolves the new question, please vote to close the new question. If you have something unique and valuable to add to the older page, please post your advice there. By ignoring pre-existing content, researchers have a harder time finding and comparing relevant insights on a given topic. – steven7mwesigwa Dec 20 '22 at 02:56