Tengo un problema al sacar datos de la base de datos. Lo que quiero hacer es sacar la lista completa de la base de datos, y filtrarla con eloquent.
Código PHP:
//UsersController.php
<?php
class UsersController extends BaseController {
public function index()
{
if (Auth::user() && Auth::user()->role == 'customer') {
$downloads = Download::all();
return View::make('downloads.index')->with('downloads', $downloads);
}
return View::make('login');
}
Código PHP:
//Add.php
<?php
class Add extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function download(){
return $this->belongsTo('Download');
}
}
Código PHP:
//Download.php
<?php
class Download extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function adds(){
return $this->hasMany('Add', 'user_id', 'did_id', 'created_at', 'updated_at');
}
}
Código PHP:
//index.blade.php
@extends('master')
@section('content')
@if(Auth::check())
<?php /*
$download->adds()->first()->did_id
$download->did == Add::where('did_id', '=', $download->did)->lists('did_id')[0]
*/
?>
<?php $newdatetime = new DateTime(); ?>
<?php echo "<p>".$newdatetime->format('Y-m-d H:i:s')."<p>"; ?>
@foreach($downloads as $download)
//en este if también quiero hacer algo como
//$download->adds()->first()->created_at > $newdatetime->format('Y-m-d H:i:s')
//$download->adds()->first()->updated_at < $newdatetime->format('Y-m-d H:i:s')
//ya que quisiera colocar un límite de descarga de 2 semanas
//devuelve "Trying to get property of non-object"
@if($download->did == $download->adds()->first()->did_id)
{{ $download->did }} Descarga <br>
@else
{{ $download->did }} NO Descarga <br>
@endif
@endforeach
@endif
<hr>
@stop
Espero puedan ayudarme, gracias a todos.