dimanche 1 novembre 2015

Passing id from view using modal-dialog to controler

I'm using the bootstrap modal to create and update users. I manage to get the create function working but not the update. My controller works as such that if no $user->id variable is passed it will create a new user else if there is an $user->id it will update that instead. However I cant figure how to pass the $user->id to the controller as I'm using a modal dialog. Can someone help me with this? My code can be seen as below.

manage.blade.php

<button class="btn btn-sm btn-warning" type="button"
      data-toggle="modal" data-target="#form">Edit&nbsp;<i class="glyphicon glyphicon-edit"></i></button>

<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#form">Register New User</button>

<!-- Modal -->
<div id="form" class="modal fade" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
           <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title">User Information</h4>

...

controller.php

class ManageAccountsController extends Controller
{
    public $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function index() 
    {
        $users = User::orderBy('name')->get();
        $roles = Role::all();

        return view('manage_accounts', compact('users', 'roles'));
    }

     public function store(StoreUserRequest $request)
    {
      // validation already handled using this: http://ift.tt/16ovaIn
        $this->userRepository->upsert($request);

        Session::flash('flash_message', 'User successfully added!');

        return redirect()->back();

    }

    public function update(StoreUserRequest $request, $id)
    {
        // validation already handled using this: http://ift.tt/16ovaIn
        $this->userRepository->upsert($request, $id);

        Session::flash('flash_message', 'User successfully updated!');

        return redirect()->back();

    }
}

class UserRepository {

    public function upsert($data, $id = null)
    {

            // You will also need something like this
            if(isset($data['id']))
            {
                $user = $this->user->find($data['id']);
            }
            else {
                $user = new User;
            }

            $user->name     = $data['name'];
            $user->email    = $data['email'];
            $user->password = Hash::make($data['password']);
            $user->mobile   = $data['mobile'];
            $user->role_id  = $data['role_id'];

            // save our user
            $user->save();

            return $user;
        }
    }



via Chebli Mohamed

Laravel 5: return number of affected rows MySQL

how to return something similar to PHP function mysql_affected_rows() when use Laravel 5 DB class,

ex: DB::delete("DELETE FROM chat WHERE id = {$mid}");

how to return number of affected row?

thanks,



via Chebli Mohamed

Laravel - 2 queries over nothing?

Hello guys,

in the official doc of Eloquent for Laravel, this is the way to make an update to a table :

$flight = App\Flight::find(1);
$flight->name = 'New Flight Name';
$flight->save();

I must say, I don't really understand that. For me, it means that for a very basic update, there will be 2 queries to the database - a select and THEN an update ?

Anyone could explain me why this would be a good solution ?

Thanks !



via Chebli Mohamed

LARAVEL add custom method Illuminate\Cache\RedisStore

I want to extend Illuminate\Cache\RedisStore class to add a custom method where i can use JSON_encode instead of PHP Serializer before sending the data to cache servers.

I tried extending both the Redis and Cache Facades then registering the alias in App.php to load the extended facade on bootstrapping.But getting the error RedisStore doesn't implement this method.



via Chebli Mohamed

PHP Soapclient in Laravel 5

I want to use php's built in SoapClient class in laravel 5

I tried using it directly is shows error saying

Class 'App\Http\Controllers\SoapClient' not found.

I tried adding SoapClient in aliases array in config/app.php like this

'SoapClient' => SoapClient::class

Still not working

what should I do?

Thanks in Advance...



via Chebli Mohamed

Laravel 5 - view('test') in controllers not working as expect

I am new in Laravel. I read documentation and googling but I can't find answer for my problem.

Im trying to render a simple view test.blade.php (located at resources/views).

a) first try: in app/Http/routes.php I wrote

Route::get('/', function(){
    return view('test');
});

It's working. Hurray! I see my simple site

b) second try: in app/Http/routes.php i wrote

Route::get('/', 'WelcomeController@index');

I have created a controller WelcomeController.php in app/Http/Controllers Inside controller I wrote:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class WelcomeController extends Controller
{
    public function index()
    {
        return view('test');
    }
}

And....this is not working, I see only blank page. Ok I was digging and digging and trying to understand what's the problem. I changed retun view('test') to return view()->make('test'); but this is not working too - it generates only blank page....

Then I simply changed retun to echo: echo view('test'); It is working like charm.

What am I doing wrong? all examples in the internet shows return view('..') not echo view('...') (which is ugly)...

Please help me :)



via Chebli Mohamed

Secure and clean code in laravel 5.1

In fact i have a question about the clean of code
i try to get some value in blade file , I Am confused between two approaches i think both are right but i need to know who's clean and secure more with the reason

First approach in my blade directly using Eloquent

@foreach (Auth::user()->company->country->cities as $city) {{$city->name}} @endforeach

Second approach using Injecting Services by create this method in my model and use it in my blade using laravel 5.1 Injecting Services

 public function getCity()
{
    foreach(Auth::user()->company->country->cities as $city) {
        return $city->name ;
    }
}

Thanks For Your Time .



via Chebli Mohamed