lundi 10 juillet 2017

Laravel 5.1 Redis - create cache for existing data

I am looking for some way to create cache for the existing data I have in laravel db. I have searched around the web. There are plenty of tips on how to enable caching for new data but none for existing. Example would be a large number of products for sale that are in my inventory database. Please advise.



via Chebli Mohamed

Call to undefined relationship [topic] on model [App\Theme]

I'm trying to display the latest Topic that is related to a Theme. I did this before and I tried the following code in the ThemesController (Index method, Sinse it's the homepage),

$topics = Theme::with('topic', 'lastTopic.user')->get();
$themes = Theme::all();

return view('themes.index')->with('themes', $themes)->with('topics', $topics);

And the method I'm calling is this.

public function lastTopic()
{
    return $this->hasOne(Topic::class)->latest();
}

The method sits inside the Theme Model, It doesn't matter if i put the method in the Topic Model of the Theme Model, It still gives me the same error. So what is causing this issue? This is for my homepage so the route is this Route::get('/', 'ThemesController@index')->name('home');. I know it is something I've done before but I can't figure it out. Thanks in advance



via Chebli Mohamed

how to publish vendor from package Laravel 5

So I've my custom Laravel 5.4 package, and i want to publish the vendor packages located into my package, this is my code (PackageServiceProvider.php):

$this->publishes([
  __DIR__.'/../vendor/soundasleep' => public_path('soundasleep'),
], 'html2text');

but when i run php artisan vendor:publish --tag=html2text the vendor is published into public folder and i want to be published into my application vendor, because when i tried use \Html2Text\Html2Text as html2text; into my package routes.php i got class Html2Text\Html2Text not found.

so please if someone has any idea i'll be very appreciative.



via Chebli Mohamed

Laravel 5.1 return eloquent between time_start and time_end

Here's my Sample Table:

reservation_date | time_start | time_end 7/10/2017 | 12:00:00 | 14:00:00

what i'm trying to do is to return all reservations between time_start and time_end base on the request by the user. and sub/add 2 hours from time_start and time_end to prevent conflicts. I tried

$times_data = $this->getTimesData($request->reservation_time_start, $request->reservation_time_end);

$reservations =  $reservations->whereBetween('reservation_time_start', $times_data)

protected function getTimesData($time_start, $time_end)
    {

        $time_from = $this->getTimeFrom($time_start);

        $time_to = $this->getTimeTo($time_end);

        return [$time_from, $time_to];
    }

protected function getTimeFrom($time)
    {
        $time = Carbon::parse($time);

        return $time->subHours(2)->toTimeString();
    }


    protected function getTimeTo($time)
    {
        $time = Carbon::parse($time);

        return $time->addHours(2)->toTimeString();
    }

But the result is not what i expected. i also tried

->whereRaw("(reservation_time_start BETWEEN ? AND ?) AND (reservation_time_end BETWEEN ? AND ?) ",$times_data)

but it returns null. Please help.



via Chebli Mohamed

vendredi 7 juillet 2017

Laravel 5.1 Search API Capability on an existing table with data

I need to build an API that will query mysql database that already has data in it to provide real time matches so that user can select on. Any suggestions? I am using Laravel 5.1 on EC2 and RDS.



via Chebli Mohamed

Laravel show last reply left on post

I'm trying to show the last reply that was left on a post. But only the name and date of it. I tried the following things

What I think this one should do is look at the replies to the post. Sort them by ID and then display the first one's username. But it doesn't work. $topic->replies->sortBydesc('id')->first()->user->username }}

I also tried this one where I requested all the replies in the entire forum and displayed the last one. It worked but I wasn't the one related to the post.

Does anyone know how I can make this work? Currently, i'm passing these two into the view (There is nothing wrong with these, they work but I think there should be something added)

public function show($id)
{


    $topics = Topic::with('theme')->find($id);
    $theme = Theme::with('topics')->find($id);

    return view('themes.theme')->with('topics', $topics)->with('theme', $theme);
}

Thanks in advance



via Chebli Mohamed

how to install vendor package from laravel package

So i'am in Laravel 5.4 and i've my custom package packages/dev/mypackage.

And i want to install soundasleep/html2text into my package, so i add this to my packages/dev/mypackage/composer.json :

{
  "require": {
    "soundasleep/html2text": "~0.5"
  }
}

then composer update , but when i tried to call html2text i got always class Html2Text\Html2Text not found.

So please if someone has any idea i'll be very appreciative.



via Chebli Mohamed