jeudi 28 décembre 2017

Laravel Echo Server and Production

I have gone over the following two links

http://ift.tt/2ldw44R

and

http://ift.tt/2lmdzun

but neither have left me feeling like I know how to run laravel echo server in production...

My question is, How do I go about running Laravel Echo Server in production?



via Chebli Mohamed

Laravel view share wont work

I have the following class

use Illuminate\Support\Facades\View;


class WebUser

{


    public function __construct()
    {

        $status = (object) [
            'isLogin' => (Session::get('token')? true : false),
            'isSubscribed' => $this->isSubscribed(),
            'isPlatinum' => $this->isPlatinum(),
            'isExpired' => $this->isExpired()
        ];

        View::share('thisUser',$status);

    }

This class will run after user is successfully signed in.

I tried to view the shared variable in view file , giving error

Undefined variable: thisUser (View: 



via Chebli Mohamed

Laravel 5.4 - DB Query Assistance

I am currently trying to write a query to display the value of 7 days worth of logs, which are automated into my table, for example the below data.

DB Structure

So what I am wanting is it to select the 7 newest entries in the posts (e.g. id 465-471) and then calculate them all together and spit out the number, e.g. 4393 for 7 latest enties of the above data.

The query I've written via SQL is as below:

SELECT SUM(posts) as poststw FROM (SELECT posts FROM `stats_log` ORDER BY dateline DESC LIMIT 7) tl

and this works successfully and manages to display via PhpMyAdmin the count correctly. I tried converting myself, and writing this in Laravel format as below, but it only ever displays 0 from the first line (to prevent page failing when query fails).

$posts_this_week = 0;
$posts_this_weeks = DB::select(DB::raw('SELECT SUM(posts) as poststw FROM (SELECT posts FROM `stats_log` ORDER BY dateline DESC LIMIT 7) tl'));

Can anyone assist me in writing the correct query which shows the correct number, as I'm having a mental blank right now!



via Chebli Mohamed

Error(s) trying to define a one-to-many relationship in laravel

I'm trying to define a one-to-many relationship in laravel where I have a Movimentacao class and a TipoMovimentacaoFin. One Movimentacao is one type TipoMovimentacaoFin and a TipoMovimentacaoFin can have many Movimentacao(s) associated with it. For it, in their models I declared:

class Movimentacao extends Model
{
public $timestamps = false;

public function tipo(){
    return $this->hasOne('App\TipoMovimentacaoFin', 'tipo', 'tipo');
 }
}

and

class TipoMovimentacaoFin extends Model
{
public $timestamps = false;

public function movimentacaos(){
    return $this->hasMany('App\Movimentacao', 'tipo', 'tipo');
  }
}

At the database level I got:

tipo_movimentacao_fins.tipo (varchar) as PK plus columns 1 and 2. and movimentacaos.tipo as FK to the first table. The names of the tables were auto generated by Laravel standards.

Now the problems begins in the View when I'm trying to access the type (tipo) of the Movimentacao. I give to the view the collections with $movimentacaos = Movimentacao::orderBy('data_prevista', 'desc')->get() and when I loop @foreach($movimentacaos as $mov) if I access $mov->tipo I get the value of movimentacaos.tipo column but if I want to access tipo_movimentacao_fins.column1 or 2 for this $mov I can't. If I just write it gives me the error "htmlspecialchars() expects parameter 1 to be string, object given". If I try to acess any of the columns like $mov->tipo()->column1 it says Undefined property. So I tryied to check what is in this object of type HasOne returned by $mov->tipo() but either dd, var_dump or var_export also gives errors. Also the Laravel docs didn't helped too much



via Chebli Mohamed

how to show project name in limited characters in Laravel?

I need show My laravel application project name with limited characters. that means as an example consider some project name contains more than 10 letters, then I need only show 10 characters.

My project name create controller is

public function store(Request $request)
    {
        $this->validate($request, [
            'name'     => 'required|min:3',
            'notes'    => 'required|min:10',
            'color'    => 'required',
            'group'    => 'required',
            'status'   => 'required'
        ]);

        $project = new Project;
        $project->project_name   = $request->input('name');
        $project->project_status = $request->input('status');
        $project->group          = $request->input('group');
        $project->color          = $request->input('color');
        $project->project_notes  = $request->input('notes');
        $project->user_id        = Auth::user()->id;

        $duplicate = Project::where('project_name',$project->project_name)->first();
        if($duplicate)
        {
            return redirect()->route('projects.index')->with('warning','Title already exists');
        }   

        $project->save();
        return redirect()->route('projects.index')->with('info','Your Project has been created successfully');

and project name showing blade file is

<div class="row">
             @foreach ($projects as $proj)
               <div class="col-md-3" style="border:3px solid {!!$proj->color!!};margin-left:5px;margin-bottom: 5px;">
               <h2><a href="/projects/">{!! $proj->project_name !!}</a></h2>

then how can I show only 10 charactors in My blade file?



via Chebli Mohamed

get value using Guzzle in Laravel

Currently i'm using guzzle htttp library to request a parameter from the server, using the code below

    $url = '//localhost/cgi-bin/sport/do.cgi?'.str_replace(' ','+',$auth).'&who.cgi';  
    $client = new Client();
    $result = $client->get($url);
    $body = (string) $result->getBody()->getContents();
    dd($body);
     Auth::attempt(['username' =>trim($body), 'password' =>'xxxx']);

the output is the following:

results

"""
{\n
"username": "inventario"\n
}\n
"""

some time ago using the same code delivered only the string value "inventario", but now it returns the json as shown above , how can i get only the needed value "inventario"??

Thanks in advance



via Chebli Mohamed

Check with Carbon if a date is on a different day

I'm trying to determine if 2 dates are on different days using Carbon. To do so, I'm using the method diffInDays(), but it doesn't work as I need.

An example

$dt1 = Carbon::createFromFormat('Y-m-d H:i:s', '2017-12-12 23:00:00');
$dt2 = Carbon::createFromFormat('Y-m-d H:i:s', '2017-12-13 8:00:00');
echo $dt1->diffInDays($dt2);

It says 0 because 24 hours are not past.

I thought to compare $dt1->day and $dt2->day, but if one was 2017-12-13 and the other 2018-1-13 the days would be the same, so I should check the months, the years... and I'm wondering if there is something which already does that out of the box.

Reading the documentation I can't find a method which directly does what I need: it should say "yes, $dt2 is a different day than $dt1". Is that possible with Carbon not writing a custom function?



via Chebli Mohamed