Need to add data in two tables

Published 3 months ago by jaeturma

i have downloaded a script running laravel 5.2 and in a process users need to register before adding some list (list <- child table) and this works fine. What I need to do is combine registration together with a 3 lists. Therefore, registration still goes to users table and list goes to list table. Do I need to specify this in model?

I have this code so far.

ListingUserController.php

use Auth;
use App\User;
use App\Listings;
use App\Categories;
use App\SubCategories;
use App\Location;

use App\Http\Requests;
use Illuminate\Http\Request;
use Session;
use Intervention\Image\Facades\Image; 
use Illuminate\Support\Facades\DB;

class ListingsUserController extends Controller
{
    public function submit_listing()    { 

    $categories = Categories::orderBy('category_name')->get();
    $locations = Location::orderBy('location_name')->get();
    return view('pages.addeditlisting',compact('categories','locations'));
}

public function addnew(Request $request)
{   
    $data =  \Input::except(array('_token')) ;
    $rule=array(
            'category' => 'required',
            'sub_category' => 'required',               
            'title' => 'required'
             );
    
    $validator = \Validator::make($data,$rule);
    if ($validator->fails())
    {
            return redirect()->back()->withErrors($validator->messages());
    } 
    $inputs = $request->all();
    
    if(!empty($inputs['id'])){
        $listings = Listings::findOrFail($inputs['id']);
    }else{
        $listings = new Listings;
    }
    
    $listing_slug = str_slug($inputs['title'], "-");
    
    if(empty($inputs['id'])){ 
        $listings->user_id = Auth::User()->id;
    } 

    $listings->cat_id = $inputs['category'];
    $listings->sub_cat_id = $inputs['sub_category'];
    $listings->location_id = $inputs['location']; 
      
    $listings->save();

    if(!empty($inputs['id'])){
        \Session::flash('flash_message', 'Changes Saved');
        return \Redirect::back();
    }else{
        \Session::flash('flash_message', 'Listing Added');
        return \Redirect::back();
    }                  
     
}     

IndexController.php

namespace App\Http\Controllers;

use Auth;
use App\User;
use App\Categories;
use App\Listings;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image; 

class IndexController extends Controller 
{

public function register()
{            
    return view('pages.register');
}

public function postRegister(Request $request)
{ 
    
    $data =  \Input::except(array('_token')) ;
    
    $inputs = $request->all();
    
    $rule=array(
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required|email|max:75|unique:users',
            'password' => 'required|min:3|confirmed'
             );
    
     $validator = \Validator::make($data,$rule);
 
    if ($validator->fails())
    {
            return redirect()->back()->withErrors($validator->messages());
    } 
           
    $user = new User;

$user->first_name = $inputs['first_name'];  
$user->last_name = $inputs['last_name'];
    $user->email = $inputs['email'];     
    $user->password= bcrypt($inputs['password']); 
       
    $user->save();
        
        \Session::flash('flash_message', '<b>Register successfully!');

        return \Redirect::back();
}    

Any help would greatly appreciated and helps me a lot.

bobbybouwmann

So you said you downloaded a script and now you want to adjust it right? So basically you have no idea what the code is doing and you suspect us to help you out! I can be wrong here, but I'm not really into helping someone that isn't even trying!

Adding data to a second table is the same as inserting it in the first table. In above code you already fill both tables, so you already have the code! That also makes me think that you have no clue at this point!

Try the documentation: https://laravel.com/docs/5.6

bashy
bashy
3 months ago (1,001,160 XP)

That code looks very Laravel 4.2ish :P

rin4ik
rin4ik
3 months ago (200,250 XP)

@bashy I'm wondering how do you earn experience points without best reply awards?

Cronix
Cronix
3 months ago (652,820 XP)

@bashy I'm wondering how you earn experience points without best reply awards?

@rin4ik What do you mean? @bashy has 560 best reply awards? You also get xp by posts, replies, watching videos, having someone like your post, etc.

Also, what does this have to do with the question?

rin4ik
rin4ik
3 months ago (200,250 XP)

@Cronix his XP magically growing. I know it isn't the place to discuss it sorry

bashy
bashy
3 months ago (1,001,160 XP)

@rin4ik I'm not keeping track but I just post replies and stuff. Are you stalking? :P

jaeturma

@bashy i have this in composer.json

    "require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.2.*",

does doesn't mean its 5.2?

@bobbybouwmann i have already modified a lot on this script. i know simple things going on so far. i just want to try making the script more friendly. I know eloquent is the right thing but i want to look into what it is existing. that is why i posted here guys just thinking if somebody can help me.

bashy
bashy
3 months ago (1,001,160 XP)

Yeah 5.2 but seems weird it using Input facade instead of the request class or helper :P https://laravel.com/docs/5.2/requests#retrieving-input

rin4ik
rin4ik
3 months ago (200,250 XP)
bashy
bashy
3 months ago (1,001,160 XP)

@rin4ik I've been watching videos just now? Although I've been talking to Bobby about our replies being liked a lot.

And it appears to be the same for Bobby. 50 odd best replies since last scoreboard cache.

bobbybouwmann

@rin4ik Yeah I'm the cheat... Thanks man! Putting all my hours in helping others and then getting this shit over me!

I contacted Jeffrey already about this... No reply so far! https://twitter.com/bobbybouwmann/status/991678176467324928

Don't judge without knowing the full story!

rin4ik
rin4ik
3 months ago (200,250 XP)

sorry about that @bobbybouwmann . but I know the full story @bashy since last month I noticed that when @bobbybouwmann actively participates in threads and earns best reply awards by helping others your xp grew all time . after I decided write you about it in this thread, because it was very strange to me. and when m-rk posted about xss vulnerability in this forum I totally understand your way of getting xp. I didn't know how can I prove that. and I thought about increasing @bobbybouwmann's best reply awards to test your activity when he reach 1 place. I have here question which answered @bobbybouwmann https://laracasts.com/discuss/channels/eloquent/how-can-i-get-rid-of-n1-problem … . and start marking his replies as best again and again.( I'd like this been fixed as well because laracasts big community and anybody can be in 1 place within 1 day by increasing best reply awards ) and you also start increasing your points.

bashy
bashy
3 months ago (1,001,160 XP)

@rin4ik You have been here 11 months? We have been here 4 years helping people on these forums. We received t-shirts when we were the first people to reach 500k and that was 3 years ago now. You've been faking his best answers and now he has no way of knowing how many he's actually got. I haven't used fake best replies and I'm not sure what XSS thread has to do with this?

bobbybouwmann

@rin4ik Dude stop it man! Everything I've worked for is now gone!

rin4ik
rin4ik
3 months ago (200,250 XP)

Please sign in or create an account to participate in this conversation.