mercredi 20 mars 2019

Getting issue "Class 'App/Category' not found"

In Category.php

namespace App;

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

and in Controller.php

namespace App\Http\Controllers;

use App\Models\Category;

use App\Setting;

use App\Post;

use function GuzzleHttp\Promise\all;

use Illuminate\Http\Request;



via Chebli Mohamed

samedi 16 mars 2019

Save data generated to a specific table in database

This is the output in my view

This is the code in my controller

public function showFinalYear(){

    $finalyear_students = Matrix::get()->where('total_subject_left','<',10);

    return view('admin.final_year_list')->with(compact('finalyear_students'));}

This is the code in my model

class Matrix extends Model
{
  protected $table = 'matrices';
  public $timestamps = false;

  public function getSubjects()
  {
    $subjects = [];
    $subjects[] = $this->S1 == 1 ? 'S1' : null;
    $subjects[] = $this->S2 == 1 ? 'S2' : null;
    $subjects[] = $this->S3 == 1 ? 'S3' : null;
    $subjects[] = $this->S4 == 1 ? 'S4' : null;
    $subjects[] = $this->S5 == 1 ? 'S5' : null;
    $subjects[] = $this->S6 == 1 ? 'S6' : null;
    .... and so on

    $subjects = array_filter($subjects);

    return implode(',', $subjects);
   }
 }

This is my code in the view

<tbody>
  @foreach ($finalyear_students as $fy)
    <tr class="gradeX">
      <td></td>
      <td></td>
    </tr>
  @endforeach
</tbody>

the problem happened when I want to save the student name and subjects based on the output where the code save the student name and subject doesn't work.

This is what I have tried but it does not work.

public function showFinalYear(){

    $finalyear_students = Matrix::get()->where('total_subject_left','<',10);

    DB::table('finalyear_students')
        ->updateOrInsert(
            ['student_name'=>$finalyear_students->student_name],
            ['subject'=>$finalyear_students->getSubjects()]
        );

    return view('admin.final_year_list')->with(compact('finalyear_students'));
}

Hopefully, someone can help me to solve this, Thanks in advance



via Chebli Mohamed

jeudi 14 mars 2019

Getting error when click in my second post for the single page

When I am trying to open first post in single page, it's opening and when trying to open my second post in single page it;s showing "Trying to get property 'title' of non-object"

Here is code

FrontendController

public function singlePost($slug)
{

    $post= Post::where('slug', $slug)->first();


    return view('single')->with('post', $post)


        ->with('title', $post->title)
        ->with('settings', Setting::first())
        ->with('categories', Category::take(4)->get());

}

single.blade.php

in that I am using same frontend controller for same page @extends('layouts.frontend')

@section('content')

<div id="product-post">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="heading-section">

                    <img src="" alt="" />
                </div>
            </div>
        </div>
        <div id="single-blog" class="page-section first-section">
            <div class="container">
                <div class="row">
                    <div class="product-item col-md-12">
                        <div class="row">
                            <div class="col-md-8">

                                <div class="product-content">
                                    <div class="product-title">
                                        <h3></h3>
                                        <span class="subtitle">4 comments</span>
                                    </div>
                                    <p>
                                        {!! $post->content!!}

                                    </p>
                                </div>



                                <div class="leave-form">
                                    <form action="#" method="post" class="leave-comment">
                                        <div class="row">
                                            <div class="name col-md-4">
                                                <input type="text" name="name" id="name" placeholder="Name" />
                                            </div>
                                            <div class="email col-md-4">
                                                <input type="text" name="email" id="email" placeholder="Email" />
                                            </div>
                                            <div class="subject col-md-4">
                                                <input type="text" name="subject" id="subject" placeholder="Subject" />
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="text col-md-12">
                                                <textarea name="text" placeholder="Comment"></textarea>
                                            </div>
                                        </div>
                                        <div class="send">
                                            <button type="submit">Send</button>
                                        </div>
                                    </form>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>




@endsection



via Chebli Mohamed

mercredi 13 mars 2019

Laravel Optimise sum query

I would like to get sum go deposit, withdraw and net count. Currently, I have ~10,335,633 rows data(~1.8GB), it takes more than 7 seconds to calculate the sum. How should I improve my query to make it faster?

Query:

        $depositQuery = Deposit::whereBetween('created_at', [$start, $end])->approved();
        $withdrawQuery = Withdraw::whereBetween('created_at', [$start, $end])->approved();
        if($request->has('user_id')){
            $depositQuery = $depositQuery->where('user_id', $request->user_id);
            $withdrawQuery = $withdrawQuery->where('user_id', $request->user_id);
        }
        $deposits = $depositQuery->sum('amount');
        $withdraws = ($withdrawQuery->sum('amount'))*-1;
        $net = $deposits + $withdraws;

Result:

Deposit     Withdraw     Net Amount
20,946.00   15,066.00    5,880.00



via Chebli Mohamed

mardi 12 mars 2019

Apply middleware when URL pattern matches in Laravel

I am trying to add a simple middleware to my project that checks if a user is allowed to access a project. My approach is:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;


class UserProjectFit
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        if ($request->is('*/projects/*')) {

            $projectUserId = DB::table('project_user')
                ->where('project_id', '=', $request->project['id'])
                ->where('user_id', '=', Auth::user()->id)
                ->first();

            if (is_null($projectUserId)) {
                abort(404);
            }

        }

        return $next($request);
    }
}

It basically works, but I have also routes like e. g. projects/create and here the middleware kicks in too. The idea would be that the middleware only takes action in case the URL contains the string project and an id, e. g. …projects/1/…

What would be a good way to solve that? If my approach isn't good, I am happy to read your suggestions.



via Chebli Mohamed

samedi 9 mars 2019

Composer update issues

I ran php composer.phar update using ssh on my shared hosting, it installed/updated the dependencies and upgraded my laravel 5.1 to 5.7 but then i got these fatal errors when i opened my site that was previously returning 404 error on all pages except the homepage, now it's saying it can't find some files

Class 'Illuminate\Routing\ControllerServiceProvider' not found [Symfony\Component\Debug\Exception\FatalThrowableError]

Now even the homepage is not working anymore.

Note: i followed instructions from previous question here and removed the entries from config/app but that did not fix it.



via Chebli Mohamed