mardi 27 octobre 2015

How to get curl POST data?

I'm trying to POST data using command line using curl.

curl -v POST -d ' { "data1": "sample1", "data2": "sample2" } ' -H "Content-Type: application/json" -H "Authorization: BASIC vkslnkg561mZEqCq3l3RglAOAZ7d8XBeg2VjIAyC" http://localhost/Bowling/public/api/foo

I'm using Laravel 5, is there any way to get the Authorization token and the data passed in routes/controller?

Right now Im using

getallheaders();

I'm able to get Authorization token but not the data { "data1": "sample1", "data2": "sample2" }



via Chebli Mohamed

How to upload an image to a form with many to many relationships?

this is very similar to this question. I am using Laravel 5 and trying to add files(an image) to my database with a form. I have a form I use to add various data (title, description, image) to my Article class. An article also 'belongsToMany' categories and days (many to many r/ship). The code below allows me to upload my data but it adds three instances of the article! The first two instances have the correct photo path/name (photo.jpg). And the third instance adds a name like this to the db: /tmp/phphJIIY1. It adds to the pivot tables correctly.

I think it's this line of the 'store' function

        $article = Article::create($request->all());    

that is causing the problems, but I need that line or I get the error described in my last question.

How can I order/change this code so I can upload an image and add categories/days to my article? I have installed intervention\image but am not using it yet.

   public function create()
{

    $categories = Category::lists('name', 'id');
    $days = Day::lists('dayname', 'id');
    return view('articles.create', compact('categories', 'days'));
}

public function store(ArticleRequest $request)
{

   $image_name = $request->file('image')->getClientOriginalName();

   $request->file('image')->move(base_path().'/public/images', $image_name);

   $article = ($request->except(['image']));

   $article['image'] = $image_name;

   Article::create($article);

    $article = Article::create($request->all());      

    $categoriesId = $request->input('categoryList');

    $article->categories()->attach($categoriesId);

    $daysId = $request->input('dayList');

    $article->days()->attach($daysId);

    return redirect()->route('articles_path');

}



via Chebli Mohamed

Laravel 5 Hosting, amsgames laravelshop

I moved from local development where everything worked just fine to shared hosting. Now i keep getting this error when it has anything to do with logging in or registering a user. Please help! I have a deadline to meet!



via Chebli Mohamed

Laravel 5: Redirect "blog/{id}" to "blog/{slug}" - Trying to get property of non-object error

I'm trying to redirect users that go to blog/{id} to blog/{slug} with the slug being the slug of the id they entered.

What I am doing is not working. I dd{$article->slug} and get the correct string, yet when I try to redirect I get "Trying to get property of non-object" error.

I'm using route/model binding. This is my RouteServiceProvider:

    $router->bind('blog', function($slug)
    {
        if ($slug) {
            if(is_numeric($slug)) {
                $article = Article::findOrFail($slug);

                return redirect('blog/' . $article->slug);
            }

            return Article::with('images')->where('slug', $slug)->first();
        }
    });

This is my show method in controller:

public function show(Article $article)
{
    return view('blog.show', compact('article'));
}

Any ideas would be greatly appreciated!



via Chebli Mohamed

Laravel join query confusion

Very new to laravel and trying to do something simple but keep getting stuck. I have 3 tables

countries:
    id
    country

regions:
    id
    region
    country_id

wineries:
    id
    winery
    country_id
    region_id 

joined together as follows:

public function index()
{   
$wineries = Country::join('wineries', 'countries.id', '=', 'wineries.country_id')
    ->join('regions', 'regions.id', '=', 'wineries.region_id')
    ->select('countries.*')
    ->orderBy('countries.country', 'asc')
    ->get() 
    ->unique();
return view('winery.index', compact('wineries'));
}

I'm trying to return a table organized by country with each winery listed along with it's region. Countries without wineries are not to be displayed. My Index page, looks like this:

   <tbody>
        @foreach ($wineries as $country)
        <tr>
            <td colspan="2">{{ $country->country }}</td>
        </tr>
        @foreach ($country->wineries as $winery)
            <tr>
                <td>{{ $winery->winery }}</td> 
                <td>{{ $winery->region }}</td> 
            </tr>   
        @endforeach     
        @endforeach     
    </tbody>

All works well except I can't get the associated region for each winery since the region is not a part of the $winery array. Any advice on how to make it a part of array or how to accomplish this using a different query?



via Chebli Mohamed

Database conflicts between 2 Laravel apps on same server

I have two applications (App 1 and App 2) on the same server (Laragon) with two separate databases. They each have separate app keys and I've changed the name of the session variable so that they're unique, but for some reason I keep getting data from App 2 saved to the App 1 database. It doesn't happen all the time, maybe 1 in every 100 commits to the database but I have no idea why it would be happening. The apps were originally the one code base but I had to separate them due to differences at both the locations the apps are for.

Every now and then you get logged out for no reason as well. I'm using the default authentication system in Laravel 5.

Has anyone had anything like this happen before, or have I missed any other settings that should be different between the apps?

The apps are on a Windows server sitting inside Laragon and the database is an MSSQL database.

Thanks for your help



via Chebli Mohamed

advanced authentication routes in laravel 5

i currently have /dashboard route that is behind login page, which after login takes you to admin panel.

Route::get('dashboard', ['middleware' => 'auth', function()
{
    return view('dash.dashboard');
}]);

this is working fine. But I can't figure out how to prevent access to all the links in admin panel if not logged in. how can i prevent all the dashboard/{} routes? note - I'm still learning laravel.



via Chebli Mohamed