Laravel: hasMany Relationship is null / empty - php

my problem is, that my hasMany relationship returns just an "empty" collection:
Collection {#172 ▼
#items: array:1 [▼
0 => 0
]
}
My belongsTo relationship just returns "null".
Here are the eloquents:
Game.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Game
*
* #property integer $id
* #property integer $steamid
* #property string $name
* #property \Carbon\Carbon $created_at
* #property \Carbon\Carbon $updated_at
*/
class Game extends Model
{
protected $fillable = [
'appid', 'name'
];
public function keys()
{
$this->hasMany('App\Models\Game\Key', 'id', 'game_id');
}
}
Key.php
<?php
namespace App\Models\Game;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Game\Key
*
*/
class Key extends Model
{
protected $fillable = [
'key', 'game_id', 'author_id',
];
public static function getRandom()
{
if (!self::available()) return false;
$keys = Key::where('redeemer_id', 0)->get();
$key = $keys[ rand(0, count($keys) - 1) ];
return $key;
}
public static function available()
{
$keys = Key::where('redeemer_id', 0)->get();
$count = count($keys);
if ($count > 0) return $count;
return false;
}
public function author()
{
$this->hasOne('App\Models\User', 'author_id');
}
public function redeemer()
{
$this->hasOne('App\Models\User', 'redeemer_id');
}
public function game()
{
$this->belongsTo('App\Models\Game', 'game_id');
}
}
User.php
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
* App\Models\User
*
* #property integer $id
* #property string $name
* #property string $email
* #property string $password
* #property string $remember_token
* #property \Carbon\Carbon $created_at
* #property \Carbon\Carbon $updated_at
*/
class User extends Authenticatable
{
protected $fillable = [
'username', 'email', 'password'
];
protected $hidden = [
'password', 'remember_token',
];
public function keys()
{
$this->hasMany('App\Models\Game\Key');
}
public function canRedeem()
{
// TODO: not tested yet
if(count(self::keys()) == 0) return true;
if(Carbon::now(-24) > self::keys()->first()->created_at) return true;
return false;
}
}
these are my migrations:
user-table
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
game-table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateGamesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
if (!Schema::hasTable('games')) {
Schema::create('games', function (Blueprint $table) {
$table->increments('id');
$table->integer('appid')->unsigned();
$table->string('name');
$table->timestamps();
});
}
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//Schema::drop('games');
}
}
keys-table
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateKeysTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('keys', function(Blueprint $table) {
$table->increments('id');
$table->string('key');
$table->integer('game_id')->unsigned()->index();
$table->foreign('game_id')->references('id')->on('games')->onDelete('cascade');
$table->integer('author_id')->unsigned()->index();
$table->integer('redeemer_id')->unsigned()->index();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('keys');
}
}
I googled alot and I found some "solutions" on Laracast, but they only tell me what I already have.
Can anyone help?
Deazl

The method that creates the relationship has to return a value, this is what I am saying:
Rather than:
public function keys()
{
$this->hasMany('App\Models\Game\Key', 'id', 'game_id');
}
You should have:
public function keys()
{
return $this->hasMany('App\Models\Game\Key', 'id', 'game_id');
}
The difference is the return statement, your methods that create the relationship do not return any, they should return the results of the hasOne and hasMany for all method that uses them for query building.
PS: You should add the return statement for the other methods that create a relationship.

If you have extra columns in your intermediate table you need to include them when defining your relation.
public function keys()
{
$this->hasMany('App\Models\Game\Key')->withPivot('author_id','redeemer_id')->withTimestamps();
}

Related

i want attach to tags but i get error Call to a member function attach() on null

