Brian Coolidge
Brian Coolidge

Reputation: 4649

Laravel 4 - Send data controller to view

Overview:

I have this method getDailyTimeRecordEntry()

public function getDailyTimeRecordEntry($record_id = false) {

    $user_id = Auth::user()->id;

    $dtr =  DailyTimeRecord::where('id', '=', $record_id)
            ->where('user_id', '=', $user_id);

    if ($dtr->count() > 0) { 
        $dtr = $dtr->first();
    } else if ($dtr->count() == 0 && $record_id != false) {

        // If other user accessing someone's record id, will redirect
        return  Redirect::route('dashboard-shinra')
                ->with('failure', 'The hell are you accessing someone\'s record?');
    }

    // if the dtr->count() is 0 or has a value it still send the data to view
    return  View::make('dashboard.shinra.dtr_entry')
            ->with('dtr', $dtr);
}

So we know that whether the url has a parameter of record id or blank, it still send the dtr value that I'm passing to view

Without parameter: www.mydomain.com/dashboard/shinra/daily-time-record-entry

With parameter: www.mydomain.com/dashboard/shinra/daily-time-record-entry/2

So on the view dashboard.shinra.dtr_entry

I have this certain line of code that has a dtr variable in it.

<input type="text" name="date" value="{{ ($dtr->date) ? $dtr->date : '' }}">

So, if we access

www.mydomain.com/dashboard/shinra/daily-time-record-entry/1

All the records will be output in the text fields like the input above. Because the parameter 1 has a record in database.

So if I access

www.mydomain.com/dashboard/shinra/daily-time-record-entry

Without the parameter, I'm encountering the error

enter image description here

I know that it's because of this one

{{ ($dtr->date) ? $dtr->date : '' }}

But, are there any fix for this?

I even tried

{{ (isset($dtr->date)) ? $dtr->date : '' }}

And still doesn't work.

Upvotes: 0

Views: 201

Answers (3)

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81167

The problem is that edge case, when count is 0 and you never execute the query.

So first I would suggest naming variables appropriately:

$query =  DailyTimeRecord::where('id', '=', $record_id)
        ->where('user_id', '=', $user_id);
...

$dtr = $query->first(); // or whatever
...
return View::make()->with('dtr', $dtr);

Then you would already know, that $dtr is unset, so it's pretty easy to debug.

Now, your code executes queries, that are not necessary:

// 1st query
if ($dtr->count() > 0) { 
// 2nd query if passed
    $dtr = $dtr->first();

// 2nd query if didn't pass
} else if ($dtr->count() == 0 && $record_id != false) {

While you could do it as simply as this:

$dtr = DailyTimeRecord::where('id', '=', $record_id)
        ->where('user_id', '=', $user_id)
        ->first();

if ( ! count($dtr))
{
  return ... // ERROR
}

return ... // correct row returned

Upvotes: 1

lowerends
lowerends

Reputation: 5267

In your method getDailyTimeRecordEntry(), you should also handle the case ($dtr->count() == 0 && $record_id == false) for when the $record_id is false. Otherwise you can have a situation where you don't get() or first() the $dtr query.

So you should have:

public function getDailyTimeRecordEntry($record_id = false) {

    $user_id = Auth::user()->id;

    $dtr =  DailyTimeRecord::where('id', '=', $record_id)
            ->where('user_id', '=', $user_id);

    if ($dtr->count() > 0) { 
        $dtr = $dtr->first();
    } else if ($dtr->count() == 0 && $record_id != false) {

        // If other user accessing someone's record id, will redirect
        return  Redirect::route('dashboard-shinra')
                ->with('failure', 'The hell are you accessing someone\'s record?');
    } else {
        // return something or make sure you do $dtr = $dtr->first() here
        $dtr = $dtr->first();
    }

    // if the dtr->count() is 0 or has a value it still send the data to view
    return  View::make('dashboard.shinra.dtr_entry')
            ->with('dtr', $dtr);
}

Also, you can use simpler syntax for the ternary operator in Blade. Instead of writing

{{ (isset($dtr->date)) ? $dtr->date : '' }}

you can write

{{ $dtr->date or '' }}

Upvotes: 1

MikeWu
MikeWu

Reputation: 3712

To me it looks like if you have no parameters set the id would be 0. That would imply you're getting an empty collection in your $dtr and passing that empty collection to the view. Than you're trying to access attribute of what you're thinking is model but in reality its a collection.

This might or not be the case based on what your db is returning when no parameter is passed to the controllers action.

Upvotes: 0

Related Questions