lundi 23 avril 2018

Have form in Laravel that adds multiple records with single field (allows nulls). How can I add additional fields?

My current form only inputs an ISBN and the logged in user's id. It also allows for blank rows in the form. I'm trying to figure out how to also add a field for the title, but I can't get anything to work. I'm guessing that it has to be done as an array, but all the examples that I find online aren't working for me and each example seems to be using different methods. This is my current form without the title field that I'd like to add. Any help or input would be seriously appreciated

Form:

<form action='wanted' method='POST'>
                <div class='form-group'>
                    <table>
                        <tr>
                            <td style="padding-bottom: 3px">
                               <input type="text" name="isbn[]" placeholder="enter isbn...">
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 3px">
                               <input type="text" name="isbn[]" placeholder="enter isbn...">
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 3px">
                               <input type="text" name="isbn[]" placeholder="enter isbn...">
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 3px">
                                <input type="text" name="isbn[]" placeholder="enter isbn...">
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 3px">
                                <input type="text" name="isbn[]" placeholder="enter isbn...">
                            </td>
                        </tr>

                        <input type="hidden" name="_token" value="">

                    </table>
                    <button type="submit" id="submit" class="btn btn-default">Add books</button>
                </div>

Here's my controller:

public function wanted(Request $request)
{
    $uid = Auth::user()->id;
    $booklist = Input::get('isbn');
        foreach($booklist as $onebook) {
            if(is_null($onebook)) continue;
                DB::table('wantedbooks')->insert(['isbn' => $onebook, 'user_id' => $uid]);
        }
    return view("bookswantedsubmitted");
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2FcA9NM
via IFTTT

Overwrite paginator items in Laravel

I have a list of ids that come from a complicated query. I paginate the response of that complicated query and then use those ids to get the eloquent models. I then put it through a resource with the pagination meta data.

The laravel AbstractPaginator class protects the items attribute so you cannot easily overwrite them. I have a solution to use a ReflectionProperty but I'm after a simpler solution.

// $studentIds == Long complicated query that would return 1000s of students
$data = $studentIds->paginate(); // Execute the query limited to 15.

// Use ids to get eloquent models for our students
$students = Student::whereIn('id', $data->pluck('id'));

// Overwrite paginate `items` attribute so that our response contains pagination meta.
$rp = new \ReflectionProperty('Illuminate\Pagination\AbstractPaginator', 'items');
$rp->setAccessible(true);
$rp->setValue($data, $students);

return new StudentResourceCollection($data);

The above works but it is not particularly elegant, I'm after a better solution.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Hpd0tq
via IFTTT

Laravel: one to one relationship becomes one to many relationship

When I create a one-to-one relationship migration, laravel creates a one-to-many relationship.

PHP 7.1 & MySQL 5.7

The models are: Persona & User.

Persona:

public function user()
{
    return $this->hasOne('App\User', 'persona_id', 'id');
}

User:

public function persona()
{
    return $this->belongsTo('App\Persona', 'persona_id', 'id');
}

Migrations:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('persona_id')->unsigned();
    $table->foreign('persona_id')->references('id')->on('personas')->onDelete('cascade');
    $table->unique(['persona_id', 'id']);
    $table->string('email')->nullable()->unique();
    $table->string('password')->nullable();
    $table->rememberToken();
    $table->timestamps();
});


Schema::create('personas', function (Blueprint $table) {
    $table->increments('id');
    $table->string('tipo');
    $table->integer('cedula')->unsigned()->unique();
    $table->string('primer_nombre');
    $table->string('segundo_nombre')->nullable();
    $table->string('primer_apellido');
    $table->string('segundo_apellido')->nullable();
    /* Agregar demas campos que se requieran */
    $table->timestamps();
});

How can I make the relationship created in the database one by one and not one to many? Currently this is allowing me to save more than one user per person.

Thanks.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2vEwpoL
via IFTTT

"Array to string conversion" errorr Laravel - Trying to save array to database

Sorry Im new to Laravel and trying to save to the database for the first time. Im trying to save an array to the database but the error "array to string conversion" is appearing. I've tried changing the string value in the migration file to other options but the same error is appearing.

Controller

    public function store(Request $request)
{   
    Myroutes::create([ //posting to acc table
        'start' => $request->start,
        'end' => $request->end,
        'waypoints' => $request->waypoints
    ]);

    return redirect('/');
}

migration

 public function up()
{
    Schema::create('myroutes', function (Blueprint $table) {
        $table->increments('myroute_id');
        $table->integer('user_id');
        $table->string('start');
        $table->string('end');
        $table->string('waypoints');
        $table->timestamps();
    });
}

model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Myroutes extends Model
{
    protected $fillable = [
        'user_id',
        'start',
        'end',
        'waypoints'
    ];
}

View

<div id="dynamicInput" class="form-group">
                    <label>Additional Destinations</label>
                    <input type="text" name="waypoints[]" class="form-control" autocomplete="on">
                </div>

Database table

database-img



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2HUtcE7
via IFTTT

hi friends I need help in laravel with eloquent. I have two tables categories and jobs

I have two tables categories and jobs. In the jobs table I have a category_id attribute that has migrated. how to retrieve the attributes from the jobs table from the categories table



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2vGGX70
via IFTTT

Laravel: Undefined variable after a post action

I'm making profile edit page and I had a problem after post action.

Routes file is

Route::get('profile', 'ProfileController@profile')->name('profile');  
Route::post('profile', 'ProfileController@update');

Profile page Controller

public function profile()
    {
        $cities = City::all();
        $current_city = Auth::user()->city;
        $current_city_name = City::cityName($current_city);
        return view('profile.edit', compact('cities', 'current_city', 'current_city_name'));
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'email' => 'required|string|email|max:255',
        ]);
    }

    public function update(Request $request)
    {
        $this->validator($request->all())->validate();
        $user = Auth::user();
        $user->email = $request->input('email');
        $user->city = $request->input('city');
        $user->save();
        $request->session()->flash('message', 'Information successfully changed');
        return view('profile.edit');
    }

And finally blade template code

<select name="city" id="city" class="form-control">
      <option value=""></option>
      @foreach ($cities as $city)
          <option value=""></option>
      @endforeach
</select>

First entrance on profile page is ok. But after form submitting I have an error

Undefined variable: current_city (View: C:\OSPanel\domains\ang\laravel\resources\views\profile\edit.blade.php)



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2JkbHg2
via IFTTT

Relationship between two tables in Eloquent (Laravel) based on a secondary table

Maybe I don't know the terminology for this, but I'm trying to create a HasMany relationship between Person and Car object.

Person:
id | user_name | created_at ....


Car:
id | make | model | year ...

Person_Car: 
car_id | person_id

So obivously the relationship is defined by linking through the Person_Car relationship. Maybe I'm brain farting but I've forgotten what that this relationship is called and how to define Person as hasMany Cars

What's the name of the relationship and how can I define it in a Laravel Eloquent object?

Thanks,

gg



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2K978Xd
via IFTTT