vendredi 6 avril 2018

Laravel 5.1, passing my ID

I'm trying to pass an ID of a past and insert it into another database.

I got a page with shows posts like this

/posts/{id}

. From there on i want to add a comment section, so i got my form set up like this:

<form method="POST" action="/posts//comments">

(When i inspect the code, it inserts the right id and the "" spot.

inside my routes.php i got the folling route:

Route::post('/posts/{post}/comments', 'CommentsController@store');

and inside CommentsController.php i got the following

    public function store(Post $post)
{
    Comment::create([

        'body' => request('body'),
        'post_id' => $post->id 
    ]);

    return back();
}

When ever i try to add a comment i get this error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post_id' cannot be null

When i change my code from CommentsController.php to this:

'post_id' => "2"

it works fine (but the comment will always be added at post with ID 2) I can't seem to find out why my id wont go trough.

Anny help with this?



via Chebli Mohamed

How to call multiple methods or controllers to a same route in laravel

Find below the controller code with two methods and suggest me how to call these two methods in the same route or whether I have to create two different controllers for the same page(route).

class TicketController extends Controller
{
    public function show(){
    $results=Whmcs::GetTickets([

    ]);
    return view('clientlayout.main.index',compact('results'));
}

    public function set(){
    $test=Whmcs::GetInvoices([

    ]);
    return view('clientlayout.main.index',compact('test'));
}

}

Find below the route file..

  Route::get('clientlayout.main.index','TicketController@show');

  Route::get('clientlayout.main.index','TicketController@set');

Find the code in the blade file and after running this I'm getting an error as undefined index:Results.

  @foreach($results['tickets']['ticket'] as $key)

  
  @endforeach



  @foreach($test['invoices']['invoice'] as $value)

  
  @endforeach

When I run these two foreach loops in a different blade file it executes correctly..But I need these two results to be viewed in the same file.. So suggest me a solution to view both tickets and invoices in the same index page...



via Chebli Mohamed

jeudi 5 avril 2018

Laravel 5.5 : Get mongodb documents between specific date range

Following is the structure of Mongo-DB document:

{
   _id : 'some id',
   message : 'some message',
   createdTime : ISODate('2018-03-08T08:01:46.000Z')
 }

I want to get documents in specific date range, my code is as following:

 $results = Messages::whereBetween(
                             'createdTime', array(
                                 "2018-4-1",
                                 "2018-4-4"
                             )
                         )->get();
        return $results;

but the code is not working, kindly guide me how can I achieve it.



via Chebli Mohamed

mercredi 4 avril 2018

Identify Route's Group in Middleware

My ultimate objective is to limit accesses to the group of routes by validating permissions provided to the user.

These target 'group of routes' have ONE COMMON PARENT GROUP and may have zero or more sub-groups, such that, if access to these target 'group of routes' is permitted/accessible to the user then, all its sub-route groups are also accessible to the user.

To achieve this, I believe I need to differentiate these target group of routes by any uniqueString/parameter in middleware, which is indeed answered here.

But, I want to generalize this further, by applying middleware to common SINGLE PARENT GROUP of all these target group of routes and identify these target group of routes by any means in the middleware.

So, my question is how do I identify/differentiate these target group of routes in the middleware? Is there any way to do so?



via Chebli Mohamed

How to I specify Tinker to use a different database connection?

I have two database connections. One for my application and another for testing. In my ..\config\database.php

         'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

        'testing' => [
            'driver'    => 'mysql',
            'host'      => env('DB_TEST_HOST', 'localhost'),
            'database'  => env('DB_TEST_DATABASE', 'forge'),
            'username'  => env('DB_TEST_USERNAME', 'forge'),
            'password'  => env('DB_TEST_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

I am able to change the database connection in seeding using

php artisan db:seed --database=testing

I wanted to use tinker for the connection 'testing' but unable to change it. Is there any way to change the database connection for tinker similar with database seeding?



via Chebli Mohamed

lundi 2 avril 2018

PHP Laravel. What are the changes in this code that media searches by id, but in url it shows slug? Please help me to solve this routes error

Query :

Due to this below code url looks like : /downloadpage/34 I want to make url seo friendly like : /downloadpage/slug

In table 'media' there is a column - slug

How to change in this code that media searches by id, but in url it shows slug?

routes.php file :

Route::get('/downloadpage/{id}', [
    'as' => 'downloadpage', 'uses' => 'DownloadController@index'

DownloadController.php file :

public function index($id){
    $images   = DB::table('media')->where("id", "=", $id)->get();
    $popular  = DB::table('media')->orderBy('count_download', 'DESC')->take(5)->get();
    $mostlike = DB::table('media')->orderBy('count_like', 'DESC')->take(5)->get();
    $alsolike = DB::table('media')->orderByRaw("RAND()")->where("id", "!=", $id)->take(3)->get();
    $settings = Setting::first();
    $previous = DB::table('media')->where('id', '<', $id)->max('id');
    $next     = DB::table('media')->where('id', '>', $id)->min('id');
    $imgshare = DB::table('media')->where("id", "=", $id)->first();
    return view('download.downloadpage', ['images'=>$images , 'popular'=>$popular, 'mostlike'=>$mostlike, 'alsolike'=>$alsolike, 'settings'=>$settings, 'previous'=>$previous, 'next'=>$next, 'imgshare'=>$imgshare]);
}



via Chebli Mohamed

Can I use laravel dusk package in Laravel 5.1 project?

There are few functions that are found in Laravel Dusk package but cannot find in Jeffrey Way's "Integrated" package. Can I require Laravel Dusk for my Laravel version 5.1? Is it mandatory to upgrade the project to Laravel 5.4?



via Chebli Mohamed