i want create tag to my blog project | laravel
in models i make Many To Many relation in models but when i want to attach i get this error:
to a member function attach() on null
it is my blog model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use User;
class Blog extends Model
{
use HasFactory;
protected $fillable = [
'name',
'body',
];
/**
* Get the blog that owns the user.
*/
public function user()
{
return $this->belongsTo(User::class);
}
public function tags()
{
$this->belongsToMany(Tag::class);
}
}
and it is my tag model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
use HasFactory;
public function blogs()
{
$this->belongsToMany(Blog::class);
}
}
and it is my tag and blog_tag table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
Schema::create('blog_tag', function (Blueprint $table) {
$table->integer('blog_id')->unsigned()->index();
$table->foreign('blog_id')->references('id')->on('tags')->onUpdate('cascade')->onDelete('cascade');
$table->integer('tag_id')->unsigned()->index();
$table->foreign('tag_id')->references('id')->on('blogs')->onUpdate('cascade')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('tags');
Schema::dropIfExists('blog_tag');
}
};
and it is my blog table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('blogs', function (Blueprint $table) {
$table->id();
$table->integer('user_id');
$table->string('name');
$table->longText('body');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onUpdate('cascade')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('blogs');
}
};
and it is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use function Ramsey\Uuid\v1;
use App\Models\User;
use App\Models\Blog;
use Illuminate\Support\Facades\Auth;
use DateTime;
class BlogController extends Controller
{
public function __construct() {
$this->middleware('auth')->only('create');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('welcome');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('blog.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request, Blog $blog)
{
$validated = $request->validate([
'name' => 'required|max:100|min:3',
'body' => 'required|min:30',
]);
$name = $request->input('name');
$body = $request->input('body');
$blog->insert(['user_id' => Auth::id(),
'name' => $name,
'body' => $body,
'created_at' => new DateTime,
'updated_at' => new DateTime
]);
$blog->tags()->attach(1);
$request->session()->flash('sucsess', 'blog created succsessfully');
return back();
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($username, $blogName, User $user)
{
$user = $user->where('name', $username)->first();
$blog = $user->blogs->where('name', $blogName)->first();
// return $user->blogs->where('name', $blogName);
return view('blog.show', compact('user', 'blog'));
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($username, $blogName, User $user)
{
$user = $user->where('name', $username)->first();
$blog = $user->blogs->where('name', $blogName)->first();
$this->authorize('edit-blog', $user->id);
return view('blog.edit', compact('user', 'blog'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$validated = $request->validate([
'body' => 'required|min:30',
]);
Blog::find($id)->update([
'body' => $request->input('body')
]);
$request->session()->flash('sucsess', 'blog created succsessfully');
return back();
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy(Request $request, $id)
{
Blog::find($id)->delete();
$request->session()->flash('sucsess', 'blog deleted succsessfully');
return redirect()->route('dashboard');
}
}
I saw questions similar to yours on stack overflow, but none answered my question
pleas help my
try:
$newBlog = $blog->insert(['user_id' => Auth::id(),
'name' => $name,
'body' => $body,
'created_at' => new DateTime,
'updated_at' => new DateTime
]);
$newBlog->tags()->attach(1);

Call to a member function attach() on boolean - Laravel

i have a problem with function attach(). I have created 3 tables. 1 table is for added movies. Table 2 applies to the movie categories. 3 table is a collection of movie ID and category ID. A combination of many to many.
When i want add a new video with category from form this i have error -> Call to a member function attach() on boolean. If anyone knows the answer, please help. Thank you for your time!
VideoController.php -> look at the method store();
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests\CreateVideoRequest;
use App\Video;
use App\Category;
use Auth;
use Session;
class VideoController extends Controller
{
public function __construct(){
$this->middleware('auth', ['except' => 'index']);
}
public function index(){
$videos = Video::latest()->get();
return view('videos.index')->with('videos', $videos);
}
public function show($id){
$video = Video::find($id);
return view('videos.show')->with('video', $video);
}
public function create(){
$categories = Category::pluck('name', 'id');
return view('videos.create')->with('categories',$categories);
}
public function store(CreateVideoRequest $request){
$video = new Video($request->all());
//dd($request->all());
Auth::user()->videos()->save($video);
$categoryIds = $request->input('CategoryList');
$video->categories()->attach($categoryIds);
Session::flash('video_created', 'Added');
return redirect('video');
}
public function edit($id){
$video = Video::findOrFail($id);
return view('videos.edit')->with('video', $video);
}
public function update($id, CreateVideoRequest $request){
$video = Video::findOrFail($id);
$video->update($request->all());
return redirect('video');
}
}
Video.php - model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
protected $fillable = [
'title',
'url',
'description'
];
public function user(){
return $this->belongsTo('App\User');
}
public function categories(){
return $this->belongsToMany('App\Video')->withTimestamps;
}
}
Category.php - model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = [ 'name' ];
public function videos(){
return $this->belongsToMany('App/Video')->withTimestamps;
}
}
User.php - model (maybe you need a User.php model solves problems)
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function videos(){
return $this->hasMany('App\Video');
}
}
Migration - create_category_video_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoryVideoTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('category_video', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('category_id');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->unsignedBigInteger('video_id');
$table->foreign('video_id')->references('id')->on('videos')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('category_video');
}
}
Migration - create_categories_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
Migration - create_videos_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateVideosTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('videos', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->string('url');
$table->text('description');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('videos');
}
}
form.blade.php - simple form in Laravel Collection
{!! Form::select('CategoryList', $categories, null, ['multiple' => 'multiple']) !!}
For the timestamps for the pivot you would call the "method" withTimestamps() not a "property" named withTimestamps:
// Video
public function categories()
{
return $this->belongsToMany(Category::class)->withTimestamps();
}
// Category
public function videos()
{
return $this->belongsToMany(Video::class)->withTimestamps();
}

Non-static method App\User::getallreferrals() should not be called statically

am trying to call functions inside my model in my controller but i get this error.
(1/1) ErrorException Non-static method App\User::getallreferrals() should not be called statically. in HomeController.php (line 37)
this is my model.
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Auth;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'first_name','last_name','other_name','user_name','number2','email', 'password', 'number','bank_name','acct_number','acct_name', 'next_kin', 'next_kin_no', 'transaction_details', 'entry_matrix','referral_id', 'referred_by','first_downline_id', 'second_downline_id'
];
/**
* The attributes that should be hidden for arrays. `1
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function scopeGetallreferrals($query)
{
return $query->where('referred_by', Auth::user()->referral_id)->pluck('referral_id');
}
public function scopeGetallreferralsobject($query)
{
return $query->where('referred_by', Auth::user()->referral_id)->get();
}
public function scopeGetallreferrals2genobject($query)
{
$referrals = $this->getallreferrals();
return $query->where('referred_by', $referrals)->get();
}
public function scopeGetallreferrals2gen($query)
{
$referrals = $this->getallreferrals();
return $query->where('referred_by', $referrals)->pluck('referral_id');
}
public function scopeGetallreferrals3gen($query){
$referrals2gen = $this->getallreferrals3gen();
return $query->where('referred_by', $referrals2gen)->pluck('referral_id');
}
public function scopeGetcaptain($query)
{
return $query->where('referral_id', Auth::user()->referred_by)->pluck('referred_by');
}
public function scopeGetallcaptain2gen($query)
{
$captain = $this->getcaptain();
return $query->where('referral_id', $captain)->pluck('referral_id');
}
public function scopeGetallcaptain3gen($query){
$captain2gen = $this->getcaptain2gen();
return $query->where('referred_by', $captain2gen)->pluck('referral_id');
}
public function scopeGetallreferralsbyID($query, $id){
return $this->getallreferrals()->where('id', $id);
}
public function scopeGetallreferrals2genbyID($query, $id){
return $this->getallreferrals2gen()->where('id', $id);
}
public function scopeGetallreferrals3genbyID($query, $id){
return $this->getallreferrals3gen()->where('id', $id);
}
}
then my controller.
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\DateTime;
use App\User;
use Auth;
use Session;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$captain = User::getcaptain();
$referrals = User::getallreferrals();
return view('home', ['captain' => $captain, 'referrals' => $referrals]);
}
Please what am i not doing right? I have been on this project for a while now.
my migrations
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('other_name');
$table->string('user_name');
$table->string('number');
$table->string('number2');
$table->string('bank_name');
$table->string('acct_number');
$table->string('acct_name');
$table->string('email')->unique();
$table->string('password');
$table->string('next_kin');
$table->string('next_kin_no');
$table->string('entry_matrix');
$table->string('transaction_details')->nullable();
$table->integer('isconfirmed')->default(0);
$table->string('total_earned')->nullable();
$table->string('total_paid')->nullable();
$table->string('type_account')->nullable();
$table->integer('stage')->default(0);
$table->string('referral_id')->unique();
$table->string('referred_by')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
As it is saying the getallreferrals method is not static, but you are trying to call it with :, which means static.
It should be something like
use a Scope for this in User model
public function scopeGetallreferrals($query, $user)
{
return $query->where('referred_by', $user->referral_id);
}
and in controller
public function __construct(User $user)
{
$this->middleware('auth');
$this->user = Auth:user();
}
public function index()
{
$captain = $this->user->getcaptain();
$referrals = $this->user->getallreferrals($this->user)->pluck('referral_id');
return view('home', ['captain' => $captain, 'referrals' => $referrals]);
}

Why is saving many to many relationship in laravel only saving the first item of the collection?

Currently I am creating an API in laravel. The models are as follows:
Offer
Offer belongsToMany Days
Day
Day belongsToMany Offers
Type
BelongsTo Offer
Models for days and offers:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Offer extends Model
{
/**
* The database table used by the model.
*
* #var string
*/
use SoftDeletes;
protected $table = 'offers';
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $hidden = ['map_location', 'regular', 'name', 'distance_to_offer'];
protected $fillable = ['id','venue_id','type_id','day','venue_address','is_featured','repeater_days','image', 'venue_description', 'offer_headline','offer_subheader','offer_terms','venue_phone_number','venue_website', 'venue_name', 'venue_headline','venue_description','venue_latitude','venue_longitude','offer_when','offer_end_time','offer_start_time','offer_start_date','offer_end_date','featured_date','current_time','current_date'];
public static $rules = array(
'image' => 'image|mimes:jpeg,jpg,png,bmp,gif,svg'
);
protected $casts = [
'is_featured' => 'boolean',
'is_regular' => 'boolean',
'offer_when' => 'array'
];
/**
* Get the offers for the offer.
*/
public function days()
{
return $this->belongsToMany('App\Day','day_offer', 'offer_id', 'day_id');
}
public function types()
{
return $this->belongsToMany('App\Type', 'offer_type');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Day extends Model
{
/**
* Get the offer days for the offer.
*/
public function offers()
{
return $this->belongsToMany('App\Offer');
}
}
In the database we have days, offers and types. We have the following pivot tables for the relationships:
day_offer
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDayOfferPivotTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('day_offer', function (Blueprint $table) {
$table->integer('day_id')->unsigned()->index();
$table->foreign('day_id')->references('id')->on('days')->onDelete('cascade');
$table->integer('offer_id')->unsigned()->index();
$table->foreign('offer_id')->references('id')->on('offers')->onDelete('cascade');
$table->primary(['day_id', 'offer_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('day_offer');
}
}
offer_type:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOfferTypePivotTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('offer_type', function (Blueprint $table) {
$table->integer('offer_id')->unsigned()->index();
$table->foreign('offer_id')->references('id')->on('offers')->onDelete('cascade');
$table->integer('type_id')->unsigned()->index();
$table->foreign('type_id')->references('id')->on('types')->onDelete('cascade');
$table->primary(['offer_id', 'type_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('offer_type');
}
}
In my OffersController I have the standard methods of create, store, update etc and when in the create method I have got the days listed in a select dropdown with multiple select values allowed when saving to offer as follows:
_form.html.erb:
<div class="form-group">
{!! Form::select('days[]', $days, (isset($offer)) ? $offer->days->lists('id')->toArray() : null, ['class' => 'form-control', 'multiple' => true]); !!}
</div>
In the controller I use this in the create method:
public function store(Request $request, Offer $offer)
{
$offerId = $offer['id'];
$mytime = Carbon::now('Europe/London');
$currentTime = $mytime->format('h:ia');
$data = Input::except('image');
$validation = Validator::make($data, Offer::$rules);
if ($validation->fails()) {
return redirect('offers')->with('message', $validation->errors());
} else {
$offer = new Offer($request->input());
$day = Day::find($request->day);
$type = Type::find($request->type);
$file = Input::file('image');
$filename = date('Y-m-d-H')."-".$file->getClientOriginalName();
$path = public_path('images/offers/' . $filename);
Image::make($file->getRealPath())
->save($path);
$offer->save();
$offer->days()->sync( $request->get('days') );
$types = $offer->types()->save($type);
}
The issue I am having with this is its only saving one day out of what was selected in the select dropdown not all of them.
Can anyone tell mw where I might be going wrong with this?
Thanks,
Mark

Laravel 5 database will not seed

I have the following:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFinanceAccountsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('finance_accounts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('created_by')->index();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('finance_accounts');
}
}
And the seeder:
class FinanceAccountsTableSeeder extends Seeder
{
public function run()
{
DB::table('finance_accounts')->delete();
App\Models\Finance\FinanceAccount::create([
'name' => 'Default account',
'created_by' => 1
]);
}
}
Which is called via:
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
$this->call('UserRolesTableSeeder');
$this->call('UserTableSeeder');
$this->call('FinanceTransactionsTableSeeder');
$this->call('FinanceAccountsTableSeeder');
$this->call('CurrencyTableSeeder');
$this->call('UserProfileTableSeeder');
}
}
However, the table is created fine, but no data gets inserted.
The seeders for all my other tables work perfectly, just not for this one.
Does anyone have an idea why?
namespace App\Models\Finance;
use Illuminate\Database\Eloquent\Model;
class FinanceAccount extends Model {
protected $table = 'finance_accounts';
}

Categories