0

How do I retrieve the result array returned from my model :-

$result = DB::select('select title from mainTable');
return $result;

in my controller so that I can pass it to my view :-

$title = "Main Page";
$data =             //I want to assign the result to data
$view = View::make('mainpage')->with('data', $data)->with('title', $title);

3 Answers 3

2

If I understand your question correctly, you are trying to figure out how to add something to the $data variable and pass it into a View. If you assign something as

$data['result'] = DB::select('select title from mainTable');
return View::make('main page', $data);

You will now be able to access the query results as $result from within your blade template. I would definitely recommend using the ORM so that you can get the entire result in a single query, as in:

// Model /app/models/Main.php
class Main extends Eloquent {
    protected $table = 'mainTable';
}

// Controller (within route method)
    $data['result'] = Main::find(1);
    /* Gets the mainTable result with an id of 1 */

    return View::make('page', $data);

// Template /app/views/page.blade.php
    <h1>{{ $result->title }}</h1>
    <!-- Outputs the title for the result as an H1 HTML element -->
Sign up to request clarification or add additional context in comments.

6 Comments

yes, u guessed it right at the beginning. As I was using codeigniter, I am trying to put all the db queries in the model and then pass it to the controller which will then pass it to the view
but I still don't have an answer to y Laravel is trying to make us run a query in the controller or fetching data from the database from the controller
Yeah, I switched from CI to Laravel as well. It took me a little bit to wrap my mind around the ORM (especially relationships), but once it clicks it saves a LOT of time!
If you set up a Model, Laravel will only run the query(ies) when you call the model. So when you call Model::find(1) it uses the ORM to execute "SELECT * FROM mainTable WHERE id = 1" (depending on your db) and then returns the result as an object with the value of each column accessible as $object->column. Then you just pass that off to your $data array as $data['result'] = $object, and when you pass that to the View all contents of the $data array are accessible as their key value, i.e. $result. Does that make sense?
I'm sure you figured this out already, but all you have to do to create a model is create a file /app/models/Main.php with the content class Main extends Eloquent { protected $table = 'mainTable'; }
|
1

You can simply create a model like this

class Main extends Eloquent {
    protected $table = 'mainTable';
}

Then from your controller you can use following code to get all records from mainTable table:

$title = "Main Page";
$data = Main::get(array('title'))->toArray(); // Or Main::all(['title']);
return View::make('mainpage')->with('data', $data)->with('title', $title);

Update: You can use something like this if you want

class Main extends Eloquent {
    protected $table = 'mainTable';

    public function scopeGatAllByFieldName($q, $field = null)
    {
        if(is_null($field)) return $q;
        return $q->select([$field]);
    }
}

From your controller you may call it like:

$title = "Main Page";
$data = Main::gatAllByFieldName('title')->get()->toArray();
return View::make('mainpage')->with('data', $data)->with('title', $title);

2 Comments

but the only problem I have is that - shouldn't stuff like db queries, logic and all be in model...? Why is Laravel forcing the developer to put something like a db query in the controller
Laravel isn't forcing you to do anything. If you want to further abstract the DB query then by all means go ahead and do it.
0

The Laravel docs (http://laravel.com/docs) do a great job of demonstrating how to use the Database and Query Builder modules.

"The select method will always return an array of results." (http://laravel.com/docs/database.)

$data = DB::select('select title from mainTable');
$view = View::make('mainpage')->with('data', $data)->with('title', $title);

I much prefer using Query Builder.

$data = DB::table('mainTable')->select('title')->get();
$view = View::make('mainpage')->with('data', $data)->with('title', $title);

5 Comments

the problem i find here is that you have a db query in the controller. shouldn't it be in the model
You can create functions in your model to manipulate your database and then call them in you controller. In your case a function getTitle for exemple. And then Modelname::getTitle()->get()
@MrShibby I tried this but I get an error "Call to a member function get() on a non-object"
What if you just call getTitle() ? Like $data = mainTable::GetTitle()?
hey $data = mainTable::GetTitle() worked , just that i had to remove another line $data = ['first' => 'pet1', 'second' => 'pet2'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.