Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 2 years ago.
0

It is not clear to me what exactly you are trying to achieve. Maybe you could elaborate? I assume that you are trying to load a candy flavor list from your db table and create the select dropdown with it.

Model

No, you are not missing anything... in particular. The model extends Eloquent (model), which enables you to use all the Eloquent methods like described here

http://laravel.com/docs/5.0/eloquent

You could add custom functions to your model or define relationships, but the former is optional and the latter is not necessary in your case.

Controller

You may want to pass the data that you fetched from the database to the view. You can do that as described here (chapter "Passing Data To Views")

http://laravel.com/docs/5.0/views

Something like this

public function candy(Candy $candy)
    {
        $data = $candy->all();
        return view('pages.candy', $data);
    }

Please note that I am using method injection here, which is only available in Laravel 5. If you do not use L5 or do not want to use method injection, you need to import your model by using the respective 'use' statements.

Template

In the template, you will then have access to that array/object

@extends ('master')
@section ('content')
{{ dd($data) }}
@stop

Use the $data array/object to create your selection list or whatever you want to.

Last updated 9 years ago.
0

Hi Goeeda,

Thank you for your reply - I'm using Laravel 5 and you hit the nail on the head with what I'm trying to accomplish. I've never worked with any programming language and I'm just jumping right into it. I'm going to read through the documentation you listed and start with a clean route, model, controller and view and see what happens. I'll keep you posted if I'm successful

0

At this point, I just want to get some data back from the database and I'm having absolutely no luck at all. I've spent hours reading through the different examples online, but i'm just getting the error ```Bad Method Call - Method [Candy] doesn't exist'. My database contains one table and it has the following columns: ID, Candy_Flavors, Created_at - anyone can spot the error with my config?

###My CandyController.php file

<?php namespace App\Http\Controllers;

use App\candy;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class CandyController extends Controller
{

    public function Candies(Candies $candy)
    {
        $data = $candy->all();
        return view('pages.candy', $data);
    }
}

###My Candy.php Model

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Candy extends Model {
    protected $table = '$candies';
}

###My Candy.blade.php

@extends ('master')

@section ('content')

    <h1>Candy List Fail Attempts</h1>
    @foreach('$candies as $candy')
        <li>{{$candies -> candy_flavor}}</li>
    @endforeach
@stop
Last updated 9 years ago.
0

Your model has a mistake, if you define the table name yourself, do it without the '$'

protected $table = 'candies';

But I think it is not necessary define that variable, Laravel will automatically assume the plural of 'Candy' as your default db table.

Your controller has a mistake too, I injected the class name 'Candy' into the method (the name appearing within brackets)

public function candy(Candy $candy)

but you injected something that does not exist, the class 'Candies'...

public function Candies(Candies $candy)

The name of the function is up to you, this one can be the singular, plural or something completely else.

Please correct those mistakes and tell me how it went.

Last updated 9 years ago.
0

So close now! I'm getting an error

Undefined variable: candy (View: /home/vagrant/Code/my-first-app/resources/views/pages/candy.blade.php)

But in the error message I can see the flavors being called. I've tried {{ dd($data) }} and {{$candy -> candy_flavor}}. Any different ways to display in the view? Thanks for your help so far! If you call the variable in the controller I guess it doesnt carry over to the view?

Last updated 9 years ago.
0
Solution

There are many ways to pass data to the view. The one that I suggested is simply the second argument in

return view('pages.candy', $data);

If you are not certain about the object/array name in your view, I would suggest to use

return view('pages.candy')->with('candies', $data);

this way you name the object/array in the view and can access it

@foreach($candies as $candy)
    <li>{{$candy->candy_flavor}}</li>
@endforeach

...and loose the single quotes in the loop. Single quotes in PHP always mean a literal string, thus, the variables inside will not be referenced and functions not perfomed.

0

Awesome!! Thank you so much for your patience and detailed experience.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

romenyrr romenyrr Joined 1 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.